Jump to content

Sky Slate Blueberry Blackcurrant Watermelon Strawberry Orange Banana Apple Emerald Chocolate
Photo

How to detect a key being held down for long?


  • Please log in to reply
3 replies to this topic
leuce
  • Members
  • 28 posts
  • Last active: Sep 29 2014 01:27 PM
  • Joined: 17 May 2006
G'day everyone

Is there a way for a script to detect that a certain key is being held down for a certain period of time? I want a certain action to happen when the user holds down the spacebar for more than 10 seconds.

The action doesn't have to happen before the user releases the spacebar (that would be another question, namely whether the script can make the action happen before the user releases the spacebar).

Thanks in advance
Samuel

Terrapin
  • Members
  • 107 posts
  • Last active: Feb 06 2007 03:38 PM
  • Joined: 15 Aug 2005
Hi, I came up with this little bit of code which might help you:


~Space::
If spaceIsPressed
	return
spaceIsPressed := true
SetTimer, WaitForRelease, 3000		; 3 seconds
return

~Space Up::
SetTimer, WaitForRelease, Off
spaceIsPressed := false
return

WaitForRelease:
SetTimer, WaitForRelease, Off
MsgBox, You held down the spacebar for three seconds....
return

Note the tilde before the hotkeys. It allows the spacebar press to go through (not trapped). This may or may not be what you want. Look in the manual (help file) under Hotkeys, and settimer, for more info.

Good Luck,

Bob

leuce
  • Members
  • 28 posts
  • Last active: Sep 29 2014 01:27 PM
  • Joined: 17 May 2006

Hi, I came up with this little bit of code which might help you...


Neat! The script has an interesting side-effect... the fact that your example uses a simple MsgBox instead of a more complex one means that when the user releases the spacebar, it also dismisses the message box :-)

Hardeep
  • Members
  • 87 posts
  • Last active: Sep 12 2007 06:40 PM
  • Joined: 02 Jul 2006
Hi leuce,

I think this is exactly what you want...hope this helps! :)
RWin::
Duration=0
Loop
{	Duration ++
	If !GetKeyState("RWin","P")
		Break
	If Duration > 10
	{	;UnCommenting the following line will wait for user to release the key even if 10 secs have passed
		;KeyWait,RWin
		Msgbox Your action here
		break
	}
	Sleep, 1000
}
return

Rwin=Right Windows Key :wink: