只有在AS3检测第一键按下事件 [英] Detect first key-down event only in AS3

查看:127
本文介绍了只有在AS3检测第一键按下事件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

的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库。请参阅<一href="http://www.senocular.com/flash/actionscript/">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/

这篇关于只有在AS3检测第一键按下事件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆