3

How do I determine if the delete key was pressed using actionscript?

addEventListener(KeyboardEvent.KEY_UP, onKeyUp);

...

function onKeyUp(event:KeyboardEvent):void
{
    trace(event.keyCode);
}

The above code yields no value when delete, backspace, enter, and other command keys are pressed. However, arrow keys do yield values.

Soviut
  • 88,194
  • 49
  • 192
  • 260

4 Answers4

8
this.stage.addEventListener(KeyboardEvent.KEY_DOWN, onKeyPressed);
....

function onKeyPressed(event:KeyboardEvent):void
{
   if (event.keyCode==Keyboard.DELETE) {
       .....
       }

}

it's workin nice... But if you test movie from Flash, it will not work, so export to swf and test....

Armen Mkrtchyan
  • 921
  • 1
  • 13
  • 34
  • Interesting. I guess because the simulator is running inside the FLash IDE, the IDE itself is intercepting delete commands. – Soviut Dec 11 '09 at 22:00
2

Just guessing you are using the TEXT_INPUT event, this doesn't work for delete and backspace. To catch those ones you can add an eventListener on the stage and listen to a KeyboardEvent.

Theo.T
  • 8,905
  • 3
  • 24
  • 38
  • No, I'm listening for events on the stage. Code snippet updated to reflect this. – Soviut Oct 09 '09 at 01:04
  • You must write stage.addEventListener(KeyboardEvent.KEY_DOWN, eventKeyDown) though (stage.* !) even though you are in the scope of the Document Class. Works for me... – Theo.T Oct 09 '09 at 01:10
1

Code will work fine if the display object that you attached the listener is in focus. For global listening, as Theo said, you have to attach the listener to the stage. Accessing stage from an object that's not yet added to the display list will result in null error. Do it in the ADDED_TO_STAGE event handler to be safe.

Amarghosh
  • 58,710
  • 11
  • 92
  • 121
1

Old thread, but if anyone gets this far: in the Flash Player inside the IDE, these keys are associated with shortcuts. When you test your movie, choose Control>disable keyboard shortcuts in the player and you'll get the events back.

jjbilly
  • 11
  • 1