Add special 'mac-' and 'win-' modifiers, fixes #29

This commit is contained in:
airstruck
2016-01-28 11:26:04 -05:00
parent f9a8ffa2e2
commit d1501fca41
5 changed files with 72 additions and 30 deletions

56
luigi/shortcut.lua Normal file
View File

@@ -0,0 +1,56 @@
--[[--
Keyboard shortcut module.
--]]--
local ROOT = (...):gsub('[^.]*$', '')
local Backend = require(ROOT .. 'backend')
local Shortcut = {}
local isMac = Backend.isMac()
local ALT = 1
local CTRL = 2
local SHIFT = 4
local GUI = 8
function Shortcut.parseKeyCombo (value)
-- expand command- and option- aliases
value = value
:gsub('%f[%a]command%-', 'mac-gui-')
:gsub('%f[%a]option%-', 'mac-alt-')
-- exit early if shortcut is for different platform
if isMac and value:match 'win%-' or not isMac and value:match 'mac%-' then
return
end
-- expand c- alias
if isMac then
value = value:gsub('%f[%a]c%-', 'gui-')
else
value = value:gsub('%f[%a]c%-', 'ctrl-')
end
-- extract main key
local mainKey = value:match '[^%-]*%-?$'
-- extract modifiers
local alt = value:match '%f[%a]alt%-' and ALT or 0
local ctrl = value:match '%f[%a]ctrl%-' and CTRL or 0
local shift = value:match '%f[%a]shift%-' and SHIFT or 0
local gui = value:match '%f[%a]gui%-' and GUI or 0
return mainKey, alt + ctrl + shift + gui
end
function Shortcut.getModifierFlags ()
local alt = Backend.isKeyDown('lalt', 'ralt') and ALT or 0
local ctrl = Backend.isKeyDown('lctrl', 'rctrl') and CTRL or 0
local shift = Backend.isKeyDown('lshift', 'rshift') and SHIFT or 0
local gui = Backend.isKeyDown('lgui', 'rgui') and GUI or 0
return alt + ctrl + shift + gui
end
return Shortcut