BindKey: Difference between revisions

From Multi Theft Auto: Wiki
Jump to navigation Jump to search
No edit summary
No edit summary
Line 3: Line 3:


==Syntax==  
==Syntax==  
<syntaxhighlight lang="lua">bindKey ( player thePlayer, string key, [ string keyState ], string command, [ string argument ] ) </syntaxhighlight>  
<syntaxhighlight lang="lua">bindKey ( player thePlayer, string key, bool keyState, string function, [ var aguments, ... ] ) </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 boolean value representing the "hit-state" of the key (whether it will be trigged when pressed or released)
*'''function:''' The function 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. Defaults to "down" if not specified.
{{OptionalArg}}
*'''argument:''' Any arguments you may want to pass to the command handler when the key is pressed by the user. When this string is recieved by the server, it will be split up into seperate words (seperated by spaces) and passed to any attached command handlers.
*'''arguments:''' Any arguments you may want to pass to the function when the key is pressed by the user. When this string is recieved by the server, it will be split up into seperate words (seperated by spaces) and passed to any attached command handlers.


==Example==   
==Example==   
'''Example 1:''' This example will bind a player's 'F1' key to a pair of console command.
'''Example 1:''' This example will bind a player's 'F1' key and 'fire' control to 1 input function.
<syntaxhighlight lang="lua">
<syntaxhighlight lang="lua">
addCommandHandler ( "bindme", "bindthekeys" )
addCommandHandler ( "bindme", "bindthekeys" )
function bindthekeys ( source )
function bindthekeys ( source )
   bindKey ( source, "F1", "down", "keydown" ) -- bind the player's F1 down key
   bindKey ( source, "F1", true, "funcInput" ) -- bind the player's F1 down key
   bindKey ( source, "F1", "up", "keyup" ) -- bind the player's F1 up key
  bindKey ( source, "F1", false, "funcInput" ) -- bind the player's F1 up key
   bindKey ( source, "fire", true, "funcInput" ) -- bind the player's fire down control
  bindKey ( source, "fire", false, "funcInput" ) -- bind the player's fire up control
end
end
 
addCommandHandler ( "keydown", "funKeydown" )
function funcInput ( source, key, hitState )
function funKeydown ( source )
   state = " let go of "
  outputChatBox ( getClientName ( source ).." pressed the F1 key" )
  if ( hitState ) then
end
     state = " pressed "
 
  end
addCommandHandler ( "keyup", "funcKeyup" )
  outputChatBox ( getClientName ( source ) .. state .. "the " .. key .. " key!" )
function funcKeyup ( source )
   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]].
<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
</syntaxhighlight>
 
'''Example 3:''' This example shows how you can support moddifier keys, i.e. two keys held at once.
<syntaxhighlight lang="lua">
addEventHandler ( "onPlayerJoin", getRootElement(), "playerJoin" )
function playerJoin ()
    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
 
-- Called when either of the two bound keys are pressed
addCommandHandler ( "keyPressed", "keyPressed" )
function keyPressed( source, commandName, keyName, isDown )
    setElementData ( source, keyName, isDown )
end
 
-- 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.
setTimer ( "keyProcess", 100, 0 )
function keyProcess ( )
    players = getElementsByType ( "player" )
    for playerKey, playerValue in players do
        checkPlayerKeys ( playerValue )
    end
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
</syntaxhighlight>
</syntaxhighlight>

Revision as of 13:10, 10 October 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, bool keyState, string function, [ var aguments, ... ] ) 

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 boolean value representing the "hit-state" of the key (whether it will be trigged when pressed or released)
  • function: The function 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

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. When this string is recieved by the server, it will be split up into seperate words (seperated by spaces) and passed to any attached command handlers.

Example

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

addCommandHandler ( "bindme", "bindthekeys" )
function bindthekeys ( source )
  bindKey ( source, "F1", true, "funcInput" ) -- bind the player's F1 down key
  bindKey ( source, "F1", false, "funcInput" ) -- bind the player's F1 up key
  bindKey ( source, "fire", true, "funcInput" ) -- bind the player's fire down control
  bindKey ( source, "fire", false, "funcInput" ) -- bind the player's fire up control
end
 
function funcInput ( source, key, hitState )
  state = " let go of "
  if ( hitState ) then
    state = " pressed "
  end
  outputChatBox ( getClientName ( source ) .. state .. "the " .. key .. " key!" )
end

See Also