Revision Difference
GM:KeyPress#523766
<cat>hook</cat>⤶
<function name="KeyPress" parent="GM" type="hook">
<ishook>yes</ishook>
<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 <page>GM:PlayerButtonDown</page>
<warning>Due to this being a predicted hook, <page>Global.ParticleEffect</page>s created only serverside from this hook will not be networked to the client, so make sure to do that on both realms</warning>
</description>
<realm>Shared</realm>
<predicted>Yes</predicted>
<args>
<arg name="ply" type="Player">The player pressing the key. If running client-side, this will always be <page>Global.LocalPlayer</page></arg>
<arg name="key" type="number">The key that the player pressed using <page>Enums/IN</page>.</arg>
</args>
</function>
<example>
<description>"hi" will be printed to the console when the player presses the IN_USE (E) key.</description>
<code>
hook.Add( "KeyPress", "keypress_use_hi", function( ply, key )
if ( key == IN_USE ) then
print( "hi" )
end
end )
</code>
<outputfixedwidth>Fixed width</outputfixedwidth>
<output>hi</output>
</example>
<example>
<description>When a player tries to jump, they will be shot straight up in the air.</description>
<code>
hook.Add( "KeyPress", "keypress_jump_super", function( ply, key )
if ( key == IN_JUMP ) then
ply:SetVelocity( ply:GetVelocity() + Vector( 0, 0, 1000 ) )
end
end )
</code>
</example>