bugfix: never use utf8 pos in string.sub
[view.love.git] / keychord.lua
blob3b4351971a2f03b2dcf7a809948dc3da57e3011a
1 -- Keyboard driver
3 Modifiers = {'lctrl', 'rctrl', 'lalt', 'ralt', 'lshift', 'rshift', 'lgui', 'rgui'}
5 function App.keypressed(key, scancode, isrepeat)
6 if array.find(Modifiers, key) then
7 -- do nothing when the modifier is pressed
8 return
9 end
10 -- include the modifier(s) when the non-modifer is pressed
11 App.keychord_press(App.combine_modifiers(key), key)
12 end
14 function App.combine_modifiers(key)
15 local result = ''
16 if App.ctrl_down() then
17 result = result..'C-'
18 end
19 if App.alt_down() then
20 result = result..'M-'
21 end
22 if App.shift_down() then
23 result = result..'S-' -- don't try to use this with letters/digits
24 end
25 if App.cmd_down() then
26 result = result..'s-'
27 end
28 result = result..key
29 return result
30 end
32 function App.any_modifier_down()
33 return App.ctrl_down() or App.alt_down() or App.shift_down() or App.cmd_down()
34 end
36 function App.ctrl_down()
37 return App.modifier_down('lctrl') or App.modifier_down('rctrl')
38 end
40 function App.alt_down()
41 return App.modifier_down('lalt') or App.modifier_down('ralt')
42 end
44 function App.shift_down()
45 return App.modifier_down('lshift') or App.modifier_down('rshift')
46 end
48 function App.cmd_down()
49 return App.modifier_down('lgui') or App.modifier_down('rgui')
50 end
52 function App.is_cursor_movement(key)
53 return array.find({'left', 'right', 'up', 'down', 'home', 'end', 'pageup', 'pagedown'}, key)
54 end
56 array = {}
58 function array.find(arr, elem)
59 if type(elem) == 'function' then
60 for i,x in ipairs(arr) do
61 if elem(x) then
62 return i
63 end
64 end
65 else
66 for i,x in ipairs(arr) do
67 if x == elem then
68 return i
69 end
70 end
71 end
72 return nil
73 end
75 function array.any(arr, f)
76 for i,x in ipairs(arr) do
77 local result = f(x)
78 if result then
79 return result
80 end
81 end
82 return false
83 end