- Documentation
- Client events
- onClientKey
Event
onClientKey
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. pressedbooltrueon press,falseon 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
triggerServerEventSends an event from the player to the server, for example a button click or a menu choice.addEventHandlerAttaches a function to an event. This is how you react to a player joining, dying, a key press, every frame and your own events.toggleControlEnables or disables one game control, e.g. shooting (fire), jumping (jump) or entering vehicles (enter_exit). A disabled control ignores the player's keys. On the server you pass a player orroot.