1
votes

I am trying to detect the key down event for the alt key in flex. I have a standard event listener for KeyboardEvent.KEY_DOWN and KeyboardEvent.KEY_UP but don't get any response for the alt key (or ctrl key).

I know I can detect if the alt key was pressed via a mouse event, but I want to update the cursor when the alt key is pressed to show it will perform a different action from when it's not pressed.

I am using Safari on a Mac for developing so let me know if perhaps it's an isolated issue.

3

3 Answers

0
votes

I don't have a Flex environment in front of me, but I don't think the modifier keys (Shift, Control, and Option in your case) signal keyboard events. If I'm right, you could still get most of what you want by testing the state of the control and alt keys on each MouseEvent, and updating the cursor state as appropriate.

It might also work to continuously sample the state of the option keys in the background, but I don't remember how to get the keyboard state without an interface event to work from. Best of luck.

0
votes

Try this:

component.addEventListener( KeyboardEvent.KEY_DOWN, keyDownHandler, true );
component.addEventListener( KeyboardEvent.KEY_UP, keyUpHandler, true );

protected function keyDownHandler( event:KeyboardEvent ):void
{
    states[ event.keyCode >>> 3 ] |= 1 << (event.keyCode & 7);
}

protected function keyUpHandler( event:KeyboardEvent ):void
{
    states[ event.keyCode >>> 3 ] &= ~(1 << (event.keyCode & 7));
}

and you can check if a key is down like this:

public function isKeyDown( keyCode:uint ):Boolean
{
    return ( states[ keyCode >>> 3 ] & (1 << (keyCode & 7)) ) != 0;
}
0
votes

As stef pointed out it seems to be a bug in Safari.

This may be rectified using JavaScript to pass the events onto Flash, although I am not sure the JavaScript will get them if the flash movie has the focus.

Here are some examples of how to do this: javascript to actionscript keypress passing utility?