Garry's Mod Wiki

GM:KeyPress

  GM:KeyPress( Player ply, number key )
This hook is predicted. This means that in singleplayer, it will not be called in the Client realm.

Description

Called whenever a player pressed a key included within the IN keys.

For a more general purpose function that handles all kinds of input, see GM:PlayerButtonDown.

Due to this being a predicted hook, ParticleEffects created only serverside from this hook will not be networked to the client, so make sure to do that on both realms.

Arguments

1 Player ply
The player pressing the key. If running client-side, this will always be LocalPlayer.
2 number key
The key that the player pressed using IN enum.

Example

hi will be printed to the console when the player presses the +use key (e).

hook.Add( "KeyPress", "keypress_use_hi", function( ply, key ) if ( key == IN_USE ) then print( "hi" ) end end )
Output: hi

Example

When a player tries to jump, they will be shot straight up in the air.

hook.Add( "KeyPress", "keypress_jump_super", function( ply, key ) if ( key == IN_JUMP ) then ply:SetVelocity( ply:GetVelocity() + Vector( 0, 0, 1000 ) ) end end )