05-03-26
Layered Keybind System with Hold Key
This Lua script for hardware input allows you to create a layered keybind system. Hold a key to access an alternate set of bindings, expanding the functionality of your hardware. Customize the script
rebindlua scriptkeybindlayerhardware inputscriptremap
Question
Layer system: hold key to access alternate binds
Rebind AI
luau
-- rebind: min_sdk=0.1.0
-- rebind: name=Layer System
-- rebind: description=Hold a key to access alternate key bindings
local cfg = UI.Schema({
layer_key = UI.Keybind("LAlt", { label = "Layer Key (Hold to Activate)" }),
-- Layer 1 (base layer)
layer1_f9 = UI.Keybind("F9", { label = "F9 Base", group = "Layer 1" }),
layer1_f10 = UI.Keybind("F10", { label = "F10 Base", group = "Layer 1" }),
layer1_mouse4 = UI.Keybind("Mouse4", { label = "Mouse4 Base", group = "Layer 1" }),
-- Layer 2 (alternate layer)
layer2_f9 = UI.Keybind("1", { label = "F9 → Layer 2", group = "Layer 2" }),
layer2_f10 = UI.Keybind("2", { label = "F10 → Layer 2", group = "Layer 2" }),
layer2_mouse4 = UI.Keybind("3", { label = "Mouse4 → Layer 2", group = "Layer 2" }),
})
local layerActive = false
local keyMapping = {}
-- Define what each key does in each layer
local function setupLayers()
keyMapping = {
-- Base layer (layer 1)
[cfg.layer1_f9] = {
action = function()
Log.Info("F9 (base layer)")
HID.Press("LCtrl+A")
end,
},
[cfg.layer1_f10] = {
action = function()
Log.Info("F10 (base layer)")
HID.Press("LCtrl+C")
end,
},
[cfg.layer1_mouse4] = {
action = function()
Log.Info("Mouse4 (base layer)")
HID.Press("LCtrl+Z")
end,
},
-- Alt layer (layer 2)
[cfg.layer2_f9] = {
action = function()
Log.Info("F9 → 1 (alt layer)")
HID.Type("Layer 2 action A")
end,
},
[cfg.layer2_f10] = {
action = function()
Log.Info("F10 → 2 (alt layer)")
HID.Type("Layer 2 action B")
end,
},
[cfg.layer2_mouse4] = {
action = function()
Log.Info("Mouse4 → 3 (alt layer)")
HID.Type("Layer 2 action C")
end,
},
}
end
function OnStart()
setupLayers()
Log.Info("Layer system ready. Hold " .. cfg.layer_key .. " to switch layers.")
end
function OnDown(key)
-- Detect layer activation
if key == cfg.layer_key then
layerActive = true
UI.Notify("Layer 2 Active", "info")
return false
end
-- Base layer: standard keys
if not layerActive then
if key == cfg.layer1_f9 then
keyMapping[cfg.layer1_f9].action()
return false
end
if key == cfg.layer1_f10 then
keyMapping[cfg.layer1_f10].action()
return false
end
if key == cfg.layer1_mouse4 then
keyMapping[cfg.layer1_mouse4].action()
return false
end
return true
end
-- Alt layer: remapped keys
if layerActive then
if key == cfg.layer2_f9 then
keyMapping[cfg.layer2_f9].action()
return false
end
if key == cfg.layer2_f10 then
keyMapping[cfg.layer2_f10].action()
return false
end
if key == cfg.layer2_mouse4 then
keyMapping[cfg.layer2_mouse4].action()
return false
end
end
return true
end
function OnUp(key)
if key == cfg.layer_key then
layerActive = false
UI.Notify("Layer 1 Active", "info")
return false
end
return true
end
function OnBlur()
layerActive = false
endHow It Works
This script implements a layer system where holding a modifier key (default: LAlt) activates an alternate set of key bindings. Think of it like Vim modes or gaming overlay systems.
Key features:
- Layer Key — Hold LAlt to activate Layer 2. Release to go back to Layer 1.
- Remapping — Each physical key can do different things depending on the active layer:
- Base layer (Layer 1): F9 selects all, F10 copies, Mouse4 undoes
- Alt layer (Layer 2): F9 types text, F10 types different text, Mouse4 types more text
- Easy configuration — Edit the
setupLayers()function to change what each key does - Notifications — UI feedback shows which layer is currently active
To customize:
Replace the action = function() ... end blocks with your own logic. For example:
luau
[cfg.layer1_f9] = {
action = function()
Run(function()
HID.Down("W")
Sleep(500)
HID.Up("W")
end)
end,
}