Handling Keyboard Events

Hi:
Below is an Air application in which I am trying to catch keyboard events.
I do catch it once. Subsequently when I try to catch the events, sometimes I must click on the canvas
to refocus. Sometimes even this does not work. When I choose an option in DropDown list then the handler
also stops working. Is there something missing in my envent Handler?
Please advise and thanks for reading and helping.
<?xml version="1.0" encoding="utf-8"?><mx:WindowedApplication xmlns:mx="http://www.adobe.com/2006/mxml"layout="absolute" windowComplete="windowComplete();"> <mx:ApplicationControlBar id="dockedBar" dock="true" paddingLeft="0" paddingRight="0"> <mx:ComboBox dataProvider="{stats}" id="statsC" change="statsChangeEvt(event)" /></mx:ApplicationControlBar>
<mx:Array id="stats"> <mx:Object label="Option 1"/> <mx:Object label="Option 2"/></mx:Array><mx:Script> <![CDATA[ import mx.controls.Alert; private function windowComplete():void{stage.addEventListener(KeyboardEvent.KEY_DOWN, handleKeyDown);} public function handleKeyDown(event:KeyboardEvent) :void{Alert.show("Key pressed");} private function statsChangeEvt(event:Event):void { Alert.show("Stats Changed")};]]></mx:Script> </mx:WindowedApplication>

Try listening to the keyDown event in the capture phase.
addEventListener(KeyboardEvent.KEY_DOWN, handleKeyDown, true).
ComboBox stops the propogation of the event if it is something that it handles. This is why you don't get it in the bubble phase.
Jason

Similar Messages

  • Handling keyboard events in applets?! Possible?!

    Hello,
    I wrote a little game that uses keyboard handling events. It is an applet that can also standalone as an application. When i run the program as a standalone application, it handles keyboard inputs fine. ie...i can press the left and right arrow keys and something happens.
    But when i run my program as an applet in a web browser, the applet starts up correctly but does not handle any keyboard events. It only handles mouse events. For instance, if i press the up and down arrow keys, the browser window scrolls up and down!!! And the up and down arrow keys have a specific purpose in my applet.
    QUESTION: how do i get my applet to accept keyboard inputs such as UP,DOWN, LEFT, RIGHT?

    Hmm something went wrong w/ my post so i hope this doesnt show up twice.
    Hey thanks a lot for helping me out. The applet/application is large so here is the main() method and the keyboard event handling class code. My question is how come the keyboard events get properly handled when i run it as a standalone application, but keyboard events go to the browser when i run it as an applet? I would post my entire code but it's over 1000 lines and spread out over 8 files. hehehe.
    Here is main():
      public static void main(String[] args)
          MyProgram applet = new MyProgram();
          applet.isStandalone = true;
          JFrame frame = new JFrame();
          frame.setTitle("Physics: Kinematics");
          frame.getContentPane().add(applet, BorderLayout.CENTER);
          applet.init();                        // initialize the applet inside frame
          applet.addKeyListener(kbHandler);     // kbHandler is a keyboard handling object
          applet.start();
          frame.setSize(APPLETWIDTH,APPLETHEIGHT);
          Dimension d = Toolkit.getDefaultToolkit().getScreenSize();
          frame.setLocation((d.width - frame.getSize().width) / 2, (d.height - frame.getSize().height) / 2);
          frame.setVisible(true);
          // private inner class to terminate the Application when frame closes
          frame.addWindowListener(
            new WindowAdapter()
                public void windowClosing(WindowEvent e)
                    System.exit(0);
          frame.show();
       }"kbHandler" is the keyboard event handling object. It gets initialized in the applet.init() method.
    kbHandler = new BMkeyboardHandler();And here is the keyboard event handling code:
    // ===== keyboard event handler ===========
       private class BMkeyboardHandler implements KeyListener
            public void keyPressed(KeyEvent e)
                pushedKey = e.getKeyCode();
                // General administrative key actions
                if(pushedKey == KE.VK_H)
                    actionArea.toggleHelpMenu();
                    actionArea.repaint();
                else if(pushedKey == KE.VK_C)
                    actionArea.toggleCtrlMenu();
                    actionArea.repaint();
         }So there it is. Again, the program handles events fine as a standalone application. But running it as an applet in a browser (and even the appletviewer) doesnt let me handle keystrokes.

  • No keyboard Events on a Window

    Hello Java community,
    I can't understand why, if you build a simple Window (not a Frame), all keyboard Events are simply ignored !! Everything acts as if the Window can't get the focus, because no LOST_ or GOT_ FOCUS events are triggered. This means that you can't handle keyboard events with a Window and therefore a Textfield will not work (however it still receive mouse events ??). BUT it perfectly works with a Frame , which extends from Window! Strange isn't it ?
    I've heard on other forums that , to be eligible for focus a Window must have:
    - its owner Frame (the one given in the constructor) :
    * displayed on screen,
    * containing a focusable component.
    - itself, containing a focusable component.
    thus i respect theses limitations, i still can't get any keyboard events
    pleaze help, i am writing a text area component and without keyboard events ...

    Thanks for the answers BUT,
    I have already read the javadoc in every ways and I am sure I respect perfectly all the conditions to have my WINDOW focusable.
    No, there is another problem, a strange one. In fact, my program works fine under Windows I can type characters into my Textfields but once executed under Linux : no keyboard events! Therefore, no character type allowed. I display in the console, all events that are incomming into my Window, and ALL events are ok (mouse, windows..) except keyboards ones. When I press a key, nothing appears in my console, that's why I think keyboards events are caught and not dispatched to the Window.
    I noticed an interresting fact when using my app in Applet. For example, I launched my application with Mozilla (Netscape 4, JRE 1.1). I displayed an empty Window on screen, when my mouse passes over it or when I click, the Java console showed me these events. When I try to hit some keyboard keys, nothing except for certain keys such as F, G, D, J and numeric keys. In fact, these keys are shortcuts to some of the System class (F calls Finalization, G=Garbage Collector, D=Debug Mode...). I truely think that on a Window, all keyboard events are send to the console. Another fact that proved me that i was right : I tried my app with JRE 1.2, and when I wanted to type characters into my TextField (which is on my Window), the typed characters appeared in the console !!!!
    A JAVA-experimented person told me to redirect the standard input to my own inputStream... but how ?
    Any ideas of solutions ?

  • How to handle key events in popup component

    Is there any possibility to handle keyboard events after a popup is opened
    requirement : I want to close popup when I press escape key (by default this functionality is present but in some scenarios this is not working ) so explicitly i want to handle key events for popup.
    Thanks,
    Raghavendra.

    in my application header part of the page is common of all page. header.jsff contains comandmenuItem named "proceduralhelp". if I click proceduralhelp a pop up opens .
    in my login page when first time I open popup and press escape popup is not closing.
    rest of the cases after login escpe will cose the popup.
    only on the loginpage that to for the first time it is not closing when pressing up on escape key. (then we click on close button. and once again open the popup in loginpage now if we press escape it works).
    Thanks,
    Raghavendra.

  • Getting keyboard events with Group

    Hi all,
    I'm new to JavaFX and I'm trying to handle keyboard events in a group but it doesn't seem to respond when I press the keyboard.
    As I understand this is because Group doesn't receive the focus. If that is the case, how can I make it receive focus? I tried calling setFocusTraversable(true) and setFocused(true) but both haven't helped.
    If it's not the focus, what is it?
    Thanks!

    Use requestFocus() instead of  setFocused(true).
    Keyboards events work as expected  with a group.
    @Override
        public void start(Stage primaryStage) {
            Button btn = new Button();
            btn.setText("Say 'Hello World'");
            Group root = new Group();
            root.addEventFilter(KeyEvent.KEY_PRESSED, new EventHandler<KeyEvent>() {
                public void handle(KeyEvent event) {
                    System.out.println(event.getCode());
                    event.consume();
            root.getChildren().add(btn);
            Scene scene = new Scene(root, 300, 250);
            primaryStage.setTitle("Hello World!");
            primaryStage.setScene(scene);
            primaryStage.show();
            root.requestFocus();

  • ADF Mobile: amx:listView keyboard event handler

    My view needs to accept keyboard input, but does not have an inputText, only a listView. I need to update listItems based on the keyboard input, but cannot add an inputText field to the view. Can I capture keyboard events in the view, the panelPage, or the listView?
    Thanks,
    Mike

    Plzz help :(

  • Can we catch keyboard event triggered by an inputfiled?

    Hi all,
    I have a question here:
    When we are doing the web dynpro abap development, if I want to catch the keyboard triggered by an
    input help and handle it, how can I achieve this?
    e.g.   I have an inputfield on the UI,  if user click "F10" on the keyboard, can I catch this kind of event?
    Thanks and Regards,
    Aaron

    Hi Thomas,
    Yes, I'm trying to acomplish an input help for an inputfield.But, the free programmed value or OVS or dictionary search help can fullfill my needs.Here is the situation:
    I have a inputfield on the UI, and next to that , I have another textview, which should be changed according inputfield. e.g.  the inputfield is a user name, and next to that , is the user id. but we only want the input help for user name. so when user select one row iof the search result table, I want that two fields both filled.
    1. the OVS can't meet my need because I can't change the layout of the search result table, I need a table with a tree in it.
    2. free programmed value help can only send back the value of the inputfield. although maybe I can get the user_id's context attribute name and by hardcoding and send back also the user id. but I don't like hardcoding.
    So, I'm wondering whether I could catch the keyboard event. and handle the F4 help all by myself.
    Do you have some suggestion on this?
    Thanks and Regards,
    Aaron

  • How to handle key events in iphone

    I want to be able to detect physical keyboard (e.g. bluetooth) events in an iOS app.
    In Mac, there is a class NSEvent which handles both keyboard and mouse events, and in ios (iphone/ipad) the counterpart of NSEvent is UIEvent which handles only touch events. I know ios API does not provide this functionality, but how can i handle key events in iphone???

    in my application header part of the page is common of all page. header.jsff contains comandmenuItem named "proceduralhelp". if I click proceduralhelp a pop up opens .
    in my login page when first time I open popup and press escape popup is not closing.
    rest of the cases after login escpe will cose the popup.
    only on the loginpage that to for the first time it is not closing when pressing up on escape key. (then we click on close button. and once again open the popup in loginpage now if we press escape it works).
    Thanks,
    Raghavendra.

  • More explanation about Keyboard Events

    Someone can help me giving me more explanation about how to use Keyboard events inside a textFlow...
    Or maybe show me some example, I try to do something like detect which key is pressed in textFlow.
    I read about: keyDownHandler and manageTabKey, but I still without a clear idea...
    Can you give me a hand please...?

    To filter the keys I'd suggest subclassing ContainerController and overriding keyDownHandler.
    Another approach would be to add your own listeners to the containers and call preventDefault to cancel the event.
    manageTabKey lets you tell TLF to treat a TAB as an insert of a TAB character and to call preventDefault.  Unfortunately that doesn't always prevent the browser from also handling TAB.  Not a problem in an AIR App though.
    Hope that helps,
    rRichard

  • Movieclip.x movement using keyboard event

    Hi, im working on a game project and i have a question about movie clip movement using keyboard event.
    Basicly i have a character on screen and it can move on the x axis using the left and right buttons.
    Im making my charater move by changing the x value of the character movieclip but i find very it laggy and not smooth and if im going point by point
    then it's too slow.
    Whats the best way to make the character move so that the transition will be smooth.

    Ok obviously you've left out where you add your eventListener, i'll assume that's a KEY_DOWN event. When the user holds down a key, that key event is triggered once immediately and then after a pause it is triggered at regular intervals. Check this out by holding down the up/down arrow and watching this browser window scroll.
    That's not the movement you'd be looking for when moving a character in a game. Instead, you'd be setting a variable on KEY_DOWN to indicate the character direction - it could be an integer for example that you increase by 1 if the user pressed RIGHT or decrease by 1 if the user pressed LEFT.
    In addition to your KEY_DOWN eventListener, you could have an ENTER_FRAME and  KEY_UP eventListeners set up:
    The enterFrame event handler could move the character in the direction specified in your direction variable. This is where you might ensure your character doesn't go beyond its limits in both directions.
    In the keyUp event handler, you could do the converse of what you did in the keyDown handler(increase by 1 if the user released LEFT, decrease by 1 if the user released RIGHT).

  • CS2: Listbox and keyboard events

    Hello all,
    I have a listbox widget in a Palette, I need to catch keyboard events specifically I need to be notified when the user presses the RETURN key.
    I checked the PanelTreeView sample which defines a custom event handler but functions like KeyDown or KeyCmd are never called despite "CanHaveKeyFocus" has been modified to return kTrue.
    How can I be notified when user presses the RETURN key in the PanelTreeView listbox?
    Thanks in advance ...
    Jorge Martinez

    If the StageWebView has focus, you should be able to capture only the hardware key events (like BACK, ..). All the others are handled internally by StageWebView.
    Do you see otherwise? If yes, can you post your application ?
    Thanks,
    Raul

  • Keyboard events disbaled in full screen

    Hi,
    I have an application which has a text area in it which has some text provided to it using a text flow.
    Now the problem is, when I make the aplication to full screen, I am no longer able to write anything in the text area. I added a keyboard event listner too, but the handler is not invoked.
    So, please can anyone let me know hoe to deal with this issue ?
    Thanks

    On the web, interaction is prevented for security reasons.  In AIR, you can
    use FULL_SCREEN_INTERACTIVE

  • Keyboard events in jsp  that working in Mozila fire fox Browser

    hai cutes
    have a peace day.
    i did some keyboard event project using JSP
    that can be nicely working in Internet Explorer
    But i want to working in Mozila fire fox Browser and other browser also...
    thank u

    You didn't do it using JSP, keyboard events are client side and will need to be done with javascript. AFAIK the only thing about events that is not cross-browser is the way the event object is handled. This can easily be fixed with a minor workaround.
    <script>
    function handleEvent(e)
    // this is the trick
    if(e == null){
      e = window.event;
    if(e.keyCode == 13) { // enter
    </script>
    <input type="button" onKeyDown="handleEvent(event)">That should make it work in multiple browsers. Well it least it works for me :)

  • Keyboard events going to InDesign and not on textbox

    Hi,
    I have ported a project from InDesign CS2 to InDesgn CS3 on Mac. I have dialogs in rsrc file (not converted to nib file) which I am also using on XCode.
    Now the problem which I am facing is that when typing in a textbox then keyboard events are going to InDesign and only those events which are not handled by InDesign is received by my textbox.
    Has anyone experienced the same problem before?
    Any help will be appreciated.
    Regards,
    Rahul Rastogi

    Start Firefox in [[Safe Mode]] to check if one of your add-ons is causing your problem (switch to the DEFAULT theme: Tools > Add-ons > Themes).<br />
    See [[Troubleshooting extensions and themes]] and [[Troubleshooting plugins]]<br />
    <br />
    If it does work in Safe-mode then disable all your extensions and then try to find which is causing it by enabling one at a time until the problem reappears.<br />
    You can use "Disable all add-ons" on the [[Safe mode]] start window to disable all extensions.<br />
    You have to close and restart Firefox after each change via "File > Exit" (Mac: "Firefox > Quit"; Linux: "File > Quit")

  • Keyboard events in borderless windows.

    I have created a borderless window (class JWindow) in order to handle transparency (non-rectangular window). While the whole system seems to work, the keyboard events are not forwarded anymore to components within the borderless window. For example, there is a list in the window that could be browsed using up/down on the keyboard; it is not possible to use the keyboard to browse it anymore now (although the mouse can hopefully still be used).
    I didn't find anything about that in the bugs database. Do someone has an idea?
    Thanks in advance :-)

    Wow, it looks like a ugly hack! But it seems it works despite of a few problems: when I click, it sometimes has some kind of painting loop that lasts until the mouse leaves the window, and when I press a key, the paint events are sent only when the mouse enters/leaves the windows. Very strange, but at least I have somewhere to look. Thanks :-)

Maybe you are looking for

  • Update termination with BAPI_FIXEDASSET_CHANGE

    Hello, We want to capture the serial number of an asset at the point of goods receipt. There does not appear to be any standard functionality to do this so I am making use of a BAdI in transaction MIGO. But I am encountering a problem with BAPI_FIXED

  • Export pdf with visibility control

    ik have a pdf with layers placed as an object in my CS5.5 document now i want to export pdf's with control over the visability of the layers of that pdf-object i can set the visability but, exporting gives 1 failure how can i solve this

  • How to run servlet in tomcat5

    How to run the servlet in tomcat5... Is it need to set any classpath here. I tried alot in editing web.xml, and also i set the environment variable as JAVA_HOME, CATALINA_HOME to Java1.2 and tomcat5 rsptly. I cant even run the jsp file... it is givin

  • Dimension build error codes

    Does anyone have a list or know where I can find a list? I'm seeing 3307 and 3315 and need to figure out what that is causing them Thanks!

  • Convert Premiere Elements 7 projects to Premiere Pro

    I took advantage of the recent promotion to upgrade to Premiere Pro from Elements 7.  Now I simply want to convert my old files created in Elements to Premiere Pro.  If I simply open the Elements file in Premiere Pro, it says it needs to convert the