Key.js 1.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  1. /**
  2. * Key is used by Keyboard, Pointer, etc, to represent a key state.
  3. *
  4. * @class
  5. */
  6. function Key()
  7. {
  8. /**
  9. * Indicates if this key is currently pressed.
  10. */
  11. this.pressed = false;
  12. /**
  13. * Indicates if this key was just pressed.
  14. */
  15. this.justPressed = false;
  16. /**
  17. * Indicates if this key was just released.
  18. */
  19. this.justReleased = false;
  20. }
  21. Key.DOWN = -1;
  22. Key.UP = 1;
  23. Key.RESET = 0;
  24. Key.prototype.constructor = Key;
  25. /**
  26. * Update Key status based on new key state.
  27. */
  28. Key.prototype.update = function(action)
  29. {
  30. this.justPressed = false;
  31. this.justReleased = false;
  32. if(action === Key.DOWN)
  33. {
  34. if(this.pressed === false)
  35. {
  36. this.justPressed = true;
  37. }
  38. this.pressed = true;
  39. }
  40. else if(action === Key.UP)
  41. {
  42. if(this.pressed)
  43. {
  44. this.justReleased = true;
  45. }
  46. this.pressed = false;
  47. }
  48. else if(action === Key.RESET)
  49. {
  50. this.justReleased = false;
  51. this.justPressed = false;
  52. }
  53. };
  54. /**
  55. * Set this key attributes manually.
  56. */
  57. Key.prototype.set = function(justPressed, pressed, justReleased)
  58. {
  59. this.justPressed = justPressed;
  60. this.pressed = pressed;
  61. this.justReleased = justReleased;
  62. };
  63. /**
  64. * Reset key to default values.
  65. */
  66. Key.prototype.reset = function()
  67. {
  68. this.justPressed = false;
  69. this.pressed = false;
  70. this.justReleased = false;
  71. };
  72. export {Key};