/src/cocktail/keyboard/as3/Keyboard.hx

http://github.com/silexlabs/Cocktail · Haxe · 88 lines · 36 code · 10 blank · 42 comment · 0 complexity · 51e08353c3e1a4fe01cd933b68bc44d1 MD5 · raw file

  1. /*This file is part of Silex - see http://projects.silexlabs.org/?/silex
  2. Silex is Š 2010-2011 Silex Labs and is released under the GPL License:
  3. This program is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License (GPL) as published by the Free Software Foundation; either version 2 of the License, or (at your option) any later version.
  4. This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.
  5. To read the license please visit http://www.gnu.org/copyleft/gpl.html
  6. */
  7. package cocktail.keyboard.as3;
  8. import flash.events.KeyboardEvent;
  9. import flash.Lib;
  10. import haxe.Log;
  11. import cocktail.keyboard.abstract.AbstractKeyboard;
  12. import cocktail.keyboard.KeyboardData;
  13. /**
  14. * This is the flash AVM2 implementation of the keyboard abstraction.
  15. * Set listeners on native flash keyboard event and call the corresponding
  16. * callback
  17. *
  18. * @author Yannick DOMINGUEZ
  19. */
  20. class Keyboard extends AbstractKeyboard
  21. {
  22. /**
  23. * class constructor. Set native keyboard listeners
  24. */
  25. public function new()
  26. {
  27. super();
  28. }
  29. //////////////////////////////////////////////////////////////////////////////////////////
  30. // EVENTS
  31. // overriden private native keyboard event handler method
  32. //////////////////////////////////////////////////////////////////////////////////////////
  33. /**
  34. * Set the listeners for flash keyboard event
  35. */
  36. override private function setNativeKeyboardListeners():Void
  37. {
  38. //in flash keyboard event are listened from the stage to receive global
  39. //keyboard event. We might evolve this features with focus management
  40. //eventually
  41. Lib.current.stage.addEventListener(KeyboardEvent.KEY_DOWN, onNativeKeyDown);
  42. Lib.current.stage.addEventListener(KeyboardEvent.KEY_UP, onNativeKeyUp);
  43. }
  44. /**
  45. * removes the listeners for flash keyboard event
  46. */
  47. override private function unsetNativeKeyboardListeners():Void
  48. {
  49. Lib.current.stage.removeEventListener(KeyboardEvent.KEY_DOWN, onNativeKeyDown);
  50. Lib.current.stage.removeEventListener(KeyboardEvent.KEY_UP, onNativeKeyUp);
  51. }
  52. //////////////////////////////////////////////////////////////////////////////////////////
  53. // Overriden private key utils methods
  54. //////////////////////////////////////////////////////////////////////////////////////////
  55. /**
  56. * Returns the key that triggered the keyboard event
  57. * @param event the native key up or down event
  58. * @return a sruct containing the key code and other key values
  59. */
  60. override private function getKeyData(event:Dynamic):Key
  61. {
  62. //cast the flash KeyboardEvent
  63. var typedEvent:KeyboardEvent = cast(event);
  64. var key:Key = {
  65. value : getKeyValue(typedEvent.keyCode),
  66. code : typedEvent.keyCode,
  67. ascii : typedEvent.charCode,
  68. altKey : typedEvent.altKey ,
  69. controlKey : typedEvent.ctrlKey,
  70. shiftKey : typedEvent.shiftKey
  71. }
  72. return key;
  73. }
  74. }