2012-06-26 00:13:30 +02:00
|
|
|
-- @docclass
|
|
|
|
g_keyboard = {}
|
2012-02-06 05:39:52 +01:00
|
|
|
|
|
|
|
-- private functions
|
2012-02-07 04:32:15 +01:00
|
|
|
function translateKeyCombo(keyCombo)
|
2012-02-06 05:39:52 +01:00
|
|
|
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)
|
2012-06-20 02:15:56 +02:00
|
|
|
if keyComboDesc == nil then
|
|
|
|
error('Unable to translate key combo \'' .. keyComboDesc .. '\'')
|
|
|
|
end
|
2012-02-06 05:39:52 +01:00
|
|
|
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
|
|
|
|
|
2012-02-07 04:32:15 +01:00
|
|
|
function determineKeyComboDesc(keyCode, keyboardModifiers)
|
2012-02-06 05:39:52 +01:00
|
|
|
local keyCombo = {}
|
|
|
|
if keyCode == KeyCtrl or keyCode == KeyShift or keyCode == KeyAlt 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 onWidgetKeyDown(widget, keyCode, keyboardModifiers)
|
2012-02-06 13:53:28 +01:00
|
|
|
if keyCode == KeyUnknown then return false end
|
2012-02-06 05:39:52 +01:00
|
|
|
local keyComboDesc = determineKeyComboDesc(keyCode, keyboardModifiers)
|
|
|
|
local callback = widget.boundKeyDownCombos[keyComboDesc]
|
|
|
|
if callback then
|
|
|
|
callback()
|
|
|
|
return true
|
|
|
|
end
|
|
|
|
return false
|
|
|
|
end
|
|
|
|
|
2012-02-06 13:53:28 +01:00
|
|
|
local function onWidgetKeyPress(widget, keyCode, keyboardModifiers, autoRepeatTicks)
|
|
|
|
if keyCode == KeyUnknown then return false end
|
2012-03-18 14:34:39 +01:00
|
|
|
local keyComboDesc = determineKeyComboDesc(keyCode, keyboardModifiers)
|
2012-02-06 13:53:28 +01:00
|
|
|
local comboConf = widget.boundKeyPressCombos[keyComboDesc]
|
|
|
|
if comboConf and (autoRepeatTicks >= comboConf.autoRepeatDelay or autoRepeatTicks == 0) and comboConf.callback then
|
|
|
|
comboConf.callback()
|
2012-02-06 05:39:52 +01:00
|
|
|
return true
|
|
|
|
end
|
|
|
|
return false
|
|
|
|
end
|
|
|
|
|
|
|
|
local function connectKeyDownEvent(widget)
|
|
|
|
if widget.boundKeyDownCombos then return end
|
|
|
|
connect(widget, { onKeyDown = onWidgetKeyDown })
|
|
|
|
widget.boundKeyDownCombos = {}
|
|
|
|
end
|
|
|
|
|
|
|
|
local function connectKeyPressEvent(widget)
|
|
|
|
if widget.boundKeyPressCombos then return end
|
|
|
|
connect(widget, { onKeyPress = onWidgetKeyPress })
|
|
|
|
widget.boundKeyPressCombos = {}
|
|
|
|
end
|
|
|
|
|
|
|
|
-- public functions
|
2012-06-26 00:13:30 +02:00
|
|
|
function g_keyboard.bindKeyDown(keyComboDesc, callback, widget)
|
2012-02-06 05:39:52 +01:00
|
|
|
widget = widget or rootWidget
|
|
|
|
connectKeyDownEvent(widget)
|
|
|
|
local keyComboDesc = retranslateKeyComboDesc(keyComboDesc)
|
2012-06-20 02:15:56 +02:00
|
|
|
widget.boundKeyDownCombos[keyComboDesc] = callback
|
2012-02-06 05:39:52 +01:00
|
|
|
end
|
|
|
|
|
2012-06-26 00:13:30 +02:00
|
|
|
function g_keyboard.bindKeyPress(keyComboDesc, callback, widget, autoRepeatDelay)
|
2012-02-06 13:53:28 +01:00
|
|
|
autoRepeatDelay = autoRepeatDelay or 500
|
2012-02-06 05:39:52 +01:00
|
|
|
widget = widget or rootWidget
|
|
|
|
connectKeyPressEvent(widget)
|
|
|
|
local keyComboDesc = retranslateKeyComboDesc(keyComboDesc)
|
2012-06-20 02:15:56 +02:00
|
|
|
widget.boundKeyPressCombos[keyComboDesc] = { callback = callback, autoRepeatDelay = autoRepeatDelay }
|
|
|
|
widget:setAutoRepeatDelay(math.min(autoRepeatDelay, widget:getAutoRepeatDelay()))
|
2012-02-06 05:39:52 +01:00
|
|
|
end
|
|
|
|
|
2012-06-26 00:13:30 +02:00
|
|
|
function g_keyboard.unbindKeyDown(keyComboDesc, widget)
|
2012-02-06 05:39:52 +01:00
|
|
|
widget = widget or rootWidget
|
|
|
|
if widget.boundKeyDownCombos == nil then return end
|
|
|
|
local keyComboDesc = retranslateKeyComboDesc(keyComboDesc)
|
|
|
|
if keyComboDesc then
|
|
|
|
widget.boundKeyDownCombos[keyComboDesc] = nil
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2012-06-26 00:13:30 +02:00
|
|
|
function g_keyboard.unbindKeyPress(keyComboDesc, widget)
|
2012-02-06 05:39:52 +01:00
|
|
|
widget = widget or rootWidget
|
|
|
|
if widget.boundKeyPressCombos == nil then return end
|
|
|
|
local keyComboDesc = retranslateKeyComboDesc(keyComboDesc)
|
|
|
|
if keyComboDesc then
|
|
|
|
widget.boundKeyPressCombos[keyComboDesc] = nil
|
|
|
|
end
|
|
|
|
end
|
2012-06-07 14:25:41 +02:00
|
|
|
|
2012-06-26 00:13:30 +02:00
|
|
|
function g_keyboard.getModifiers()
|
2012-06-07 14:25:41 +02:00
|
|
|
return g_window.getKeyboardModifiers()
|
|
|
|
end
|
|
|
|
|
2012-06-26 00:13:30 +02:00
|
|
|
function g_keyboard.isCtrlPressed()
|
2012-06-07 14:25:41 +02:00
|
|
|
return bit32.band(g_window.getKeyboardModifiers(), KeyboardCtrlModifier) ~= 0
|
|
|
|
end
|
|
|
|
|
2012-06-26 00:13:30 +02:00
|
|
|
function g_keyboard.isAltPressed()
|
2012-06-07 14:25:41 +02:00
|
|
|
return bit32.band(g_window.getKeyboardModifiers(), KeyboardAltModifier) ~= 0
|
|
|
|
end
|
|
|
|
|
2012-06-26 00:13:30 +02:00
|
|
|
function g_keyboard.isShiftPressed()
|
2012-06-07 14:25:41 +02:00
|
|
|
return bit32.band(g_window.getKeyboardModifiers(), KeyboardShiftModifier) ~= 0
|
|
|
|
end
|