Skip to content

OnKeyBinding

A key the game mode declared was pressed or released.

The game mode declares keys of its own with its RegisterKeyBinding - “open the map”, “whistle for the horse” - and each player’s game binds them to a key: the mode’s default, or one the player chose. This callback runs when that key goes down (pressed is true) and when it comes up (false), with the binding’s name as the mode declared it, whichever physical key it is on. The server hears the same press in its OnPlayerKeyBinding, and the pages of the web interface in KcdMp.onKeyBinding.

A bound key belongs to the mode: the game does not get it while it is bound. OnKey hears a bound key like any other. Nothing is heard while the chat box or the F9 window is open, one of the game’s own menus is up, a page holds the keyboard or SetKeyboardCapture took it; a key that went down as a binding always gets its release, even if one of those came up in between, and so does the game’s window losing the focus. A held key’s repeat is one press.

Keys the game keeps for itself can never be bound: F9, F12, Esc, Tab, Enter, the Windows keys, G and T, the walk, jump, crouch and sprint keys, the push-to-talk key, the third-person key, the cursor key and the keys that scroll the chat.

function OnKeyBinding(name, pressed)
-- ...
end
Parameter Type
name string the binding’s name, as the mode declared it (a-z 0-9 _ . : -, 32 characters at most)
pressed boolean true when the key went down, false when it came up

nothing

local open = false
function OnKeyBinding(name, pressed)
if name == "map.toggle" and pressed then
open = not open
if open then ShowUiElement("map") else HideUiElement("map") end
end
end

OnKey · ShowUiElement · the The game’s UI for a script group of the index