/src/uk/co/bigroom/input/KeyPoll.as

http://bigroom.googlecode.com/ · ActionScript · 129 lines · 59 code · 9 blank · 61 comment · 4 complexity · c1cee3865ae60487e829b660d294ff39 MD5 · raw file

  1. /*
  2. * Author: Richard Lord
  3. * Copyright (c) Big Room Ventures Ltd. 2007-2008
  4. * Version: 1.0.3
  5. *
  6. * Licence Agreement
  7. *
  8. * Permission is hereby granted, free of charge, to any person obtaining a copy
  9. * of this software and associated documentation files (the "Software"), to deal
  10. * in the Software without restriction, including without limitation the rights
  11. * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  12. * copies of the Software, and to permit persons to whom the Software is
  13. * furnished to do so, subject to the following conditions:
  14. *
  15. * The above copyright notice and this permission notice shall be included in
  16. * all copies or substantial portions of the Software.
  17. *
  18. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  19. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  20. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  21. * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  22. * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  23. * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  24. * THE SOFTWARE.
  25. */
  26. package uk.co.bigroom.input
  27. {
  28. import flash.events.KeyboardEvent;
  29. import flash.events.Event;
  30. import flash.display.DisplayObject;
  31. import flash.utils.ByteArray;
  32. /**
  33. * <p>Games often need to get the current state of various keys in order to respond to user input.
  34. * This is not the same as responding to key down and key up events, but is rather a case of discovering
  35. * if a particular key is currently pressed.</p>
  36. *
  37. * <p>In Actionscript 2 this was a simple matter of calling Key.isDown() with the appropriate key code.
  38. * But in Actionscript 3 Key.isDown no longer exists and the only intrinsic way to react to the keyboard
  39. * is via the keyUp and keyDown events.</p>
  40. *
  41. * <p>The KeyPoll class rectifies this. It has isDown and isUp methods, each taking a key code as a
  42. * parameter and returning a Boolean.</p>
  43. */
  44. public class KeyPoll
  45. {
  46. private var states:ByteArray;
  47. private var dispObj:DisplayObject;
  48. /**
  49. * Constructor
  50. *
  51. * @param stage A display object on which to listen for keyboard events.
  52. * To catch all key events, this should be a reference to the stage.
  53. */
  54. public function KeyPoll( stage:DisplayObject )
  55. {
  56. states = new ByteArray();
  57. states.writeUnsignedInt( 0 );
  58. states.writeUnsignedInt( 0 );
  59. states.writeUnsignedInt( 0 );
  60. states.writeUnsignedInt( 0 );
  61. states.writeUnsignedInt( 0 );
  62. states.writeUnsignedInt( 0 );
  63. states.writeUnsignedInt( 0 );
  64. states.writeUnsignedInt( 0 );
  65. dispObj = stage;
  66. dispObj.addEventListener( KeyboardEvent.KEY_DOWN, keyDownListener, false, 0, true );
  67. dispObj.addEventListener( KeyboardEvent.KEY_UP, keyUpListener, false, 0, true );
  68. dispObj.addEventListener( Event.ACTIVATE, activateListener, false, 0, true );
  69. dispObj.addEventListener( Event.DEACTIVATE, deactivateListener, false, 0, true );
  70. }
  71. private function keyDownListener( ev:KeyboardEvent ):void
  72. {
  73. states[ ev.keyCode >>> 3 ] |= 1 << (ev.keyCode & 7);
  74. }
  75. private function keyUpListener( ev:KeyboardEvent ):void
  76. {
  77. states[ ev.keyCode >>> 3 ] &= ~(1 << (ev.keyCode & 7));
  78. }
  79. private function activateListener( ev:Event ):void
  80. {
  81. for ( var i:int = 0; i < 32; ++i )
  82. {
  83. states[ i ] = 0;
  84. }
  85. }
  86. private function deactivateListener( ev:Event ):void
  87. {
  88. for ( var i:int = 0; i < 32; ++i )
  89. {
  90. states[ i ] = 0;
  91. }
  92. }
  93. /**
  94. * To test whether a key is down.
  95. *
  96. * @param keyCode code for the key to test.
  97. *
  98. * @return true if the key is down, false otherwise.
  99. *
  100. * @see #isUp()
  101. */
  102. public function isDown( keyCode:uint ):Boolean
  103. {
  104. return ( states[ keyCode >>> 3 ] & (1 << (keyCode & 7)) ) != 0;
  105. }
  106. /**
  107. * To test whether a key is up.
  108. *
  109. * @param keyCode code for the key to test.
  110. *
  111. * @return true if the key is up, false otherwise.
  112. *
  113. * @see #isDown()
  114. */
  115. public function isUp( keyCode:uint ):Boolean
  116. {
  117. return ( states[ keyCode >>> 3 ] & (1 << (keyCode & 7)) ) == 0;
  118. }
  119. }
  120. }