getKeyState | Multi Theft Auto: Wiki Skip to content

getKeyState

Client-side
Server-side
Shared

Manual Review Required

Please finish this page using the corresponding Old Wiki article. Go to Contribution guidelines for more information.


This function determines if a certain key is pressed or not.

Syntax

bool getKeyState ( string keyName )
Required Arguments
  • keyName: The name of the key you're checking state of. See Key names .

Returns

  • bool: value

Returns true if the specified key is pressed, false if it isn't or if an invalid key name is passed.

Code Examples

shared

This clientside example prints a message when "p" is pressed, and a different one for the "control+p" combination.

-- define a function that outputs a message if control is pressed, and a different one if it isn't
function printMessageFunction()
-- if the left or right control keys are pressed, the user has pressed the "lctrl + p" combo.
if getKeyState("lctrl") or getKeyState("rctrl") then
outputChatBox ("You have pressed 'Left Control + P'.")
-- if none of those were pressed, the player just pressed the "p" key.
else
outputChatBox ("You have pressed 'p'.")
end
end
-- bind the "p" key to our function
bindKey("p", "down", printMessageFunction)