BindKey: Difference between revisions

From Multi Theft Auto: Wiki
Jump to navigation Jump to search
mNo edit summary
Line 130: Line 130:


<section name="Server" class="server" show="true">
<section name="Server" class="server" show="true">
This example says how cool <s>Robpol86</s> Ransom is if players wants to move.
This example says how cool <s>Robpol86</s> <s>Ransom</s> Aintaro is if players wants to move.
<syntaxhighlight lang="lua">
<syntaxhighlight lang="lua">
function fanFunction()
function fanFunction()
   bindKey (source,"forwards","down",
   bindKey (source,"forwards","down",
     function(player,key,state)
     function(player,key,state)
       outputChatBox (getPlayerName (player) .. "#FFFF00 thinks Ransom is cool.",getRootElement(),255,255,0,true)
       outputChatBox (getPlayerName (player) .. "#FFFF00 thinks Aintaro is cool.",getRootElement(),255,255,0,true)
     end
     end
   )
   )

Revision as of 13:47, 12 July 2014

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

Syntax

Click to collapse [-]
Server - Syntax 1
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.

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
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.

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.

Issues

Issue ID Description
#7948 bindKey using a command and control name doesn't work

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

function bindTheKeys ()
  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 Robpol86 Ransom Aintaro is if players wants to move.

function fanFunction()
  bindKey (source,"forwards","down",
    function(player,key,state)
      outputChatBox (getPlayerName (player) .. "#FFFF00 thinks Aintaro is cool.",getRootElement(),255,255,0,true)
    end
  )
end
addEventHandler ("onPlayerLogin",getRootElement(),fanFunction)

See Also