Keyboard.lua 2.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  1. local events = require("Engine.events")
  2. return function(config) --A function that creates a new Keyboard peripheral.
  3. if config._SpaceWalkthrough then
  4. events:register("love:keypressed",function(key,sc,isrepeat)
  5. if key == "space" then
  6. events:trigger("love:textinput"," ")
  7. end
  8. end)
  9. end
  10. if config._Android then
  11. events:register("love:textinput",function(t)
  12. events:trigger("love:keypressed",string.lower(t),string.lower(t))
  13. events:trigger("love:keyreleased",string.lower(t),string.lower(t))
  14. end)
  15. end
  16. if config.CPUKit then --Register Keyboard events
  17. local cpukit = config.CPUKit
  18. events:register("love:keypressed", function(...)
  19. cpukit.triggerEvent("keypressed",...)
  20. end)
  21. events:register("love:keyreleased", function(...)
  22. cpukit.triggerEvent("keyreleased",...)
  23. end)
  24. local gpukit = config.GPUKit
  25. --The hook the textinput for feltering characters not in the font
  26. events:register("love:textinput",function(text)
  27. local text_escaped = text:gsub("[%(%)%.%%%+%-%*%?%[%]%^%$]", "%%%1")
  28. if #text == 1 and ((not gpukit) or gpukit._FontChars:find(text_escaped)) then
  29. cpukit.triggerEvent("textinput",text)
  30. end
  31. end)
  32. end
  33. --The api starts here--
  34. local KB = {}
  35. function KB.textinput(state)
  36. if type(state) ~= "nil" then
  37. love.keyboard.setTextInput(state or config._EXKB)
  38. else
  39. return love.keyboard.getTextInput()
  40. end
  41. end
  42. function KB.keyrepeat(state)
  43. if type(state) ~= "nil" then
  44. love.keyboard.setKeyRepeat(state)
  45. else
  46. return love.keyboard.getKeyRepeat()
  47. end
  48. end
  49. function KB.keytoscancode(key)
  50. if type(key) ~= "string" then return false, "Key must be a string, provided: "..type(key) end --Error
  51. local ok, err = pcall(love.keyboard.getScancodeFromKey, key)
  52. if ok then
  53. return err
  54. else
  55. return error(err)
  56. end
  57. end
  58. function KB.scancodetokey(scancode)
  59. if type(scancode) ~= "string" then return false, "Scancode must be a string, provided: "..type(scancode) end --Error
  60. local ok, err = pcall(love.keyboard.getKeyFromScancode, key)
  61. if ok then
  62. return err
  63. else
  64. return error(err)
  65. end
  66. end
  67. function KB.isKDown(...)
  68. return love.keyboard.isDown(...)
  69. end
  70. return KB
  71. end