Key.js 1.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  1. "use strict";
  2. /**
  3. * Key is used by Keyboard, Mouse, etc, to represent a key state.
  4. *
  5. * @class Key
  6. * @module Input
  7. */
  8. function Key()
  9. {
  10. /**
  11. * Indicates if this key is currently pressed.
  12. * @property pressed
  13. * @default false
  14. * @type {boolean}
  15. */
  16. this.pressed = false;
  17. /**
  18. * Indicates if this key was just pressed.
  19. * @property justPressed
  20. * @default false
  21. * @type {boolean}
  22. */
  23. this.justPressed = false;
  24. /**
  25. * Indicates if this key was just released.
  26. * @property justReleased
  27. * @default false
  28. * @type {boolean}
  29. */
  30. this.justReleased = false;
  31. }
  32. /**
  33. * Down
  34. * @attribute DOWN
  35. * @type {Number}
  36. */
  37. Key.DOWN = -1;
  38. /**
  39. * Up
  40. * @attribute UP
  41. * @type {Number}
  42. */
  43. Key.UP = 1;
  44. /**
  45. * Reset
  46. * @attribute RESET
  47. * @type {Number}
  48. */
  49. Key.RESET = 0;
  50. Key.prototype.constructor = Key;
  51. /**
  52. * Update Key status based on new key state.
  53. *
  54. * @method update
  55. */
  56. Key.prototype.update = function(action)
  57. {
  58. this.justPressed = false;
  59. this.justReleased = false;
  60. if(action === Key.DOWN)
  61. {
  62. if(this.pressed === false)
  63. {
  64. this.justPressed = true;
  65. }
  66. this.pressed = true;
  67. }
  68. else if(action === Key.UP)
  69. {
  70. if(this.pressed)
  71. {
  72. this.justReleased = true;
  73. }
  74. this.pressed = false;
  75. }
  76. else if(action === Key.RESET)
  77. {
  78. this.justReleased = false;
  79. this.justPressed = false;
  80. }
  81. };
  82. /**
  83. * Set this key attributes manually.
  84. *
  85. * @method set
  86. */
  87. Key.prototype.set = function(justPressed, pressed, justReleased)
  88. {
  89. this.justPressed = justPressed;
  90. this.pressed = pressed;
  91. this.justReleased = justReleased;
  92. };
  93. /**
  94. * Reset key to default values.
  95. *
  96. * @method reset
  97. */
  98. Key.prototype.reset = function()
  99. {
  100. this.justPressed = false;
  101. this.pressed = false;
  102. this.justReleased = false;
  103. };