I need to create a global key listener for an ActionScript application - it should receive all key events regardless of whether a control on the stage has the focus. The stage is populated by a variety of standard fl.controls as well as a Sprite and an fl.containers.ScrollPane within which custom rendering occurs.
I've tried attaching the listener to the stage and to NativeApplication.nativeApplication (to see if either would resolve the problems that I'm experiencing - neither does.) Both of those listeners appear to receive the same keyboard events and work fine if the stage's focus is set to the stage itself or to NULL, but miss keystrokes in several other situations.
For example, if a Sprite is selected, the global listeners continue to receive all of the keystrokes. That's good. However, if the mx.controls.List control is selected, the global listeners no longer receive ANY keys. Since the List control actually utilizes keystrokes - you can press letters to jump to an entry starting with that character, navigate up and down within the list with the vertical arrow keys, etc. - I thought that I might be on to something.
However, the fl.controls.Slider also acts on keystrokes. When it has the focus, it moves the value bar left and right as the left and right arrow keys are pressed. The global key handlers don't receive those left and right arrow keys, but they do receive vertical arrow key events (which aren't used by the Slider) as well as other keys (such as letters, numbers, etc..) Ok, I thought, if a given control acts upon a particular keystroke, maybe the global keystroke handlers don't receive those events.
Nope. A focused ScrollPane control can intercept both up/down and left/right key events when it has scrollbars attached (due to the image being larger than the display area.) Unlike the Slider, though, the ScrollPane doesn't cause any arrow keystroke events to be missed by the global keystroke handlers.
Further, the fl.controls.TextInput control can have the focus - you can be typing text right into it - and the global keyboard handlers will still receive every single keystroke (which is the behavior that I'm after.)
So to recap...the Sprite, some simple controls (like fl.controls.CheckBox), and some advanced controls (like fl.controls.TextInput) can be selected, retain the focus, and all keyboard events will still be received by a global keystroke handler. The Slider control can have the focus and all keystroke events except for the left/right arrow keys (which it utilizes) can be seen by the global keystroke handler. The ScrollPane can have the focus and - even when it has associated scrollbars that act upon the up/down/left/right arrow keys - all of its arrow keys (as well as everything else) can be seen by the global keystroke handlers. Then, finally, we have things like fl.controls.List and fl.controls.ComboBox which - once they obtain the focus - prevent ANY keys from being received by the global keyboard handler.
I understand that I could simply force the stage.focus back to 'stage' (or null.) That would cause the global listeners to once again start receiving keystrokes, but it would cause a lot of key-based functionality within the controls to cease functioning. For example, the user could no longer select an fl.controls.List control and press a letter to get the selection to jump to an entry starting with that character. Arrow keys (on things like scrollbars for the ScrollPane and fl.controls.Slider) would also stop working if I utilized such a hack. Thus, simply resetting the stage's focus would be a bad solution to this problem.
Can someone shed some light on this behavior? Why do some controls prevent some or all keystrokes from being received by global keystroke handlers, and is there any way to force them to allow those events to be passed on to those global handlers?
- LC
Global Key Listener
Wednesday, May 9, 2012
[Action Script] Global Key Listener May,2012
Posted by Bimo Hery Prabowo at 7:13 AM
Subscribe to:
Post Comments (Atom)
0 comments:
Post a Comment