Home Tutorials Forums Articles Blogs Movies Library Employment Press
Old 05-07-2012, 10:00 PM   #1
LooseCannon16
Registered User
 
Join Date: May 2012
Posts: 1
Default Global Key Listener

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
LooseCannon16 is offline   Reply With Quote
Old 05-07-2012, 10:23 PM   #2
abeall
Senior Member
 
Join Date: Feb 2006
Location: Washington, DC
Posts: 2,812
Send a message via AIM to abeall
Default

This is a Flex specific question. The problem is that some Flex controls cancel the key events from bubbling, which is an issue with the Flex framework. In terms of AS3 using the stage is how to capture all key events.
__________________
Aaron Beall | Flash portfolio | Fireworks extensions | Twitter
abeall is offline   Reply With Quote
Reply


Thread Tools
Display Modes Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT. The time now is 04:44 PM.

///
Follow actionscriptorg on Twitter

 


Powered by vBulletin® Version 3.8.5
Copyright ©2000 - 2014, Jelsoft Enterprises Ltd.
Ad Management plugin by RedTyger
Copyright 2000-2013 ActionScript.org. All Rights Reserved.
Your use of this site is subject to our Privacy Policy and Terms of Use.