keychord.lua 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  1. -- Keyboard driver
  2. Modifiers = {'lctrl', 'rctrl', 'lalt', 'ralt', 'lshift', 'rshift', 'lgui', 'rgui'}
  3. function App.keypressed(key, scancode, isrepeat)
  4. if array.find(Modifiers, key) then
  5. -- do nothing when the modifier is pressed
  6. return
  7. end
  8. -- include the modifier(s) when the non-modifer is pressed
  9. App.keychord_press(App.combine_modifiers(key), key)
  10. end
  11. function App.combine_modifiers(key)
  12. if love.keyboard.isModifierActive then -- waiting for LÖVE v12
  13. if key:match('^kp') then
  14. key = App.translate_numlock(key)
  15. end
  16. end
  17. local result = ''
  18. if App.ctrl_down() then
  19. result = result..'C-'
  20. end
  21. if App.alt_down() then
  22. result = result..'M-'
  23. end
  24. if App.shift_down() then
  25. result = result..'S-' -- don't try to use this with letters/digits
  26. end
  27. if App.cmd_down() then
  28. result = result..'s-'
  29. end
  30. result = result..key
  31. return result
  32. end
  33. function App.any_modifier_down()
  34. return App.ctrl_down() or App.alt_down() or App.shift_down() or App.cmd_down()
  35. end
  36. function App.ctrl_down()
  37. return App.key_down('lctrl') or App.key_down('rctrl')
  38. end
  39. function App.alt_down()
  40. return App.key_down('lalt') or App.key_down('ralt')
  41. end
  42. function App.shift_down()
  43. return App.key_down('lshift') or App.key_down('rshift')
  44. end
  45. function App.cmd_down()
  46. return App.key_down('lgui') or App.key_down('rgui')
  47. end
  48. function App.is_cursor_movement(key)
  49. return array.find({'left', 'right', 'up', 'down', 'home', 'end', 'pageup', 'pagedown'}, key)
  50. end
  51. -- mappings only to non-printable keys; leave out mappings that textinput will handle
  52. Numlock_off = {
  53. kp0='insert',
  54. kp1='end',
  55. kp2='down',
  56. kp3='pagedown',
  57. kp4='left',
  58. -- numpad 5 translates to nothing
  59. kp6='right',
  60. kp7='home',
  61. kp8='up',
  62. kp9='pageup',
  63. ['kp.']='delete',
  64. -- LÖVE handles keypad operators in textinput
  65. -- what's with the `kp=` and `kp,` keys? None of my keyboards have one.
  66. -- Hopefully LÖVE handles them as well in textinput.
  67. kpenter='enter',
  68. kpdel='delete',
  69. }
  70. Numlock_on = {
  71. kpenter='enter',
  72. kpdel='delete',
  73. }
  74. function App.translate_numlock(key)
  75. if love.keyboard.isModifierActive('numlock') then
  76. return Numlock_on[key] or key
  77. else
  78. return Numlock_off[key] or key
  79. end
  80. return key
  81. end
  82. array = {}
  83. function array.find(arr, elem)
  84. if type(elem) == 'function' then
  85. for i,x in ipairs(arr) do
  86. if elem(x) then
  87. return i
  88. end
  89. end
  90. else
  91. for i,x in ipairs(arr) do
  92. if x == elem then
  93. return i
  94. end
  95. end
  96. end
  97. return nil
  98. end
  99. function array.any(arr, f)
  100. for i,x in ipairs(arr) do
  101. local result = f(x)
  102. if result then
  103. return result
  104. end
  105. end
  106. return false
  107. end