Hotkey.lua (3743B)
1 -- Hotkey.lua 2 -- Keyboard shortcut management system 3 4 local Hotkey = {} 5 6 -- Create a new hotkey manager instance 7 function Hotkey.newInstance() 8 local instance = { 9 hotkeys = {} -- Map of normalized hotkey strings to callback functions 10 } 11 12 -- Normalize a hotkey string to consistent ordering: ctrl+alt+meta+shift+key 13 local function normalizeHotkey(hotkeyStr) 14 -- Split the hotkey string by '+' 15 local parts = {} 16 for part in hotkeyStr:gmatch("[^+]+") do 17 table.insert(parts, part:lower()) 18 end 19 20 -- Separate modifiers from the key 21 local modifiers = { 22 ctrl = false, 23 alt = false, 24 meta = false, 25 shift = false 26 } 27 local key = nil 28 29 for _, part in ipairs(parts) do 30 if part == "ctrl" or part == "control" then 31 modifiers.ctrl = true 32 elseif part == "alt" then 33 modifiers.alt = true 34 elseif part == "meta" or part == "super" or part == "cmd" then 35 modifiers.meta = true 36 elseif part == "shift" then 37 modifiers.shift = true 38 else 39 -- This is the actual key 40 key = part 41 end 42 end 43 44 -- Build normalized string in order: ctrl+alt+meta+shift+key 45 local normalized = "" 46 if modifiers.ctrl then 47 normalized = normalized .. "ctrl+" 48 end 49 if modifiers.alt then 50 normalized = normalized .. "alt+" 51 end 52 if modifiers.meta then 53 normalized = normalized .. "meta+" 54 end 55 if modifiers.shift then 56 normalized = normalized .. "shift+" 57 end 58 if key then 59 normalized = normalized .. key 60 else 61 -- No key specified, just return the modifiers 62 -- Remove trailing '+' 63 if normalized:sub(-1) == "+" then 64 normalized = normalized:sub(1, -2) 65 end 66 end 67 68 return normalized 69 end 70 71 -- Add a hotkey 72 function instance.add(hotkeyStr, callback) 73 if type(hotkeyStr) ~= "string" then 74 error("Hotkey string must be a string") 75 end 76 if type(callback) ~= "function" then 77 error("Callback must be a function") 78 end 79 80 local normalized = normalizeHotkey(hotkeyStr) 81 instance.hotkeys[normalized] = callback 82 83 return normalized -- Return the normalized form for reference 84 end 85 86 -- Remove a hotkey 87 function instance.remove(hotkeyStr) 88 if type(hotkeyStr) ~= "string" then 89 error("Hotkey string must be a string") 90 end 91 92 local normalized = normalizeHotkey(hotkeyStr) 93 instance.hotkeys[normalized] = nil 94 95 return normalized -- Return the normalized form for reference 96 end 97 98 -- Check if a hotkey exists 99 function instance.has(hotkeyStr) 100 local normalized = normalizeHotkey(hotkeyStr) 101 return instance.hotkeys[normalized] ~= nil 102 end 103 104 -- Trigger a hotkey if it exists 105 function instance.trigger(hotkeyStr, ...) 106 local normalized = normalizeHotkey(hotkeyStr) 107 local callback = instance.hotkeys[normalized] 108 109 if callback then 110 return callback(...) 111 end 112 113 return false -- Hotkey not found 114 end 115 116 -- Get all registered hotkeys 117 function instance.getAll() 118 local list = {} 119 for hotkey, callback in pairs(instance.hotkeys) do 120 table.insert(list, hotkey) 121 end 122 return list 123 end 124 125 -- Clear all hotkeys 126 function instance.clear() 127 instance.hotkeys = {} 128 end 129 130 return instance 131 end 132 133 return Hotkey