4 Replies Latest reply on May 13, 2009 5:23 AM by Barna Biro

    Keyboard "Delete" Key

    thedex1 Level 1

      Hi eveyone, how are all of you? I have a little problem. I am using Keyboard event to call a function i-e, when user press the Delete key, the selected object will be deleted. Now the problem is that when I press only Delete key, nothing happens but if I press "TAB+Delete", it works 100% fine.

      If am using condition "if(event.keyCode == 46)". I want it to work on pressing "Delete" key instead of "Tab+Delete".

       

      If anyone of you has any idea about this issue, please let me know .... I'll be really really thankfull to you.

       

      Thanks ...

       

      ...

      .:.WAHAB

       

        • 1. Re: Keyboard "Delete" Key
          Gregory Lafrance Level 6

          What happens when you run this program and you hit delete?

           

          <?xml version="1.0"?>
          <mx:Application xmlns:mx="http://www.adobe.com/2006/mxml"
            creationComplete="initApp();">
            <mx:Script>
              <![CDATA[
                private function initApp():void {
                  application.addEventListener(KeyboardEvent.KEY_UP, keyHandler);
                }

           

                private function keyHandler(event:KeyboardEvent):void {
                  txt.text = "keyCode: " + event.keyCode + " - charCode: " + event.charCode;
                }
              ]]>
            </mx:Script>
            <mx:TextInput id="myTextInput"/>
            <mx:Text id="txt"/>
          </mx:Application>

          • 2. Re: Keyboard "Delete" Key
            Barna Biro Level 3

            Hi there,

             

            This is quoted from here: http://livedocs.adobe.com/flex/3/html/help.html?content=events_11.html

             

            A common way to handle global key presses is to create a listener for the KeyboardEvent.KEY_DOWN or KeyboardEvent.KEY_UP event on the application. Listeners on the application container are triggered every time a key is pressed, regardless of where the focus is (as long as the focus is in the application on not in the browser controls or outside of the browser). Inside the handler, you can examine the key code or the character code using the charCode and keyCode properties of the KeyboardEvent class.

             

             

            It works when you press TAB because TAB essentially gets a child from the display list and focuses on it. Once your application/object has focus ( because you pressed TAB ) the keyboard listener can react. If your application is out of focus then it won't react to any keyboard events.

             

            In case your object is selected and your application has focus and it's still not reacting then you could try using event.preventDefault(); to prevent default behavior; if that's not helping either then you need to recheck your code because the problem is in your code somewhere.

             

            With best regards,

            Barna Biro

            Blog: http://blog.wisebisoft.com

            • 3. Re: Keyboard "Delete" Key
              thedex1 Level 1

              It says, "Keycode 46 and Char Code 127".

              I think it is a problem with javascript code that embed the flex application.

               

              Thanks...

              • 4. Re: Keyboard "Delete" Key
                Barna Biro Level 3

                You could easily use event.charCode == Keyboard.DELETE instead of specifying the exact charcode manually. It's not a problem if you don't use Keyboard.DELETE, but it's safer this way because you can stay assured and calm that you are always comparing the received charCode to a correct delete charcode.