lines.love/keychord.lua
Kartik K. Agaram d441f8a760 some helpers
I spent some time trying to reduce the duplication between identical
operations with and without the shift key pressed. However it makes
things harder to understand. If you try to process selection in
App.keychord_pressed in main.lua, you have to sometimes process the
selection before (e.g. including the state of the cursor _before_ an
arrow key takes effect), and sometimes after (e.g. copying the selection
before resetting it, even though it doesn't include a shift key)

Let's just leave things as they are.
2022-05-29 16:13:24 -07:00

72 lines
1.6 KiB
Lua

-- Keyboard driver
Modifiers = {'lctrl', 'rctrl', 'lalt', 'ralt', 'lshift', 'rshift', 'lgui', 'rgui'}
function App.keypressed(key, scancode, isrepeat)
if array.find(Modifiers, key) then
-- do nothing when the modifier is pressed
end
-- include the modifier(s) when the non-modifer is pressed
App.keychord_pressed(App.combine_modifiers(key))
end
function App.combine_modifiers(key)
local result = ''
local down = love.keyboard.isDown
if down('lctrl') or down('rctrl') then
result = result..'C-'
end
if down('lalt') or down('ralt') then
result = result..'M-'
end
if down('lshift') or down('rshift') then
result = result..'S-' -- don't try to use this with letters/digits
end
if down('lgui') or down('rgui') then
result = result..'s-'
end
result = result..key
return result
end
function App.modifier_down()
return array.any(Modifiers, love.keyboard.isDown)
end
function App.ctrl_down()
return love.keyboard.isDown('lctrl') or love.keyboard.isDown('rctrl')
end
function App.alt_down()
return love.keyboard.isDown('lalt') or love.keyboard.isDown('ralt')
end
function App.shift_down()
return love.keyboard.isDown('lshift') or love.keyboard.isDown('rshift')
end
function App.gui_down()
return love.keyboard.isDown('lgui') or love.keyboard.isDown('rgui')
end
array = {}
function array.find(arr, elem)
for i,x in ipairs(arr) do
if x == elem then
return i
end
end
return nil
end
function array.any(arr, f)
for i,x in ipairs(arr) do
local result = f(x)
if result then
return result
end
end
return false
end