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
|
|
|
|
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
|
|
|
|
|
2012-08-25 21:11:54 +02:00
|
|
|
local function getKeyCode(key)
|
|
|
|
for keyCode, keyDesc in pairs(KeyCodeDescs) do
|
|
|
|
if keyDesc:lower() == key:trim():lower() then
|
|
|
|
return keyCode
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2012-02-06 05:39:52 +01:00
|
|
|
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
|
|
|
|
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
|
2013-01-26 23:12:00 +01:00
|
|
|
local callback = widget.boundAloneKeyDownCombos[determineKeyComboDesc(keyCode, KeyboardNoModifier)]
|
2013-02-18 17:16:22 +01:00
|
|
|
signalcall(callback, widget, keyCode)
|
2013-01-26 23:12:00 +01:00
|
|
|
callback = widget.boundKeyDownCombos[determineKeyComboDesc(keyCode, keyboardModifiers)]
|
2013-02-18 17:16:22 +01:00
|
|
|
return signalcall(callback, widget, keyCode)
|
2012-02-06 05:39:52 +01:00
|
|
|
end
|
|
|
|
|
2012-12-29 18:41:14 +01:00
|
|
|
local function onWidgetKeyUp(widget, keyCode, keyboardModifiers)
|
|
|
|
if keyCode == KeyUnknown then return false end
|
2013-01-26 23:12:00 +01:00
|
|
|
local callback = widget.boundAloneKeyUpCombos[determineKeyComboDesc(keyCode, KeyboardNoModifier)]
|
2013-02-18 17:16:22 +01:00
|
|
|
signalcall(callback, widget, keyCode)
|
2013-01-26 23:12:00 +01:00
|
|
|
callback = widget.boundKeyUpCombos[determineKeyComboDesc(keyCode, keyboardModifiers)]
|
2013-02-18 17:16:22 +01:00
|
|
|
return signalcall(callback, widget, keyCode)
|
2012-12-29 18:41:14 +01:00
|
|
|
end
|
|
|
|
|
2012-02-06 13:53:28 +01:00
|
|
|
local function onWidgetKeyPress(widget, keyCode, keyboardModifiers, autoRepeatTicks)
|
|
|
|
if keyCode == KeyUnknown then return false end
|
2013-02-18 17:16:22 +01:00
|
|
|
local callback = widget.boundKeyPressCombos[determineKeyComboDesc(keyCode, keyboardModifiers)]
|
|
|
|
return signalcall(callback, widget, keyCode, autoRepeatTicks)
|
2012-02-06 05:39:52 +01:00
|
|
|
end
|
|
|
|
|
|
|
|
local function connectKeyDownEvent(widget)
|
|
|
|
if widget.boundKeyDownCombos then return end
|
|
|
|
connect(widget, { onKeyDown = onWidgetKeyDown })
|
|
|
|
widget.boundKeyDownCombos = {}
|
2013-01-26 23:12:00 +01:00
|
|
|
widget.boundAloneKeyDownCombos = {}
|
2012-02-06 05:39:52 +01:00
|
|
|
end
|
|
|
|
|
2012-12-29 18:41:14 +01:00
|
|
|
local function connectKeyUpEvent(widget)
|
|
|
|
if widget.boundKeyUpCombos then return end
|
|
|
|
connect(widget, { onKeyUp = onWidgetKeyUp })
|
|
|
|
widget.boundKeyUpCombos = {}
|
2013-01-26 23:12:00 +01:00
|
|
|
widget.boundAloneKeyUpCombos = {}
|
2012-12-29 18:41:14 +01:00
|
|
|
end
|
|
|
|
|
2012-02-06 05:39:52 +01:00
|
|
|
local function connectKeyPressEvent(widget)
|
|
|
|
if widget.boundKeyPressCombos then return end
|
|
|
|
connect(widget, { onKeyPress = onWidgetKeyPress })
|
|
|
|
widget.boundKeyPressCombos = {}
|
|
|
|
end
|
|
|
|
|
|
|
|
-- public functions
|
2013-01-26 23:12:00 +01:00
|
|
|
function g_keyboard.bindKeyDown(keyComboDesc, callback, widget, alone)
|
2012-02-06 05:39:52 +01:00
|
|
|
widget = widget or rootWidget
|
|
|
|
connectKeyDownEvent(widget)
|
|
|
|
local keyComboDesc = retranslateKeyComboDesc(keyComboDesc)
|
2013-01-26 23:12:00 +01:00
|
|
|
if alone then
|
2013-02-18 17:16:22 +01:00
|
|
|
connect(widget.boundAloneKeyDownCombos, keyComboDesc, callback)
|
2013-01-26 23:12:00 +01:00
|
|
|
else
|
2013-02-18 17:16:22 +01:00
|
|
|
connect(widget.boundKeyDownCombos, keyComboDesc, callback)
|
2013-01-26 23:12:00 +01:00
|
|
|
end
|
2012-02-06 05:39:52 +01:00
|
|
|
end
|
|
|
|
|
2013-01-26 23:12:00 +01:00
|
|
|
function g_keyboard.bindKeyUp(keyComboDesc, callback, widget, alone)
|
2012-12-29 18:41:14 +01:00
|
|
|
widget = widget or rootWidget
|
|
|
|
connectKeyUpEvent(widget)
|
|
|
|
local keyComboDesc = retranslateKeyComboDesc(keyComboDesc)
|
2013-01-26 23:12:00 +01:00
|
|
|
if alone then
|
2013-02-18 17:16:22 +01:00
|
|
|
connect(widget.boundAloneKeyUpCombos, keyComboDesc, callback)
|
2013-01-26 23:12:00 +01:00
|
|
|
else
|
2013-02-18 17:16:22 +01:00
|
|
|
connect(widget.boundKeyUpCombos, keyComboDesc, callback)
|
2013-01-26 23:12:00 +01:00
|
|
|
end
|
2012-12-29 18:41:14 +01:00
|
|
|
end
|
|
|
|
|
2013-02-18 17:16:22 +01:00
|
|
|
function g_keyboard.bindKeyPress(keyComboDesc, callback, widget)
|
2012-02-06 05:39:52 +01:00
|
|
|
widget = widget or rootWidget
|
|
|
|
connectKeyPressEvent(widget)
|
|
|
|
local keyComboDesc = retranslateKeyComboDesc(keyComboDesc)
|
2013-02-18 17:16:22 +01:00
|
|
|
connect(widget.boundKeyPressCombos, keyComboDesc, callback)
|
2012-02-06 05:39:52 +01:00
|
|
|
end
|
|
|
|
|
2013-02-18 17:16:22 +01:00
|
|
|
local function getUnbindArgs(arg1, arg2)
|
|
|
|
local callback
|
|
|
|
local widget
|
|
|
|
if type(arg1) == 'function' then callback = arg1
|
|
|
|
elseif type(arg2) == 'function' then callback = arg2 end
|
|
|
|
if type(arg1) == 'userdata' then widget = arg1
|
|
|
|
elseif type(arg2) == 'userdata' then widget = arg2 end
|
2012-02-06 05:39:52 +01:00
|
|
|
widget = widget or rootWidget
|
2013-02-18 17:16:22 +01:00
|
|
|
return callback, widget
|
|
|
|
end
|
|
|
|
|
|
|
|
function g_keyboard.unbindKeyDown(keyComboDesc, arg1, arg2)
|
|
|
|
local callback, widget = getUnbindArgs(arg1, arg2)
|
2012-02-06 05:39:52 +01:00
|
|
|
if widget.boundKeyDownCombos == nil then return end
|
|
|
|
local keyComboDesc = retranslateKeyComboDesc(keyComboDesc)
|
2013-02-18 17:16:22 +01:00
|
|
|
disconnect(widget.boundKeyDownCombos, keyComboDesc, callback)
|
2012-02-06 05:39:52 +01:00
|
|
|
end
|
|
|
|
|
2012-12-29 18:41:14 +01:00
|
|
|
function g_keyboard.unbindKeyUp(keyComboDesc, widget)
|
2013-02-18 17:16:22 +01:00
|
|
|
local callback, widget = getUnbindArgs(arg1, arg2)
|
2012-12-29 18:41:14 +01:00
|
|
|
if widget.boundKeyUpCombos == nil then return end
|
|
|
|
local keyComboDesc = retranslateKeyComboDesc(keyComboDesc)
|
2013-02-18 17:16:22 +01:00
|
|
|
disconnect(widget.boundKeyUpCombos, keyComboDesc, callback)
|
2012-12-29 18:41:14 +01:00
|
|
|
end
|
|
|
|
|
2013-02-18 17:16:22 +01:00
|
|
|
function g_keyboard.unbindKeyPress(keyComboDesc, widget, callback)
|
|
|
|
local callback, widget = getUnbindArgs(arg1, arg2)
|
2012-02-06 05:39:52 +01:00
|
|
|
if widget.boundKeyPressCombos == nil then return end
|
|
|
|
local keyComboDesc = retranslateKeyComboDesc(keyComboDesc)
|
2013-02-18 17:16:22 +01:00
|
|
|
disconnect(widget.boundKeyPressCombos, keyComboDesc, callback)
|
2012-02-06 05:39:52 +01:00
|
|
|
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-08-25 21:11:54 +02:00
|
|
|
function g_keyboard.isKeyPressed(key)
|
|
|
|
if type(key) == 'string' then
|
|
|
|
key = getKeyCode(key)
|
|
|
|
end
|
|
|
|
return g_window.isKeyPressed(key)
|
|
|
|
end
|
|
|
|
|
2012-12-29 18:41:14 +01:00
|
|
|
function g_keyboard.isKeySetPressed(keys, all)
|
|
|
|
all = all or false
|
|
|
|
local result = {}
|
|
|
|
for k,v in pairs(keys) do
|
|
|
|
if type(v) == 'string' then
|
2012-12-30 07:14:49 +01:00
|
|
|
v = getKeyCode(v)
|
2012-12-29 18:41:14 +01:00
|
|
|
end
|
2012-12-30 07:14:49 +01:00
|
|
|
if g_window.isKeyPressed(v) then
|
2012-12-29 18:41:14 +01:00
|
|
|
if not all then
|
|
|
|
return true
|
|
|
|
end
|
|
|
|
table.insert(result, true)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
return #result == #keys
|
|
|
|
end
|
|
|
|
|
|
|
|
function g_keyboard.isInUse()
|
|
|
|
for i = FirstKey, LastKey do
|
|
|
|
if g_window.isKeyPressed(key) then
|
|
|
|
return true
|
|
|
|
end
|
|
|
|
end
|
|
|
|
return false
|
|
|
|
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
|