ServersYour serverFor creatorsForumFAQDocumentationSecurity
Download the launcher
  1. Documentation
  2. Client events
  3. onClientKey

Event

onClientKey

ClientInput and controlsIdentical to MTA:SA

The player pressed or released a key. The event sees every key, including the ones the game uses.

Handler

addEventHandler("onClientKey", root, function(key, pressed) ... end)

Parameters

keystring
Key name, e.g. a, enter, mouse1, F2.
pressedbool
true on press, false on release.

Source

source is root.

The key reaches the script after the game has already received it, so cancelEvent() does not stop game actions (shooting, switching weapons with Q / E / the wheel, jumping). Use toggleControl (e.g. fire, next_weapon, previous_weapon) or toggleAllControls for that. cancelEvent() only stops entering as a passenger with g. While the player types in a text field (a GUI edit or memo with the cursor shown, or a browser with keyboard focus), escape goes only to the script and the page and the ESC menu does not open; a second ESC within 0.6 s opens the menu.

Example

While the menu is open, the left mouse button does not shoot.

local menuOpen = false

bindKey("F3", "down", function()
    menuOpen = not menuOpen
    showCursor(menuOpen)
    toggleControl("fire", not menuOpen)
end)

addEventHandler("onClientKey", root, function(key, pressed)
    if menuOpen and pressed then
        outputConsole("Key in menu: " .. key)
    end
end)

Notes

One key, one action? If you just want to call a function on a key, bindKey is simpler. onClientKey is useful when you want to see every key or block them.

See also