KEY Enumerations

At this page:

you can see those KEYS like KEY_F which is “F”.
**
And there are those:**

you can see those ACTIONS like IN_ATTACK which is “the left mouse button”.

My question is how can I use the KEY Enumerations as a function like those IN Enumerations
As we can see those IN Enumerations can use as example


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

And I want to use a key from the KEY Enumerations to check if that key is pressed like “F” then it should run something. And maybe have a cind function like the flashlight like it checks if its on or off.

Inside a think hook, use

input.IsKeyDown and have a small check to prevent spamming (if needed).

Example with 0.5 second spam prevention:
[lua]
local key_down_timer = CurTime()

local key_down_delay = 0.5

hook.Add( “Think”, “iskeydown_example”, function()

if ( input.IsKeyDown( KEY_F ) and key_down_timer <= CurTime() ) then

	-- Do your stuff here

	key_down_timer = CurTime() + key_down_delay -- reset the timer

end

end )
[/lua]

Let’s say I press it again and I want that it does another function what I do?

And if I press it again later it should do the first one.

Like different step like click 1 it does that function, click 2 it does another function and then it goes back and is this also possible with more clicks? like click 3?

Make a boolean that toggles it

ugh?

Bruh…

[lua]
local key_down_timer = CurTime()
local key_down_delay = 0.5
local key_down_toggle = key_down_toggle or true – prevents auto-refresh resetting it

hook.Add( “Think”, “iskeydown_example”, function()

if ( input.IsKeyDown( KEY_F ) and key_down_timer <= CurTime() ) then

	key_down_timer = CurTime() + key_down_delay
	
	if key_down_toggle then
		
		-- Scenario 1
	else
	
		-- Scenario 2
	end
	
	key_down_toggle = !key_down_toggle -- Toggles the boolean

end

end )
[/lua]

Not that…

When I don’t hit the key it will do scenario 2.

I meant to when I hit it AGAIN then it should do scenario 2 and when I hit it again go back to scenario 1

Thats literally what it does

Simplified example:

[lua]

local example = example or true

local function toggle()
if example then
print(“Scenario 1”)
example = false
else
print(“Scenario 2”)
example = true
end
end

for i=1, 4 do
print( toggle() )
end
[/lua]

Output:
Scenario 1
Scenario 2
Scenario 1
Scenario 2

You can test it here https://www.lua.org/cgi-bin/demo

You can use the PlayerButtonDown hook which uses KEY enums as well.

Oh wait this runs scenario 1 and scenario 2 togheter but I want to make a cind of boolean.