lines2.love/keychord.lua

90 lines
2.1 KiB
Lua

Modifiers = {'lctrl', 'rctrl', 'lalt', 'ralt', 'lshift', 'rshift', 'lgui', 'rgui'}
function love.keypressed(key, scancode, isrepeat)
if array.find(Modifiers, key) then
-- do nothing when the modifier is pressed
return
end
-- include the modifier(s) when the non-modifer is pressed
love.keychord_pressed(combine_modifiers(key), key)
end
function combine_modifiers(key)
if love.keyboard.isModifierActive then -- waiting for LÖVE v12
if key:match('^kp') then
key = translate_numlock(key)
end
end
local result = ''
if ctrl_down() then
result = result..'C-'
end
if alt_down() then
result = result..'M-'
end
if shift_down() then
result = result..'S-' -- don't try to use this with letters/digits
end
if cmd_down() then
result = result..'s-'
end
result = result..key
return result
end
function any_modifier_down()
return ctrl_down() or alt_down() or shift_down() or cmd_down()
end
function ctrl_down()
return Keys_down.lctrl or Keys_down.rctrl
end
function alt_down()
return Keys_down.lalt or Keys_down.ralt
end
function shift_down()
return Keys_down.lshift or Keys_down.rshift
end
function cmd_down()
return Keys_down.lgui or Keys_down.rgui
end
function is_cursor_movement(key)
return array.find({'left', 'right', 'up', 'down', 'home', 'end', 'pageup', 'pagedown'}, key)
end
-- mappings only to non-printable keys; leave out mappings that textinput will handle
Numlock_off = {
kp0='insert',
kp1='end',
kp2='down',
kp3='pagedown',
kp4='left',
-- numpad 5 translates to nothing
kp6='right',
kp7='home',
kp8='up',
kp9='pageup',
['kp.']='delete',
-- LÖVE handles keypad operators in textinput
-- what's with the `kp=` and `kp,` keys? None of my keyboards have one.
-- Hopefully LÖVE handles them as well in textinput.
kpenter='enter',
kpdel='delete',
}
Numlock_on = {
kpenter='enter',
kpdel='delete',
}
function translate_numlock(key)
if love.keyboard.isModifierActive('numlock') then
return Numlock_on[key] or key
else
return Numlock_off[key] or key
end
return key
end