views:

572

answers:

4

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.

A: 

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.

David Seiler
Thanks Dave, I thought about a constant onMouseMove event, but it's a bit dirty. The thing is I need to show the user that pressing alt will perform a different action than without it. By the time they have pressed the mouse it's too late.I can get get events from shift, so may have to use that. Not ideal :(
robmcm
+1  A: 

I think you're right about Safari. I'm running v 4.0.3 and tried the alt key using a sample from Adobe: http://livedocs.adobe.com/flex/3/html/help.html?content=events%5F11.html, it's working on Firefox but not on Safari!

stef
Yeah it looks like it's a limitation in Safari. Safari does allow JavaScript to detect alt key presses however so I will need to pass those JavaScript events through to Flex. Here is a JavaScript example http://www.javascriptkit.com/javatutors/javascriptkey2.shtml
robmcm
A: 

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;
}
ghalex
Thanks ghalex, but the problem is that Safari doesn't get an event for an alt key press. Interesting to see your use of bitwise operators, look crazy ;)
robmcm
A: 

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: http://stackoverflow.com/questions/683486/javascript-to-actionscript-keypress-passing-utility

robmcm