GetKeyState: Difference between revisions
Jump to navigation
Jump to search
(One intermediate revision by one other user not shown) | |||
Line 21: | Line 21: | ||
-- define a function that outputs a message if control is pressed, and a different one if it isn't | -- define a function that outputs a message if control is pressed, and a different one if it isn't | ||
function printMessageFunction() | function printMessageFunction() | ||
-- if the left or right control keys are pressed, the user has pressed the " | -- if the left or right control keys are pressed, the user has pressed the "lctrl + p" combo. | ||
if getKeyState( "lctrl" ) | if getKeyState("lctrl") or getKeyState("rctrl") then | ||
outputChatBox ( "You have pressed ' | outputChatBox ("You have pressed 'Left Control + P'.") | ||
-- if none of those were pressed, | -- if none of those were pressed, the player just pressed the "p" key. | ||
else | else | ||
outputChatBox ( "You have pressed 'p'." ) | outputChatBox ("You have pressed 'p'.") | ||
end | end | ||
end | end | ||
-- bind the "p" key to our function | -- bind the "p" key to our function | ||
bindKey( "p", "down | bindKey("p", "down", printMessageFunction) | ||
</syntaxhighlight> | </syntaxhighlight> | ||
==See Also== | ==See Also== | ||
{{Client_input_functions}} | {{Client_input_functions}} |
Latest revision as of 03:22, 26 July 2020
This function determines if a certain key is pressed or not.
Note: 'ralt' may trigger both 'ralt' and 'lctrl', this is due to AltGr
Syntax
bool getKeyState ( string keyName )
Required Arguments
- keyName: The name of the key you're checking state of. See Key names.
Returns
Returns true if the specified key is pressed, false if it isn't or if an invalid key name is passed.
Example
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)
See Also
- getAnalogControlState
- getBoundKeys
- getCommandsBoundToKey
- getKeyBoundToCommand
- getKeyState
- isCapsLockEnabled
- setAnalogControlState
- Shared
- addCommandHandler
- bindKey
- executeCommandHandler
- getCommandHandlers
- getFunctionsBoundToKey
- getKeyBoundToFunction
- isControlEnabled
- removeCommandHandler
- toggleAllControls
- toggleControl
- unbindKey