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

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

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

Post by ilbarbax »

A keyboard key or a mouse key.

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

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

Post by Bugala »

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: 603
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 »

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: 5443
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 »

+1 for jPV's solution :)
ilbarbax
Posts: 114
Joined: Thu Apr 01, 2010 6:41 pm

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

Post by ilbarbax »

It works thanks
Post Reply