There is a way to detect how long a key is pressed?

Discuss any general programming issues here
Post Reply
ilbarbax
Posts: 81
Joined: Thu Apr 01, 2010 6:41 pm

There is a way to detect how long a key is pressed?

Post by ilbarbax » Fri Mar 08, 2019 2:11 pm

A keyboard key or a mouse key.

thanks

Bugala
Posts: 715
Joined: Sun Feb 14, 2010 7:11 pm

Re: There is a way to detect how long a key is pressed?

Post by Bugala » Fri Mar 08, 2019 5:33 pm

I quess no direct command for that, but you can program it:

Code: Select all

EscapeQuit(True)
StartTimer(1)
Repeat
   If IsLeftMouse() = True
	ResetTimer(1)
        endloop=False
	Repeat
		If IsLeftMouse() = False Then endloop=True
	Until endloop=True
	timeittook = GetTimer(1)
	DebugPrint("left mouse button was down for "..timeittook.." milliseconds")
   EndIf
Forever

User avatar
jPV
Posts: 211
Joined: Sat Mar 26, 2016 10:44 am
Location: RNO
Contact:

Re: There is a way to detect how long a key is pressed?

Post by jPV » Sat Mar 09, 2019 11:48 am

Although the previous example has some busy loops which you should always avoid :) Add at least some waits there...

Or you could do it by events like this:

Code: Select all

Function p_Input(msg)
    Switch msg.Action
        Case "OnMouseDown":
            StartTimer(1)
        Case "OnMouseUp":
            DebugPrint("Mouse was pressed for " .. GetTimer(1) .. " milliseconds.")
            StopTimer(1)
        Case "OnKeyDown":
            StartTimer(2)
        Case "OnKeyUp":
            DebugPrint("A key was pressed for " .. GetTimer(2) .. " milliseconds.")
            StopTimer(2)
    EndSwitch
EndFunction

EscapeQuit(True)
InstallEventHandler({OnMouseDown = p_Input, OnMouseUp = p_Input, OnKeyDown = p_Input, OnKeyUp = p_Input})

Repeat
    WaitEvent
Forever

User avatar
airsoftsoftwair
Posts: 2832
Joined: Fri Feb 12, 2010 2:33 pm
Location: Germany
Contact:

Re: There is a way to detect how long a key is pressed?

Post by airsoftsoftwair » Sun Mar 10, 2019 3:55 pm

+1 for jPV's solution :)

ilbarbax
Posts: 81
Joined: Thu Apr 01, 2010 6:41 pm

Re: There is a way to detect how long a key is pressed?

Post by ilbarbax » Mon Mar 11, 2019 1:51 pm

It works thanks

Post Reply