[AS3]as3.0处理按键事件,多个按键按下时的响应事件,按键盘的事件(2)
packagecom{ importflash.events.KeyboardEvent; importflash.events.Event; importflash.display.DisplayObject; importflash.utils.ByteArray; publicclassKeyPoll{ privatevarstates:ByteArray; privatevardispO
- package com{
- import flash.events.KeyboardEvent;
- import flash.events.Event;
- import flash.display.DisplayObject;
- import flash.utils.ByteArray;
- public class KeyPoll {
- private var states:ByteArray;
- private var dispObj:DisplayObject;
- public function KeyPoll( displayObj:DisplayObject ) {
- states = new ByteArray();
- states.writeUnsignedInt( 0 );
- states.writeUnsignedInt( 0 );
- states.writeUnsignedInt( 0 );
- states.writeUnsignedInt( 0 );
- states.writeUnsignedInt( 0 );
- states.writeUnsignedInt( 0 );
- states.writeUnsignedInt( 0 );
- states.writeUnsignedInt( 0 );
- dispObj=displayObj;
- dispObj.addEventListener( KeyboardEvent.KEY_DOWN, keyDownListener, false, 0, true );
- dispObj.addEventListener( KeyboardEvent.KEY_UP, keyUpListener, false, 0, true );
- dispObj.addEventListener( Event.ACTIVATE, activateListener, false, 0, true );
- dispObj.addEventListener( Event.DEACTIVATE, deactivateListener, false, 0, true );
- }
- private function keyDownListener( ev:KeyboardEvent ):void {
- states[ ev.keyCode >>> 3 ] |= 1 << (ev.keyCode & 7);
- }
- private function keyUpListener( ev:KeyboardEvent ):void {
- states[ ev.keyCode >>> 3 ] &= ~(1 << (ev.keyCode & 7));
- }
- private function activateListener( ev:Event ):void {
- for (var i:int = 0; i < 32; ++i) {
- states[i]=0;
- }
- }
- private function deactivateListener( ev:Event ):void {
- for (var i:int = 0; i < 32; ++i) {
- states[i]=0;
- }
- }
- public function isDown( keyCode:uint ):Boolean {
- return ( states[ keyCode >>> 3 ] & (1 << (keyCode & 7)) ) != 0;
- }
- public function isUp( keyCode:uint ):Boolean {
- return ( states[ keyCode >>> 3 ] & (1 << (keyCode & 7)) ) == 0;
- }
- }
- }
热门文章推荐
- [HLS]做自己的m3u8点播系统使用HTTP Live Streaming(HLS技术)
- [FMS]FMS流媒体服务器配置与使用相关的介绍
- [AS3]什么是M3U8,与HTML5的区别是什么
- AS2.0 让flash自适应全屏,并且不自动缩放
- [AS3]as3.0的sound类常用技巧整理
- [AS3]as3与ByteArray详解、ByteArray介绍、ByteArray用法
- 关于RTMP,RTMPT,RTMPS,RTMPE,RTMPTE协议的介绍
- [JS]分享浏览器弹出窗口不被拦截JS示例
请稍候...