Jump to content

Calculate time how long player have pressed a key.


HePezi

Recommended Posts

You listen for key down and up events. Store start time (getTickCount() - time in milliseconds) on 'down'. And on 'up' check if current tick count is >= 2000 milliseconds (assuming more than 2 seconds is also ok - you can add some buffer zone, like between 2 and 3 seconds).

 

UPDATE: Ah, you probably meant knowing two seconds has passed even before releasing the key. Then you simply call https://wiki.multitheftauto.com/wiki/SetTimer on the 'down' event. You have to be careful with timers though. If user clicks down many times during the 2 seconds, the old timers have to be canceled or ignored.

Edited by vx89
  • Thanks 1
Link to comment

I think this server-side code should work:

bindKey( player, key, "both", funcInput )

function funcInput ( player, key, keyState )
	if key == "key" and keyState == "down" then
		if timer then return end

		timer = setTimer( f, 2000, 1, player )
		--outputChatBox("down")
	elseif key == "key" and keyState == "up" then
		if timer and isTimer(timer) then
			--did not wait 2 seconds
		end
		--outputChatBox("up")
	end
end

function f( p )
	killTimer(timer)
	timer = nil

end

 

  • Thanks 1
Link to comment

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...