OnClientKey: Difference between revisions

From Multi Theft Auto: Wiki
Jump to navigation Jump to search
m (fixed link)
(Exception warning.)
 
(10 intermediate revisions by 8 users not shown)
Line 1: Line 1:
{{Client event}}
{{Client event}}
__NOTOC__  
__NOTOC__  
This event triggers whenever the user presses a button on their keyboard.
This event triggers whenever the user presses a button on their keyboard or mouse.
This event can also be used to see if the client scrolls his mousewheel.
This event can also be used to see if the client scrolls their mouse wheel.
{{Warning|This event is not triggered by the '''F8''' key.}}


==Parameters==
==Parameters==
<syntaxhighlight lang="lua">string button, bool pressOrRelease
<syntaxhighlight lang="lua">string button, bool pressOrRelease
</syntaxhighlight>
</syntaxhighlight>
* '''button''':  This refers the button pressed. see [[Key names]] for list of keys string
* '''button''':  This refers the button pressed. See [[key names]] for a list of keys.
* '''pressOrRelease''': This refers to whether they were pressing or releasing the key, true when pressing, false when releasing.
* '''pressOrRelease''': This refers to whether they were pressing or releasing the key, ''true'' when pressing, ''false'' when releasing.


==Source==
==Source==
Line 16: Line 17:
{{New items|5620|1.4|
{{New items|5620|1.4|
If this event is [[Event system#Canceling|canceled]], then all GTA and MTA binds, bound to the canceled key, won't be triggered.
If this event is [[Event system#Canceling|canceled]], then all GTA and MTA binds, bound to the canceled key, won't be triggered.
'''Note 1:''' The escape key can only be cancelled once. If a user presses the escape key twice in a row the main menu will still open.
'''Note 2:''' The event is only cancellable when the key is being pressed, not when being released.
}}
}}


Line 40: Line 45:
end )
end )
</syntaxhighlight>
</syntaxhighlight>
[[pl:onClientKey]]
[[pt-br:onClientKey]]


==See Also==
==See Also==
===GUI events===
{{GUI_events}}
{{GUI_events}}
===Client event functions===
===Client event functions===
{{Client_event_functions}}
{{Client_event_functions}}

Latest revision as of 19:31, 19 March 2024

This event triggers whenever the user presses a button on their keyboard or mouse. This event can also be used to see if the client scrolls their mouse wheel.

[[|link=|]] Warning: This event is not triggered by the F8 key.

Parameters

string button, bool pressOrRelease
  • button: This refers the button pressed. See key names for a list of keys.
  • pressOrRelease: This refers to whether they were pressing or releasing the key, true when pressing, false when releasing.

Source

The source of this event is the client's root element.

Cancel effect

ADDED/UPDATED IN VERSION 1.4 :

If this event is canceled, then all GTA and MTA binds, bound to the canceled key, won't be triggered.

Note 1: The escape key can only be cancelled once. If a user presses the escape key twice in a row the main menu will still open.

Note 2: The event is only cancellable when the key is being pressed, not when being released.

Example

This example will say in chatbox every time the user presses down a a key.

function playerPressedKey(button, press)
    if (press) then -- Only output when they press it down
        outputChatBox("You pressed the "..button.." key!")
    end
end
addEventHandler("onClientKey", root, playerPressedKey)

This example outputs if the client moves his mousewheel.

addEventHandler( "onClientKey", root, function(button,press) 
    -- Since mouse_wheel_up and mouse_wheel_down cant return a release, we dont have to check the press.
    if button == "mouse_wheel_up" or button == "mouse_wheel_down" then
        outputDebugString( button .. " moved." )
        return true
    end
    return false
end )

See Also

Input

GUI


Client event functions