How to detect if the delete key was pressed in Actionscript 3?

强颜欢笑 提交于 2019-12-23 19:20:21

问题


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.


回答1:


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....




回答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.




回答3:


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.




回答4:


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.



来源:https://stackoverflow.com/questions/1541276/how-to-detect-if-the-delete-key-was-pressed-in-actionscript-3

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!