views:

687

answers:

4

In Actionscript 3 I see that if I press the "8" key (from the numpad) I got the result of keyCode of the numpad key "8", but when I disable the "Num Lock" and press the numpad key "8", the event I receive the keyCode of the Up Arrow key...

How to difference between the keys of the numpad (with Num Lock disabled) and the original keys, such as the Arrows, End, Insert, Delete and so on?

I've tried using the keyLocation property of the event, the value of that property is 0 for the arrows and 3 for the numpad, but when the Num Lock is disabled the value for the 4, 6, 2 and 8 in the numpad are 0, so I can't difference between the numbers with numpad disabled and the Arrow keys.

Also the numLock property of the Keyboard class didn't work, because I can't know if I press the Up Arrow or the 8 whit Num Lock disabled (because they trigger the same event, or I think that)

A: 

I have not been able to find a way to do what you ask. My suspicion is that the actual key code itself is absorbed by the OS-layer driver and never reaches FLEX to be read. This was lent some weight by testing with my Microsoft Natural keyboard on my iMac (which does not have MS Natural drivers). Without the drivers, the keyboard was unable to switch numlock on, and the key code remained constant.

I don't know if this will help or not, but I did write a small testing program to determing what key codes, location, and character codes were visible by flex on each platform.

Here's the code for the test app:

<?xml version="1.0" encoding="utf-8"?>
<mx:Application xmlns:mx="http://www.adobe.com/2006/mxml" layout="absolute" applicationComplete="onApplicationComplete()" viewSourceURL="srcview/index.html">
    <mx:Script>
     <![CDATA[
      public function onApplicationComplete():void
      {
       stage.addEventListener(KeyboardEvent.KEY_DOWN,onKeyDown);
      }

      public function onKeyDown(keyboardEvent:KeyboardEvent):void
      {
       charCodeLabel.text = keyboardEvent.charCode.toString();
       keyCodeLabel.text = keyboardEvent.keyCode.toString();
       switch(keyboardEvent.keyLocation)
       {
        case KeyLocation.LEFT:
         keyLocationBox.text = "Left";
         break;
        case KeyLocation.NUM_PAD:
         keyLocationBox.text ="Num Pad";
         break;
        case KeyLocation.RIGHT:
         keyLocationBox.text = "Right";
         break;
        default:
         keyLocationBox.text = "Standard";
       }

      }
     ]]>
    </mx:Script>
    <mx:Label x="46" y="34" text="Just click anywhere inside the flash movie and start typing."/>
    <mx:Label x="46" y="72" text="You typed Char Code:"/>
    <mx:Label x="232" y="72" text="Key Code:"/>
    <mx:Label x="183" y="72" id="charCodeLabel"/>
    <mx:Label x="303" y="72" id="keyCodeLabel"/>
    <mx:Label x="46" y="98" text="Key Location:"/>
    <mx:Label x="134" y="98" width="68" id="keyLocationBox"/>

</mx:Application>

You can also simply use the live version if you want which I uploaded here: Flex Online Key Code Tool

Palantar
I agree that this is not possible within Flash. I don't think the distinction is eaten at the driver level though, as I'm pretty sure other platforms (a .exe, for example) can tell the difference between these two events.
fenomas
Thank you, and I'm really sad about this. Why this isn't possible within Flash? What a pitty...
unkiwii
A: 

Hi,

Maybe this will help you...

package {
    import flash.utils.Dictionary;
    import flash.ui.Keyboard;
    import flash.events.KeyboardEvent;
    import flash.display.Sprite;

    public class Main extends Sprite {

     private static const _NUMERIC_KEYS:Dictionary=new Dictionary();
     {
     _NUMERIC_KEYS[45]=0;
     _NUMERIC_KEYS[35]=1;
     _NUMERIC_KEYS[40]=2;
     _NUMERIC_KEYS[34]=3;
     _NUMERIC_KEYS[37]=4;
     _NUMERIC_KEYS[12]=5;
     _NUMERIC_KEYS[39]=6;
     _NUMERIC_KEYS[36]=7;
     _NUMERIC_KEYS[38]=8;
     _NUMERIC_KEYS[33]=9;
     }

     public function Main() {
      stage.addEventListener(KeyboardEvent.KEY_UP, stageKeyUp);
     }

     private function stageKeyUp(e : KeyboardEvent) : void {
      if(Keyboard.numLock){
       trace("NumLock ON :",String.fromCharCode(e.charCode));
      }else{
       trace("NumLock OFF :", _NUMERIC_KEYS[e.keyCode]);
      }
     }
    }
}
OXMO456
This doesn't help me.. when the NumLock is disabled.. how can you difference between the "UpArrow" and the "8" on the NumPad?And in you're example: How can you difference between the Numeric Keys or the Number keys on the NumPad?.. This definitely doesn't help.
unkiwii
A: 

From what I understand this is not possible due to the differences and limitations between the two different plugin architectures that Flash must support (in order to work in IE(activeX) and everything else). Sorry - it does bite.

Branden Hall
A: 

EDIT:

I see that AutoHotkey CAN do this. So it CAN be done. I was wrong :(.

But again.. It seems that this is impossible inside a Flash Player.


I've run several tests and the one in Assembler throw me this result:

With NumLock activated:

  • Numpad 8 Key: AX=4838
  • Up Arrow: AX=4800

With NumLock deactivated:

  • Numpad 8 Key: AX=4800
  • Up Arrow: AX=4800

PS: I use INT 16H so it returns (in AX) the KeyCode and ScanCode of the key pressed.

unkiwii
AutoHotKey can tell the difference, so it CAN be done.http://www.autohotkey.com/docs/KeyList.htm
Greg
But probably not in Flash
Greg