BindKey: Difference between revisions
Jump to navigation
Jump to search
No edit summary |
No edit summary |
||
Line 7: | Line 7: | ||
===Required Arguments=== | ===Required Arguments=== | ||
*'''thePlayer:''' The player you wish to bind the key of. | *'''thePlayer:''' The player you wish to bind the key of. | ||
*'''key:''' The key you wish to bind to the command. See [[Key names]] for a list of possible keys. | *'''key:''' The key or control you wish to bind to the command. See [[Key names]] for a list of possible keys and [[control names]] for a list of possible controls. | ||
*'''command:''' The command that will be triggered when the player's key is pressed. See [[Control names]] for a list of GTA keys that can be used. | *'''command:''' The command that will be triggered when the player's key is pressed. See [[Control names]] for a list of GTA keys that can be used. | ||
===Optional Arguments=== | ===Optional Arguments=== | ||
*'''keyState:''' A string containing the word "up" or "down" determining when the binded command will be called. | *'''keyState:''' A string containing the word "up" or "down" determining when the binded command will be called. Defaults to "down" if not specified. | ||
*'''arguments:''' Any arguments you may want to pass to the command handler. | *'''arguments:''' Any arguments you may want to pass to the command handler. | ||
==Example== | ==Example== | ||
This | '''Example 1:''' This example will bind a player's 'F1' key to a pair of console command. | ||
<syntaxhighlight lang="lua"> | <syntaxhighlight lang="lua"> | ||
addCommandHandler ( " | addCommandHandler ( "bindme", "bindthekeys" ) | ||
function | function bindthekeys ( source ) | ||
bindKey ( source, "F1", "down", "keydown" ) -- bind the player's F1 down key | bindKey ( source, "F1", "down", "keydown" ) -- bind the player's F1 down key | ||
bindKey ( source, "F1", "up", "keyup" ) -- bind the player's F1 up key | bindKey ( source, "F1", "up", "keyup" ) -- bind the player's F1 up key | ||
Line 33: | Line 31: | ||
function keyup ( source ) | function keyup ( source ) | ||
outputChatBox ( getClientName ( source ).." let go of the F1 key" ) | outputChatBox ( getClientName ( source ).." let go of the F1 key" ) | ||
end | |||
</syntaxhighlight> | |||
'''Example 2:''' This example will bind a player's handbrake key to a console command. | |||
<syntaxhighlight lang="lua"> | |||
addEventHandler ( "onPlayerJoin", getRootElement(), "playerJoin" ) | |||
function playerJoin () | |||
bindKey ( source, "handbrake", "down", "handbrakePressed", "1" ) | |||
bindKey ( source, "handbrake", "up", "handbrakePressed", "0" ) | |||
end | |||
addCommandHandler ( "handbrakePressed", "handbrakePressed" ) | |||
function handbrakePressed ( source, commandName, isDown ) | |||
if ( isDown == "1" ) | |||
outputChatBox ( getClientName ( source ) .. " is using the handbrake!" ) | |||
else | |||
outputChatBox ( getClientName ( source ) .. " has stopped using the handbrake!" ) | |||
end | |||
end | end | ||
</syntaxhighlight> | </syntaxhighlight> |
Revision as of 10:46, 12 July 2006
Binds a player's key to a console command, which will be triggered when the key is pressed.
Syntax
bindKey ( player thePlayer, string key, [ string keyState ], string command, [ string arguments ] )
Required Arguments
- thePlayer: The player you wish to bind the key of.
- key: The key or control you wish to bind to the command. See Key names for a list of possible keys and control names for a list of possible controls.
- command: The command that will be triggered when the player's key is pressed. See Control names for a list of GTA keys that can be used.
Optional Arguments
- keyState: A string containing the word "up" or "down" determining when the binded command will be called. Defaults to "down" if not specified.
- arguments: Any arguments you may want to pass to the command handler.
Example
Example 1: This example will bind a player's 'F1' key to a pair of console command.
addCommandHandler ( "bindme", "bindthekeys" ) function bindthekeys ( source ) bindKey ( source, "F1", "down", "keydown" ) -- bind the player's F1 down key bindKey ( source, "F1", "up", "keyup" ) -- bind the player's F1 up key end addCommandHandler ( "keydown", "keydown" ) function keydown ( source ) outputChatBox ( getClientName ( source ).." pressed the F1 key" ) end addCommandHandler ( "keyup", "keyup" ) function keyup ( source ) outputChatBox ( getClientName ( source ).." let go of the F1 key" ) end
Example 2: This example will bind a player's handbrake key to a console command.
addEventHandler ( "onPlayerJoin", getRootElement(), "playerJoin" ) function playerJoin () bindKey ( source, "handbrake", "down", "handbrakePressed", "1" ) bindKey ( source, "handbrake", "up", "handbrakePressed", "0" ) end addCommandHandler ( "handbrakePressed", "handbrakePressed" ) function handbrakePressed ( source, commandName, isDown ) if ( isDown == "1" ) outputChatBox ( getClientName ( source ) .. " is using the handbrake!" ) else outputChatBox ( getClientName ( source ) .. " has stopped using the handbrake!" ) end end
See Also
- addCommandHandler
- bindKey
- executeCommandHandler
- getCommandHandlers
- getFunctionsBoundToKey
- getKeyBoundToFunction
- isControlEnabled
- removeCommandHandler
- toggleAllControls
- toggleControl
- unbindKey