BindKey: Difference between revisions

From Multi Theft Auto: Wiki
Jump to navigation Jump to search
mNo edit summary
 
(61 intermediate revisions by 29 users not shown)
Line 1: Line 1:
__NOTOC__  
{{Server client function}}
Binds a player's key to a console command, which will be triggered when the key is pressed.
__NOTOC__
Binds a player's key to a handler function or command, which will be called when the key is pressed.
 
{{Note|Using '''escape''' key or '''F8''' key will always return false. Use [[onClientKey]] event instead.}}
{{Note|Handler function won't be triggered while focused in CEGUI editbox. You can use [[guiSetInputMode]] or [[onClientKey]] in order to fix that.}}


==Syntax==  
==Syntax==  
<syntaxhighlight lang="lua">bindKey ( player thePlayer, string key, [ string keyState ], string command, [ string arguments ] ) </syntaxhighlight>  
<section name="Server - Syntax 1" class="server" show="true">
<syntaxhighlight lang="lua">bool bindKey ( player thePlayer, string key, string keyState, function handlerFunction, [ var arguments, ... ] )</syntaxhighlight>


===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 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.
*'''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.
*'''keyState:''' A string that has one of the following values:
**'''"up":''' If the bound key should trigger the function when the key is released
**'''"down":''' If the bound key should trigger the function when the key is pressed
**'''"both":''' If the bound key should trigger the function when the key is pressed or released
*'''handlerFunction:''' The function that will be triggered when the player's key is pressed. This function should have the form:
:<syntaxhighlight lang="lua">function functionName ( player keyPresser, string key, string keyState, [ var arguments, ... ] )</syntaxhighlight>
:The values passed to this function are:
:*'''keyPresser:''' The player who pressed the key
:*'''key:''' The key that was pressed
:*'''keyState:''' The state of the key that was pressed, ''down'' if it was pressed, ''up'' if it was released.
:*'''arguments''' The optional arguments you specified when calling [[bindKey]] (see below).
</section>


{{New feature|3|1.0|
<section name="Server - Syntax 2" class="server" show="true">
This alternative syntax allows you to bind a key with a command.  This will also allow users to customize the control in their Settings menu.  Use in conjunction with [[addCommandHandler]] to add handler functions to the keybind.
<syntaxhighlight lang="lua">bool bindKey ( player thePlayer, string key, string keyState, string commandName, [ string arguments ] )</syntaxhighlight>
===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.
*'''keyState:''' A string that has one of the following values:
**'''"up":''' If the bound key should trigger the function when the key is released
**'''"down":''' If the bound key should trigger the function when the key is pressed
**'''"both":''' If the bound key should trigger the function when the key is pressed or released
*'''commandName:''' The name of the command that the key should be binded to. 
===Optional Arguments===
===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''' Space delimited arguments that are entered as if one was typing the command.
*'''arguments:''' Any arguments you may want to pass to the command handler.
</section>
}}
 
<section name="Client - Syntax 1" class="client" show="true">
<syntaxhighlight lang="lua">bool bindKey ( string key, string keyState, function handlerFunction,  [ var arguments, ... ] ) </syntaxhighlight>
 
===Required Arguments===
*'''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.
*'''keyState:''' A string that has one of the following values:
**'''"up":''' If the bound key should trigger the function when the key is released
**'''"down":''' If the bound key should trigger the function when the key is pressed
**'''"both":''' If the bound key should trigger the function when the key is pressed or released
<!--*'''bindName:''' The name for this key bind when it appears in the client's settings dialog.-->
*'''handlerFunction:''' The function that will be triggered when the player's key is pressed. This function should have the form:
:<syntaxhighlight lang="lua">function functionName ( string key, string keyState, [ var arguments, ... ] )</syntaxhighlight>
:The values passed to this function are:
:*'''key:''' The key that was pressed
:*'''keyState:''' The state of the key that was pressed, ''down'' if it was pressed, ''up'' if it was released.
:*'''arguments''' The optional arguments you specified when calling [[bindKey]] (see below).
</section>


==Example=
{{New feature|3|1.0|
'''Example 1:''' This example will bind a player's 'F1' key to a pair of console command.
<section name="Client - Syntax 2" class="client" show="true">
<syntaxhighlight lang="lua">
This alternative syntax allows you to bind a key with a command.  This will also allow users to customize the control in their Settings menu.  Use in conjunction with [[addCommandHandler]] to add handler functions to the keybind.
addCommandHandler ( "bindme", "bindthekeys" )
 
function bindthekeys ( source )
<syntaxhighlight lang="lua">bool bindKey ( string key, string keyState, string commandName, [ string arguments ] )</syntaxhighlight>
  bindKey ( source, "F1", "down", "keydown" ) -- bind the player's F1 down key
 
  bindKey ( source, "F1", "up", "keyup" ) -- bind the player's F1 up key
===Required Arguments===
end
*'''key:''' The key or control you wish to bind to the command. See [[key names]] for a list of possible keys.
*'''keyState:''' A string that has one of the following values:
**'''"up":''' If the bound key should trigger the function when the key is released
**'''"down":''' If the bound key should trigger the function when the key is pressed
**'''"both":''' If the bound key should trigger the function when the key is pressed or released
*'''commandName:''' The name of the command that the key should be binded to. 
*'''arguments''' Space delimited arguments that are entered as if one was typing the command.
</section>
}}


addCommandHandler ( "keydown", "keydown" )
===Optional Arguments===
function keydown ( source )
{{OptionalArg}}
  outputChatBox ( getClientName ( source ).." pressed the F1 key" )
*'''arguments:''' Any arguments you may want to pass to the function when the key is pressed by the user. Any number of arguments of  can be specified, each being passed to the designated function. You may not pass functions.
end


addCommandHandler ( "keyup", "keyup" )
===Returns===
function keyup ( source )
Returns ''true'' if the key was bound, ''false'' otherwise.
  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. This takes advantage of the fact you can bind a key to a controller function, so that whatever key the player has bound locally this will work. This allows reliable notification as to when the player is pressing a particular key in-game. An easier, lower bandwidth but less reliable method is to use [[getControlState]].
==Example==
Example 1
<section name="Server" class="server" show="true">
This example will bind a player's 'F1' key and 'fire' control to 1 input function.
<syntaxhighlight lang="lua">
<syntaxhighlight lang="lua">
addEventHandler ( "onPlayerJoin", getRootElement(), "playerJoin" )
function funcInput ( player, key, keyState )
function playerJoin ()
  outputChatBox ( getPlayerName ( player) .. " " .. (keyState == "down" and "pressed" or "released") .. " the " .. key .. " key!" )
    bindKey ( source, "handbrake", "down", "handbrakePressed", "1" )
    bindKey ( source, "handbrake", "up", "handbrakePressed", "0" )
end
end


addCommandHandler ( "handbrakePressed", "handbrakePressed" )
function bindTheKeys ( player, commandName )
function handbrakePressed ( source, commandName, isDown )
  bindKey ( player, "F1", "down", funcInput )   -- bind the player's F1 down key
    if ( isDown == "1" )
  bindKey ( player, "F1", "up", funcInput )     -- bind the player's F1 up key
        outputChatBox ( getClientName ( source ) .. " is using the handbrake!" )
  bindKey ( player, "fire", "both", funcInput ) -- bind the player's fire down and up control
    else
        outputChatBox ( getClientName ( source ) .. " has stopped using the handbrake!" )
    end
end
end
addCommandHandler ( "bindme", bindTheKeys )
</syntaxhighlight>
</syntaxhighlight>
</section>


'''Example 3:''' This example shows how you can support moddifier keys, i.e. two keys held at once.
Example 2
<section name="Client" class="client" show="true">
This example will bind a player's 'F1' key and 'fire' control to 1 input function, clientside.
<syntaxhighlight lang="lua">
<syntaxhighlight lang="lua">
addEventHandler ( "onPlayerJoin", getRootElement(), "playerJoin" )
function funcInput ( key, keyState )
function playerJoin ()
outputChatBox( "You " .. (keyState == "down" and "pressed" or "let go of") .. " the " .. key .. " key!" )
    bindKey ( source, "lshift", "down", "keyPressed", "lshiftKey", "1" )
    bindKey ( source, "lshift", "up", "keyPressed", "lshiftKey", "0" )
 
    bindKey ( source, "q", "down", "keyPressed", "qKey", "1" )
    bindKey ( source, "q", "up", "keyPressed", "qKey", "0" )
end
end


-- Called when either of the two bound keys are pressed
function bindTheKeys ( commandName )
addCommandHandler ( "keyPressed", "keyPressed" )
bindKey( "F1", "down", funcInput )   -- bind the player's F1 down key
function keyPressed( source, commandName, keyName, isDown )
bindKey( "F1", "up", funcInput )     -- bind the player's F1 up key
    setElementData ( source, keyName, isDown )
bindKey( "fire", "both", funcInput ) -- bind the player's fire down and up control
end
end
addCommandHandler ( "bindme", bindTheKeys )
</syntaxhighlight>
</section>


-- Call this function every 100ms to check for players keys. Could be sped up by caching the players table and updating that only onPlayerJoin and onPlayerQuit.
<section name="Server" class="server" show="true">
setTimer ( "keyProcess", 100, 0 )
This example says how cool is the MTA:SA is if players wants to move.
function keyProcess ( )
<syntaxhighlight lang="lua">
    players = getElementsByType ( "player" )
function fanFunction()
    for playerKey, playerValue in players do
  bindKey (source,"forwards","down",
        checkPlayerKeys ( playerValue )
     function(player,key,state)
     end
      outputChatBox (getPlayerName (player) .. "#FFFF00 thinks MTA:SA is so cool.",root,255,255,0,true)
end
 
-- Called for each player to check if they're pressing shift + q
function checkPlayerKeys ( thePlayer )
    if ( getElementData ( thePlayer, "lshift" ) == 1 and getElementData ( thePlayer, "q" ) == 1 ) then
        outputChatBox ( thePlayer, "Shift + Q pressed!" )
     end
     end
  )
end
end
addEventHandler ("onPlayerLogin",root,fanFunction)
</syntaxhighlight>
</syntaxhighlight>
</section>


==See Also==
==See Also==
{{Input functions}}
{{Input functions}}
[[pt-br:bindKey]]

Latest revision as of 19:46, 19 March 2024

Binds a player's key to a handler function or command, which will be called when the key is pressed.

[[{{{image}}}|link=|]] Note: Using escape key or F8 key will always return false. Use onClientKey event instead.
[[{{{image}}}|link=|]] Note: Handler function won't be triggered while focused in CEGUI editbox. You can use guiSetInputMode or onClientKey in order to fix that.

Syntax

Click to collapse [-]
Server - Syntax 1
bool bindKey ( player thePlayer, string key, string keyState, function handlerFunction,  [ var 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.
  • keyState: A string that has one of the following values:
    • "up": If the bound key should trigger the function when the key is released
    • "down": If the bound key should trigger the function when the key is pressed
    • "both": If the bound key should trigger the function when the key is pressed or released
  • handlerFunction: The function that will be triggered when the player's key is pressed. This function should have the form:
function functionName ( player keyPresser, string key, string keyState, [ var arguments, ... ] )
The values passed to this function are:
  • keyPresser: The player who pressed the key
  • key: The key that was pressed
  • keyState: The state of the key that was pressed, down if it was pressed, up if it was released.
  • arguments The optional arguments you specified when calling bindKey (see below).
Click to collapse [-]
Server - Syntax 2

This alternative syntax allows you to bind a key with a command. This will also allow users to customize the control in their Settings menu. Use in conjunction with addCommandHandler to add handler functions to the keybind.

bool bindKey ( player thePlayer, string key, string keyState, string commandName, [ 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.
  • keyState: A string that has one of the following values:
    • "up": If the bound key should trigger the function when the key is released
    • "down": If the bound key should trigger the function when the key is pressed
    • "both": If the bound key should trigger the function when the key is pressed or released
  • commandName: The name of the command that the key should be binded to.

Optional Arguments

  • arguments Space delimited arguments that are entered as if one was typing the command.
Click to collapse [-]
Client - Syntax 1
bool bindKey ( string key, string keyState, function handlerFunction,  [ var arguments, ... ] ) 

Required Arguments

  • 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.
  • keyState: A string that has one of the following values:
    • "up": If the bound key should trigger the function when the key is released
    • "down": If the bound key should trigger the function when the key is pressed
    • "both": If the bound key should trigger the function when the key is pressed or released
  • handlerFunction: The function that will be triggered when the player's key is pressed. This function should have the form:
function functionName ( string key, string keyState, [ var arguments, ... ] )
The values passed to this function are:
  • key: The key that was pressed
  • keyState: The state of the key that was pressed, down if it was pressed, up if it was released.
  • arguments The optional arguments you specified when calling bindKey (see below).
Click to collapse [-]
Client - Syntax 2

This alternative syntax allows you to bind a key with a command. This will also allow users to customize the control in their Settings menu. Use in conjunction with addCommandHandler to add handler functions to the keybind.

bool bindKey ( string key, string keyState, string commandName, [ string arguments ] )

Required Arguments

  • key: The key or control you wish to bind to the command. See key names for a list of possible keys.
  • keyState: A string that has one of the following values:
    • "up": If the bound key should trigger the function when the key is released
    • "down": If the bound key should trigger the function when the key is pressed
    • "both": If the bound key should trigger the function when the key is pressed or released
  • commandName: The name of the command that the key should be binded to.
  • arguments Space delimited arguments that are entered as if one was typing the command.

Optional Arguments

NOTE: When using optional arguments, you might need to supply all arguments before the one you wish to use. For more information on optional arguments, see optional arguments.

  • arguments: Any arguments you may want to pass to the function when the key is pressed by the user. Any number of arguments of can be specified, each being passed to the designated function. You may not pass functions.

Returns

Returns true if the key was bound, false otherwise.

Example

Example 1

Click to collapse [-]
Server

This example will bind a player's 'F1' key and 'fire' control to 1 input function.

function funcInput ( player, key, keyState )
  outputChatBox ( getPlayerName ( player) .. " " .. (keyState == "down" and "pressed" or "released") .. " the " .. key .. " key!" )
end

function bindTheKeys ( player, commandName )
  bindKey ( player, "F1", "down", funcInput )   -- bind the player's F1 down key
  bindKey ( player, "F1", "up", funcInput )     -- bind the player's F1 up key
  bindKey ( player, "fire", "both", funcInput ) -- bind the player's fire down and up control
end
addCommandHandler ( "bindme", bindTheKeys )

Example 2

Click to collapse [-]
Client

This example will bind a player's 'F1' key and 'fire' control to 1 input function, clientside.

function funcInput ( key, keyState )
	outputChatBox( "You " .. (keyState == "down" and "pressed" or "let go of") .. " the " .. key .. " key!" )
end

function bindTheKeys ( commandName )
	bindKey( "F1", "down", funcInput )   -- bind the player's F1 down key
	bindKey( "F1", "up", funcInput )     -- bind the player's F1 up key
	bindKey( "fire", "both", funcInput ) -- bind the player's fire down and up control
end
addCommandHandler ( "bindme", bindTheKeys )
Click to collapse [-]
Server

This example says how cool is the MTA:SA is if players wants to move.

function fanFunction()
  bindKey (source,"forwards","down",
    function(player,key,state)
      outputChatBox (getPlayerName (player) .. "#FFFF00 thinks MTA:SA is so cool.",root,255,255,0,true)
    end
  )
end
addEventHandler ("onPlayerLogin",root,fanFunction)

See Also