只有在AS3检测第一键按下事件按下、一键、事件

2023-09-08 14:12:43 作者:你是人间最后一抹解药

的KeyboardEvent 对象我得到键按下事件,有没有办法知道,如果该事件是第一个关键下来,或重演的关键按住? 我想切换设置时的一个关键是pressed;我可以听钥匙了,但我想preFER的动作只要键被按下取...只要我能阻止它来回切换为重点,下生成重复的事件。

In the KeyboardEvent object I get on key-down events, is there a way to know if the event is the first key-down, or a repeat when the key is held down? I want to toggle a setting when a key is pressed; I could listen to key-up but I'd prefer the action is taken as soon as the key is pushed... as long as I can stop it toggling back and forth as the key-down repeat events are generated.

推荐答案

测试和工程

import flash.events.KeyboardEvent;

stage.addEventListener(KeyboardEvent.KEY_DOWN, onDown);
stage.addEventListener(KeyboardEvent.KEY_UP, onUp);

var keys:Object = {};

function onDown(e:KeyboardEvent):void {
    if( ! Boolean(e.keyCode in keys)) {
        trace("First Time");        
        keys[e.keyCode] = true;
    }       
}

function onUp(e:KeyboardEvent):void {
    delete keys[e.keyCode];
}

在对象存储键codeS的概念和使用删除键上的最初来自Senoculars库。请参阅http://www.senocular.com/flash/actionscript/

The idea of storing key codes in an object and the use of delete on key up originally came from Senoculars library. See http://www.senocular.com/flash/actionscript/