Input
Three ways to get input#
| Need | Use | Permission |
|---|---|---|
| Let the user choose a key in your settings | ui.keybind(id, label, default_vk) | none |
| Know whether a key or mouse button is down right now | input.key_down(vk) | input |
| Pointer position in the overlay | input.mouse() | input |
| Click, hover and keys inside your own widget or canvas | the w / c table passed to your callback | none |
input.* reports state only (is it down now); there is no history and no way to send keys or clicks. It reports nothing useful while neither the game nor the overlay menu has the focus.
Key codes#
Keys are Windows virtual-key codes (integers). The common ones:
| Key | Code | Key | Code |
|---|---|---|---|
| Left mouse | 0x01 | 0-9 | 0x30-0x39 |
| Right mouse | 0x02 | A-Z | 0x41-0x5A (A = 0x41) |
| Middle mouse | 0x04 | F1-F12 | 0x70-0x7B |
| Mouse button 4 / 5 | 0x05 / 0x06 | Left, up, right, down arrows | 0x25, 0x26, 0x27, 0x28 |
| Backspace | 0x08 | Insert / Delete | 0x2D / 0x2E |
| Tab | 0x09 | Home / End | 0x24 / 0x23 |
| Enter | 0x0D | Page up / down | 0x21 / 0x22 |
| Shift / Ctrl / Alt | 0x10 / 0x11 / 0x12 | Space | 0x20 |
| Escape | 0x1B | Left Shift / Left Ctrl | 0xA0 / 0xA2 |
Detecting a press (not just "held")#
input.key_down is true for as long as the key is held. To react once per press, remember the previous state:
-- manifest: "permissions": ["input"]
local key = 0x46 -- F
local was = false
events.on("frame", function()
local down = input.key_down(key)
if down and not was then
log.info("F pressed")
end
was = down
end)A user-chosen key#
Combine the two: let the user pick the key, then poll it.
local show = false
local was = false
ui.tab("Panel", function()
ui.card("Key", function()
ui.keybind("toggle_key", "Toggle key", 0) -- saved under "toggle_key"
end)
end)
events.on("frame", function()
local k = store.get("toggle_key", 0)
if k ~= 0 then
local down = input.key_down(k)
if down and not was then show = not show end
was = down
end
end)ui.keybind returns the key code (0 means none).
Mouse in your own interface#
- A widget with
interactive = truereceivesw.mouse_x,w.mouse_y,w.hovered,w.downandw.clicked, in widget coordinates, only while the menu is open. With the menu closed the overlay lets the mouse through to the game, so buttons in a widget cannot be clicked then. - A
ui.canvascallback receivesc.mouse_x,c.mouse_y,c.hovered,c.down,c.clicked,c.released,c.right_clicked,c.wheel, andc.key_down(name)/c.key_pressed(name)for the keysa-z,0-9,left,right,up,down,space,enter,escape,shift,ctrl,tab.
What is not available#
There are no key-press events (only polling), no text-input capture outside your own controls, and no way to send input to the game. See the gaps page.