Adding show keys to hud

Hey I was wondering how to make it possible to show what players keys are pressed on their hud e.g. on their hud off to the side you can see when they press wasd crouch and space?


Just do an input.IsKeyDown check in your HUDPaint hook. You could use draw.WordBox for the box saying what key, just position it and change the color of it (or something) if your keydown check returns true.

[editline]6th October 2015[/editline]

Here’s some example code with a helper function (cause drawing keys is a bit tedious and repetitive)- it just draws the key ‘W’ in the bottom-right corner (which was done by taking away the width and height of the key from the screen width and height), but it should be easy enough to draw all the keys you want.

hook.Add( 'HUDPaint', 'DrawKeys', function()
	local function DrawKey( enum, key, x, y, w, h, key_color, key_color_if_pressed, key_text_color, font, outline_enabled, outline_color ) -- Helper function
		if input.IsKeyDown( enum ) then
			surface.SetDrawColor( key_color_if_pressed )
			surface.SetDrawColor( key_color )
		surface.DrawRect( x, y, w, h )
		if outline_enabled then
			surface.SetDrawColor( outline_color )
			surface.DrawOutlinedRect( x, y, w, h )
		surface.SetTextColor( key_text_color )
		surface.SetFont( font )
		local textw, texth = surface.GetTextSize( key )
		surface.SetTextPos( x + ( w / 2 ) - ( textw / 2 ), y + ( h / 2 ) - ( texth / 2 ) ) -- This just centres the key
		surface.DrawText( key )
	DrawKey( KEY_W, 'W', ScrW() - 55, ScrH() - 55, 50, 50, Color( 255, 255, 255 ), Color( 255, 0, 0 ), Color( 0, 0, 0 ), 'DermaLarge', true, Color( 0, 0, 0 ) ) -- Draws 'W' in the bottom-right corner
end )

That should save you some time (you can find the key enums here). They’re just what input.IsKeyDown uses to check whether a key is down. They’re pretty self explanatory.

That helps so much! Just one question ! If someone has the key bound to something else, how can I make it still show they pressed W on the hud even though that’s not their binding. Thanks for the help.


Use ply:KeyDown(IN_FORWARD)

I’ve tried messing around with that but all I seem to get is script errors. Where should I place it?

Anywhere where ‘ply’ is defined (As an Entity, who is a player)

In the code I did, change the

if input.IsKeyDown( enum ) then


if LocalPlayer():KeyDown( enum ) then

And make sure the enum you provide to the function is from this list.

That code should then go on clientside beacouse of the locavplayer thing.
As it would be clientside then it’d be better to use input.IsKeyDown

[editline]8th October 2015[/editline]

Also, LocalPlayer doesnt exist on server and that may be why its giving you errors as you may be runing it serverside.
Its the only way i could see the error to happen.

Um… he said he wanted an ALTERNATIVE to input.IsKeyDown if the player had W bound to another key, and since it’s a HUD, of course he would’ve ran it clientside or none of it would’ve worked at all.

Whqt i mean is that he said he was getting an error beacouse of that, and that error may just be that he is running it serverside and the localplayer function is giving an error.
It may be that, or it might not