parent
c4b2dd18d6
commit
060d8740f5
Binary file not shown.
@ -1,18 +1,23 @@
|
|||||||
function string:split(sep)
|
function string.split(s, delim)
|
||||||
local t = { }
|
local start = 1
|
||||||
local function helper(word)
|
local results = {}
|
||||||
table.insert(t, word)
|
while true do
|
||||||
return ""
|
local pos = string.find(s, delim, start, true)
|
||||||
end
|
if not pos then
|
||||||
if not self:gsub("%w+", helper):find("%S") then
|
break
|
||||||
return t
|
end
|
||||||
|
table.insert(results, string.sub(s, start, pos-1))
|
||||||
|
start = pos + string.len(delim)
|
||||||
end
|
end
|
||||||
|
table.insert(results, string.sub(s, start))
|
||||||
|
table.removevalue(results, '')
|
||||||
|
return results
|
||||||
end
|
end
|
||||||
|
|
||||||
function string:starts(start)
|
function string.starts(s, start)
|
||||||
return self:sub(1, #start) == start
|
return string.sub(s, 1, #start) == start
|
||||||
end
|
end
|
||||||
|
|
||||||
function string:trim()
|
function string.trim(s)
|
||||||
return self:match('^%s*(.*%S)') or ''
|
return string.match(s, '^%s*(.*%S)') or ''
|
||||||
end
|
end
|
||||||
|
@ -1,6 +1,96 @@
|
|||||||
Hotkeys = {}
|
Hotkeys = {}
|
||||||
|
|
||||||
function Hotkeys.bindKey(keyDesc, func)
|
-- private functions
|
||||||
|
local function translateKeyCombo(keyCombo)
|
||||||
|
if not keyCombo or #keyCombo == 0 then return nil end
|
||||||
|
table.sort(keyCombo)
|
||||||
|
local keyComboDesc = ''
|
||||||
|
for k,v in pairs(keyCombo) do
|
||||||
|
local keyDesc = KeyCodeDescs[v]
|
||||||
|
if keyDesc == nil then return nil end
|
||||||
|
keyComboDesc = keyComboDesc .. '+' .. keyDesc
|
||||||
|
end
|
||||||
|
keyComboDesc = keyComboDesc:sub(2)
|
||||||
|
return keyComboDesc
|
||||||
|
end
|
||||||
|
|
||||||
|
local function retranslateKeyComboDesc(keyComboDesc)
|
||||||
|
if keyComboDesc == nil then return nil end
|
||||||
|
local keyCombo = {}
|
||||||
|
for i,currentKeyDesc in ipairs(keyComboDesc:split('+')) do
|
||||||
|
for keyCode, keyDesc in pairs(KeyCodeDescs) do
|
||||||
|
if keyDesc:lower() == currentKeyDesc:trim():lower() then
|
||||||
|
table.insert(keyCombo, keyCode)
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
||||||
|
return translateKeyCombo(keyCombo)
|
||||||
|
end
|
||||||
|
|
||||||
|
local function determineKeyComboDesc(keyCode, keyboardModifiers)
|
||||||
|
local keyCombo = {}
|
||||||
|
if keyCode == KeyShift or keyCode == KeyAlt or keyCode == KeyAltGr then
|
||||||
|
table.insert(keyCombo, keyCode)
|
||||||
|
elseif KeyCodeDescs[keyCode] ~= nil then
|
||||||
|
if keyboardModifiers == KeyboardCtrlModifier then
|
||||||
|
table.insert(keyCombo, KeyCtrl)
|
||||||
|
elseif keyboardModifiers == KeyboardAltModifier then
|
||||||
|
table.insert(keyCombo, KeyAlt)
|
||||||
|
elseif keyboardModifiers == KeyboardCtrlAltModifier then
|
||||||
|
table.insert(keyCombo, KeyCtrl)
|
||||||
|
table.insert(keyCombo, KeyAlt)
|
||||||
|
elseif keyboardModifiers == KeyboardShiftModifier then
|
||||||
|
table.insert(keyCombo, KeyShift)
|
||||||
|
elseif keyboardModifiers == KeyboardCtrlShiftModifier then
|
||||||
|
table.insert(keyCombo, KeyCtrl)
|
||||||
|
table.insert(keyCombo, KeyShift)
|
||||||
|
elseif keyboardModifiers == KeyboardAltShiftModifier then
|
||||||
|
table.insert(keyCombo, KeyAlt)
|
||||||
|
table.insert(keyCombo, KeyShift)
|
||||||
|
elseif keyboardModifiers == KeyboardCtrlAltShiftModifier then
|
||||||
|
table.insert(keyCombo, KeyCtrl)
|
||||||
|
table.insert(keyCombo, KeyAlt)
|
||||||
|
table.insert(keyCombo, KeyShift)
|
||||||
|
end
|
||||||
|
table.insert(keyCombo, keyCode)
|
||||||
|
end
|
||||||
|
table.sort(keyCombo)
|
||||||
|
return translateKeyCombo(keyCombo)
|
||||||
|
end
|
||||||
|
|
||||||
|
local function onWidgetKeyPress(widget, keyCode, keyText, keyboardModifiers)
|
||||||
|
local keyComboDesc = determineKeyComboDesc(keyCode, keyboardModifiers)
|
||||||
|
local callback = widget.boundKeyCombos[keyComboDesc]
|
||||||
|
if callback then
|
||||||
|
callback()
|
||||||
|
return true
|
||||||
|
end
|
||||||
|
return false
|
||||||
end
|
end
|
||||||
|
|
||||||
|
local function connectWidgetHotkeyEvent(widget)
|
||||||
|
if widget.boundKeyCombos then return end
|
||||||
|
connect(widget, { onKeyPress = onWidgetKeyPress })
|
||||||
|
widget.boundKeyCombos = {}
|
||||||
|
end
|
||||||
|
|
||||||
|
-- public functions
|
||||||
|
function Hotkeys.bind(keyComboDesc, callback, widget)
|
||||||
|
widget = widget or rootWidget
|
||||||
|
connectWidgetHotkeyEvent(widget)
|
||||||
|
local keyComboDesc = retranslateKeyComboDesc(keyComboDesc)
|
||||||
|
if keyComboDesc then
|
||||||
|
widget.boundKeyCombos[keyComboDesc] = callback
|
||||||
|
else
|
||||||
|
error('key combo \'' .. keyComboDesc .. '\' is failed')
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
function Hotkeys.unbind(keyComboDesc, widget)
|
||||||
|
widget = widget or rootWidget
|
||||||
|
if widget.boundKeyCombos == nil then return end
|
||||||
|
local keyComboDesc = retranslateKeyComboDesc(keyComboDesc)
|
||||||
|
if keyComboDesc then
|
||||||
|
widget.boundKeyCombos[keyComboDesc] = nil
|
||||||
|
end
|
||||||
|
end
|
||||||
|
Loading…
Reference in new issue