ServersYour serverFor creatorsForumFAQDocumentationSecurity
Download the launcher
  1. Documentation
  2. Shared functions
  3. bindKey

Function

bindKey

Server and clientInput and controlsIdentical to MTA:SA

Binds a function or a command to a player's key. The function gets (on the server also the player) the key name, the state and the extra arguments from bindKey. Instead of a function you can pass a command name: the key runs it like /command. On the server the player's game watches the key and reports presses to the server.

Syntax

Server

bool bindKey(player thePlayer, string key, string keyState, function|string handler, ...)

Client

bool bindKey(string key, string keyState, function|string handler, ...)

Arguments

thePlayerplayer
The player (server only).
keystring
The key: "a", "F2", "enter", "mouse1", "num_5"...
keyStatestring
"down" (press), "up" (release) or "both".
handlerfunction|string
A function or a command name.
...any, optional
Arguments passed to the function.

Returns

true if the key was bound (the same function twice returns false).

Example

F2 opens and closes a menu with the cursor.

local open = false
bindKey("F2", "down", function()
    open = not open
    showCursor(open)
end)

See also