Keyboard.lua 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  1. --[[
  2. MIT License
  3. Copyright (c) 2019 Mitchell Davis <coding.jackalope@gmail.com>
  4. Permission is hereby granted, free of charge, to any person obtaining a copy
  5. of this software and associated documentation files (the "Software"), to deal
  6. in the Software without restriction, including without limitation the rights
  7. to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  8. copies of the Software, and to permit persons to whom the Software is
  9. furnished to do so, subject to the following conditions:
  10. The above copyright notice and this permission notice shall be included in all
  11. copies or substantial portions of the Software.
  12. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  13. IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  14. FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  15. AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  16. LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  17. OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
  18. SOFTWARE.
  19. --]]
  20. local Keyboard = {}
  21. local State =
  22. {
  23. Pressed = {},
  24. WasPressed = {},
  25. LastPressed = nil,
  26. PressedTime = 0.0,
  27. ShouldRepeat = false
  28. }
  29. local RepeatDelay = 0.6
  30. local RepeatTime = 0.075
  31. local function InsertKey(Key)
  32. if State.Pressed[Key] == nil then
  33. State.Pressed[Key] = love.keyboard.isDown(Key)
  34. State.WasPressed[Key] = false
  35. end
  36. end
  37. function Keyboard.Update()
  38. for K, V in pairs(State.Pressed) do
  39. State.WasPressed[K] = State.Pressed[K]
  40. State.Pressed[K] = love.keyboard.isDown(K)
  41. if Keyboard.IsPressed(K) then
  42. State.LastPressed = K
  43. State.PressedTime = love.timer.getTime()
  44. end
  45. end
  46. if State.LastPressed ~= nil then
  47. if not Keyboard.IsDown(State.LastPressed) then
  48. State.LastPressed = nil
  49. State.ShouldRepeat = false
  50. end
  51. local Elapsed = love.timer.getTime() - State.PressedTime
  52. local Reset = false
  53. if not State.ShouldRepeat then
  54. if Elapsed >= RepeatDelay then
  55. Reset = true
  56. State.ShouldRepeat = true
  57. end
  58. else
  59. if Elapsed >= RepeatTime then
  60. Reset = true
  61. end
  62. end
  63. if Reset then
  64. State.PressedTime = love.timer.getTime()
  65. State.Pressed[State.LastPressed] = false
  66. State.WasPressed[State.LastPressed] = false
  67. end
  68. end
  69. end
  70. function Keyboard.IsPressed(Key, CancelRepeat)
  71. InsertKey(Key)
  72. if Key == State.LastPressed and CancelRepeat then
  73. State.LastPressed = nil
  74. end
  75. return State.Pressed[Key] and not State.WasPressed[Key]
  76. end
  77. function Keyboard.IsReleased(Key)
  78. InsertKey(Key)
  79. return not State.Pressed[Key] and State.WasPressed[Key]
  80. end
  81. function Keyboard.IsDown(Key)
  82. InsertKey(Key)
  83. return State.Pressed[Key] or love.keyboard.isDown(Key)
  84. end
  85. function Keyboard.Keys()
  86. local Result = {}
  87. for K, V in pairs(State.Pressed) do
  88. table.insert(Result, K)
  89. end
  90. return Result
  91. end
  92. return Keyboard