mirror of
https://github.com/edubart/otclient.git
synced 2025-10-14 03:24:55 +02:00
lua hotkeys system
This commit is contained in:
BIN
modules/core_lib/.hotkeys.lua.swp
Normal file
BIN
modules/core_lib/.hotkeys.lua.swp
Normal file
Binary file not shown.
@@ -14,6 +14,38 @@ LogFatal = 4
|
||||
|
||||
ActiveFocusReason = 2
|
||||
|
||||
KeyboardNoModifier = 0
|
||||
KeyboardCtrlModifier = 1
|
||||
KeyboardAltModifier = 2
|
||||
KeyboardCtrlAltModifier = 3
|
||||
KeyboardShiftModifier = 4
|
||||
KeyboardCtrlShiftModifier = 5
|
||||
KeyboardAltShiftModifier = 6
|
||||
KeyboardCtrlAltShiftModifier = 7
|
||||
|
||||
MouseNoButton = 0
|
||||
MouseLeftButton = 1
|
||||
MouseRightButton = 2
|
||||
MouseMidButton = 3
|
||||
|
||||
AlignNone = 0
|
||||
AlignLeft = 1
|
||||
AlignRight = 2
|
||||
AlignTop = 4
|
||||
AlignBottom = 8
|
||||
AlignHorizontalCenter = 16
|
||||
AlignVerticalCenter = 32
|
||||
AlignTopLeft = 5
|
||||
AlignTopRight = 6
|
||||
AlignBottomLeft = 9
|
||||
AlignBottomRight = 10
|
||||
AlignLeftCenter = 33
|
||||
AlignRightCenter = 34
|
||||
AlignTopCenter = 20
|
||||
AlignBottomCenter = 24
|
||||
AlignCenter = 48
|
||||
|
||||
|
||||
KeyUnknown = 0
|
||||
KeyEscape = 1
|
||||
KeyTab = 2
|
||||
@@ -123,29 +155,113 @@ KeyF10 = 138
|
||||
KeyF11 = 139
|
||||
KeyF12 = 140
|
||||
|
||||
KeyboardNoModifier = 0
|
||||
KeyboardCtrlModifier = 1
|
||||
KeyboardAltModifier = 2
|
||||
KeyboardShiftModifier = 4
|
||||
|
||||
MouseNoButton = 0
|
||||
MouseLeftButton = 1
|
||||
MouseRightButton = 2
|
||||
MouseMidButton = 3
|
||||
|
||||
AlignNone = 0
|
||||
AlignLeft = 1
|
||||
AlignRight = 2
|
||||
AlignTop = 4
|
||||
AlignBottom = 8
|
||||
AlignHorizontalCenter = 16
|
||||
AlignVerticalCenter = 32
|
||||
AlignTopLeft = 5
|
||||
AlignTopRight = 6
|
||||
AlignBottomLeft = 9
|
||||
AlignBottomRight = 10
|
||||
AlignLeftCenter = 33
|
||||
AlignRightCenter = 34
|
||||
AlignTopCenter = 20
|
||||
AlignBottomCenter = 24
|
||||
AlignCenter = 48
|
||||
KeyCodeDescs = {
|
||||
[KeyUnknown] = 'Unknown',
|
||||
[KeyEscape] = 'Escape',
|
||||
[KeyTab] = 'Tab',
|
||||
[KeyBackspace] = 'Backspace',
|
||||
[KeyReturn] = 'Return',
|
||||
[KeyEnter] = 'Enter',
|
||||
[KeyInsert] = 'Insert',
|
||||
[KeyDelete] = 'Delete',
|
||||
[KeyPause] = 'Pause',
|
||||
[KeyPrintScreen] = 'PrintScreen',
|
||||
[KeyHome] = 'Home',
|
||||
[KeyEnd] = 'End',
|
||||
[KeyPageUp] = 'PageUp',
|
||||
[KeyPageDown] = 'PageDown',
|
||||
[KeyUp] = 'Up',
|
||||
[KeyDown] = 'Down',
|
||||
[KeyLeft] = 'Left',
|
||||
[KeyRight] = 'Right',
|
||||
[KeyNumLock] = 'NumLock',
|
||||
[KeyScrollLock] = 'ScrollLock',
|
||||
[KeyCapsLock] = 'CapsLock',
|
||||
[KeyCtrl] = 'Ctrl',
|
||||
[KeyShift] = 'Shift',
|
||||
[KeyAlt] = 'Alt',
|
||||
[KeyAltGr] = 'AltGr',
|
||||
[KeyMeta] = 'Meta',
|
||||
[KeyMenu] = 'Menu',
|
||||
[KeySpace] = 'Space',
|
||||
[KeyExclamation] = '!',
|
||||
[KeyQuote] = '\"',
|
||||
[KeyNumberSign] = '#',
|
||||
[KeyDollar] = '$',
|
||||
[KeyPercent] = '%',
|
||||
[KeyAmpersand] = '&',
|
||||
[KeyApostrophe] = '\'',
|
||||
[KeyLeftParen] = '(',
|
||||
[KeyRightParen] = ')',
|
||||
[KeyAsterisk] = '*',
|
||||
[KeyPlus] = '+',
|
||||
[KeyComma] = ',',
|
||||
[KeyMinus] = '-',
|
||||
[KeyPeriod] = '.',
|
||||
[KeySlash] = '/',
|
||||
[Key0] = '0',
|
||||
[Key1] = '1',
|
||||
[Key2] = '2',
|
||||
[Key3] = '3',
|
||||
[Key4] = '4',
|
||||
[Key5] = '5',
|
||||
[Key6] = '6',
|
||||
[Key7] = '7',
|
||||
[Key8] = '8',
|
||||
[Key9] = '9',
|
||||
[KeyColon] = ':',
|
||||
[KeySemicolon] = ';',
|
||||
[KeyLess] = '<',
|
||||
[KeyEqual] = '=',
|
||||
[KeyGreater] = '>',
|
||||
[KeyQuestion] = '?',
|
||||
[KeyAtSign] = '@',
|
||||
[KeyA] = 'A',
|
||||
[KeyB] = 'B',
|
||||
[KeyC] = 'C',
|
||||
[KeyD] = 'D',
|
||||
[KeyE] = 'E',
|
||||
[KeyF] = 'F',
|
||||
[KeyG] = 'G',
|
||||
[KeyH] = 'H',
|
||||
[KeyI] = 'I',
|
||||
[KeyJ] = 'J',
|
||||
[KeyK] = 'K',
|
||||
[KeyL] = 'L',
|
||||
[KeyM] = 'M',
|
||||
[KeyN] = 'N',
|
||||
[KeyO] = 'O',
|
||||
[KeyP] = 'P',
|
||||
[KeyQ] = 'Q',
|
||||
[KeyR] = 'R',
|
||||
[KeyS] = 'S',
|
||||
[KeyT] = 'T',
|
||||
[KeyU] = 'U',
|
||||
[KeyV] = 'V',
|
||||
[KeyW] = 'W',
|
||||
[KeyX] = 'X',
|
||||
[KeyY] = 'Y',
|
||||
[KeyZ] = 'Z',
|
||||
[KeyLeftBracket] = '[',
|
||||
[KeyBackslash] = '\\',
|
||||
[KeyRightBracket] = ']',
|
||||
[KeyCaret] = '^',
|
||||
[KeyUnderscore] = '_',
|
||||
[KeyGrave] = '`',
|
||||
[KeyLeftCurly] = '{',
|
||||
[KeyBar] = '|',
|
||||
[KeyRightCurly] = '}',
|
||||
[KeyTilde] = '~',
|
||||
[KeyF1] = 'F1',
|
||||
[KeyF2] = 'F2',
|
||||
[KeyF3] = 'F3',
|
||||
[KeyF4] = 'F4',
|
||||
[KeyF5] = 'F5',
|
||||
[KeyF6] = 'F6',
|
||||
[KeyF7] = 'F7',
|
||||
[KeyF8] = 'F8',
|
||||
[KeyF9] = 'F9',
|
||||
[KeyF10] = 'F10',
|
||||
[KeyF11] = 'F11',
|
||||
[KeyF12] = 'F12'
|
||||
}
|
@@ -17,3 +17,4 @@ Module
|
||||
require 'dispatcher'
|
||||
require 'effects'
|
||||
require 'settings'
|
||||
require 'hotkeys'
|
||||
|
@@ -1,18 +1,23 @@
|
||||
function string:split(sep)
|
||||
local t = { }
|
||||
local function helper(word)
|
||||
table.insert(t, word)
|
||||
return ""
|
||||
end
|
||||
if not self:gsub("%w+", helper):find("%S") then
|
||||
return t
|
||||
function string.split(s, delim)
|
||||
local start = 1
|
||||
local results = {}
|
||||
while true do
|
||||
local pos = string.find(s, delim, start, true)
|
||||
if not pos then
|
||||
break
|
||||
end
|
||||
table.insert(results, string.sub(s, start, pos-1))
|
||||
start = pos + string.len(delim)
|
||||
end
|
||||
table.insert(results, string.sub(s, start))
|
||||
table.removevalue(results, '')
|
||||
return results
|
||||
end
|
||||
|
||||
function string:starts(start)
|
||||
return self:sub(1, #start) == start
|
||||
function string.starts(s, start)
|
||||
return string.sub(s, 1, #start) == start
|
||||
end
|
||||
|
||||
function string:trim()
|
||||
return self:match('^%s*(.*%S)') or ''
|
||||
function string.trim(s)
|
||||
return string.match(s, '^%s*(.*%S)') or ''
|
||||
end
|
||||
|
@@ -40,13 +40,17 @@ function table.find(t, value)
|
||||
end
|
||||
|
||||
function table.removevalue(t, value)
|
||||
local queue = {}
|
||||
for k,v in pairs(t) do
|
||||
if v == value then
|
||||
table.insert(queue, k)
|
||||
table.remove(t, k)
|
||||
end
|
||||
end
|
||||
for i,v in pairs(queue) do
|
||||
table.remove(t, i)
|
||||
end
|
||||
end
|
||||
|
||||
function table.compare(t, other)
|
||||
if #t ~= #other then return false end
|
||||
for k,v in pairs(t) do
|
||||
if v ~= other[k] then return false end
|
||||
end
|
||||
return true
|
||||
end
|
||||
|
@@ -1,6 +1,96 @@
|
||||
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
|
||||
|
||||
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
|
||||
|
Reference in New Issue
Block a user