handle chords

For shift we'll mostly rely on love.textinput. For the rest I've created
a simple driver.
This commit is contained in:
Kartik K. Agaram 2022-05-02 08:20:30 -07:00
parent 57d87f2353
commit 4aa2003c94
2 changed files with 46 additions and 26 deletions

25
keychord.lua Normal file
View File

@ -0,0 +1,25 @@
-- Keyboard driver
function love.keypressed(key, scancode, isrepeat)
if key == 'lctrl' or key == 'rctrl' or key == 'lalt' or key == 'ralt' or key == 'lshift' or key == 'rshift' or key == 'lgui' or key == 'rgui' then
-- do nothing when the modifier is pressed
end
-- include the modifier(s) when the non-modifer is pressed
keychord_pressed(combine_modifiers(key))
end
function 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('lgui') or down('rgui') then
result = result..'S-'
end
result = result..key
return result
end

View File

@ -1,3 +1,5 @@
require 'keychord'
lines = {} lines = {}
width, height, flags = 0, 0, nil width, height, flags = 0, 0, nil
@ -25,27 +27,29 @@ end
function love.update(dt) function love.update(dt)
end end
function love.keypressed(key, scancode, isrepeat) function love.textinput(t)
if key == 'return' then lines[#lines] = lines[#lines]..t
end
function keychord_pressed(chord)
-- Don't handle any keys here that would trigger love.textinput above.
if chord == 'return' then
table.insert(lines, '') table.insert(lines, '')
elseif key == 'space' then elseif chord == 'C-r' then
lines[#lines] = lines[#lines]..' ' lines[#lines+1] = eval(lines[#lines])[1]
elseif key == 'lctrl' or key == 'rctrl' then lines[#lines+1] = ''
-- do nothing
elseif key == 'lalt' or key == 'ralt' then
-- do nothing
elseif key == 'lshift' or key == 'rshift' then
-- do nothing
elseif love.keyboard.isDown('lctrl') or love.keyboard.isDown('rctrl') then
if key == 'r' then
lines[#lines+1] = eval(lines[#lines])[1]
lines[#lines+1] = ''
end
else
lines[#lines] = lines[#lines]..key
end end
end end
function love.keyreleased(key, scancode)
end
function love.mousepressed(x, y, button)
end
function love.mousereleased(x, y, button)
end
function eval(buf) function eval(buf)
local f = load('return '..buf, 'REPL') local f = load('return '..buf, 'REPL')
if f then if f then
@ -69,12 +73,3 @@ function gather_results(success, ...)
local n = select('#', ...) local n = select('#', ...)
return success, { n = n, ... } return success, { n = n, ... }
end end
function love.keyreleased(key, scancode)
end
function love.mousepressed(x, y, button)
end
function love.mousereleased(x, y, button)
end