Garry's Mod Wiki

Player:KeyPressed

  boolean Player:KeyPressed( number key )

Description

Gets whether a key was just pressed this tick.

Arguments

1 number key
Corresponds to an IN enum. You can use bit.bor here (see example 2)

Returns

1 boolean
Was pressed or not

Example

Prints whenever the first player first starts pressing +forward key (w on QWERTY keyboards).

hook.Add( "Tick", "CheckPlayer1Forward", function() if Entity( 1 ):KeyPressed( IN_FORWARD ) then print( "Player1 just started moving forward!" ) end end )

Example

Respawn the player when pressing +attack, +attack2 or +jump (defaults to lmb, rmb and space respectively)

local respawnKeys = bit.bor( IN_ATTACK, IN_ATTACK2, IN_JUMP ) hook.Add( "PlayerDeathThink", "DoRespawn", function( ply ) if ply:KeyPressed( respawnKeys ) then ply:Spawn() end end )