BindKey: Difference between revisions

From Multi Theft Auto: Wiki
Jump to navigation Jump to search
No edit summary
mNo edit summary
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. Keys that are bound to actual keyboard keys are displayed and can be modified by the user from the settings window.
__NOTOC__
Binds a player's key to a handler function, which will be called when the key is pressed. Keys that are bound to actual keyboard keys are displayed and can be modified by the user from the settings window.


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


===Required Arguments===  
===Required Arguments===
<section name="Server" class="server" show="true">
*'''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.
Line 26: Line 23:
:*'''arguments''' The optional arguments you specified when calling [[bindKey]] (see below).
:*'''arguments''' The optional arguments you specified when calling [[bindKey]] (see below).
</section>
</section>
<section name="Client" class="client" show="false">
 
<section name="Client" class="client">
<syntaxhighlight lang="lua">bindKey ( string key, string keyState, string bindName, 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.
*'''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:
*'''keyState:''' A string that has one of the following values:
Line 45: Line 46:
*'''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.
*'''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.


==Example==
==Example==
<section name="Example 1" class="server" show="true">
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.
This example will bind a player's 'F1' key and 'fire' control to 1 input function.
<syntaxhighlight lang="lua">
<syntaxhighlight lang="lua">
Line 58: Line 60:


function bindTheKeys ( source )
function bindTheKeys ( source )
   bindKey ( source, "F1", "down", "", funcInput ) -- bind the player's F1 down key
   bindKey ( source, "F1", "down", "", funcInput )   -- bind the player's F1 down key
   bindKey ( source, "F1", "up", "", funcInput ) -- bind the player's F1 up key
   bindKey ( source, "F1", "up", "", funcInput )     -- bind the player's F1 up key
   bindKey ( source, "fire", "both", "", funcInput ) -- bind the player's fire down and up control
   bindKey ( source, "fire", "both", "", funcInput ) -- bind the player's fire down and up control
end
end
Line 65: Line 67:
</syntaxhighlight>
</syntaxhighlight>
</section>
</section>
<section name="Example 2" class="client" show="false">
 
Example 2
<section name="Client" class="client" show="false">
This example will bind a player's 'F1' key and 'fire' control to 1 input function, clientside.
This example will bind a player's 'F1' key and 'fire' control to 1 input function, clientside.
<syntaxhighlight lang="lua">
<syntaxhighlight lang="lua">
Line 77: Line 81:


function bindTheKeys ( source )
function bindTheKeys ( source )
   bindKey ( "F1", "down", "", funcInput ) -- bind the player's F1 down key
   bindKey ( "F1", "down", "", funcInput )   -- bind the player's F1 down key
   bindKey ( "F1", "up", "", funcInput ) -- bind the player's F1 up 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
   bindKey ( "fire", "both", "", funcInput ) -- bind the player's fire down and up control
end
end

Revision as of 14:17, 21 August 2007

Binds a player's key to a handler function, which will be called when the key is pressed. Keys that are bound to actual keyboard keys are displayed and can be modified by the user from the settings window.

Syntax

Click to collapse [-]
Server
bindKey ( player thePlayer, string key, string keyState, string bindName, 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
  • 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:
function functionName ( source, key, keyState, [ var arguments, ... ] )
The values passed to this function are:
  • source: 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 expand [+]
Client

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.

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 ( source, key, keyState )
  local state = "let go of"
  if ( keyState == "down" ) then
    state = "pressed"
  end
  outputChatBox ( getClientName ( source ) .. " " .. state .. " the " .. key .. " key!" )
end

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

Example 2

Click to expand [+]
Client

See Also