Do not allow double key being pressed once again

Get help with using AutoHotkey (v1.1 and older) and its commands and hotkeys
Banayat
Posts: 10
Joined: 08 Jul 2018, 14:29

Do not allow double key being pressed once again

17 Aug 2018, 12:12

Hi folks, I am pretty new to all of this. I got a question after hours of trying^^: How can I prevent a mouse key from being pressed more than two times? Also, I got an error message for: "if quickclicks > 0" which says "this variable has not been assigned a value" but quickclicks is declared "1". Any Suggestions? ;)

Code: Select all

;(1) Check if key has been pressed thrice, if so, disable hotkey for 5 seconds
;(2) Check if key has been pressed twice
;(3) Execute windows screenshot hotkey and Gsplit function after waiting 1 second

TheKey:
SetTimer, TheKey, off

quickclicks = 1
SetTimer, TheKey, 579
Return

$XButton2::
; [b](1)[/b]   
   if quickclicks > 0 ; [b]returns an error: >> "this variable has not been assigned a value"[/b]
   {
    quickclicks += 1
    Return
   }
; [b](2) [/b]  
   if A_PriorHotkey <> $XButton2
   {
      KeyWait, XButton2
      return
   }
   if A_TimeSincePriorHotkey > 400
   {
      KeyWait, XButton2
      return
   }
; [b](3)[/b]
   Send, #{PrintScreen}
   Sleep, 1000
   Gsplit()
Return

if quickclicks >= 3
   {
   msgbox, Key got clicked thrice
   hotkey, XButton2, off ; disable mouse hotkey for 1 Minute
   Sleep 5000
   hotkey, XButton2, on ; enable mouse hotkey
   }
quickclicks = 0
Return

Gsplit()
{
msgbox, Key got clicked twice
}
Rohwedder
Posts: 7628
Joined: 04 Jun 2014, 08:33
Location: Germany

Re: Do not allow double key being pressed once again

18 Aug 2018, 03:37

Hallo,
try:

Code: Select all

;(1) Check if key has been pressed thrice, if so, disable hotkey for 5 seconds
;(2) Check if key has been pressed twice
;(3) Execute windows screenshot hotkey and Gsplit function after waiting 1 second
quickclicks = 0
Return
#MaxThreadsPerHotkey 2
$XButton2::
#MaxThreadsPerHotkey 1
if (A_PriorHotkey <> "$XButton2" Or A_TimeSincePriorHotkey > 400)
	quickclicks = 0
quickclicks += 1
If quickclicks = 2
{
	ToolTip, Key got clicked twice
	Sleep, 1000 ; Execute windows screenshot hotkey and Gsplit function after waiting 1 second
	Send, #{PrintScreen}
	Gsplit()
}
if quickclicks >= 3
{
	ToolTip, Key got clicked thrice
	hotkey, $XButton2, off ; disable mouse hotkey for 5 seconds
	SetTimer, XButton2_On, -5000
}
Return
XButton2_On:
quickclicks = 0
hotkey, $XButton2, on ; enable mouse hotkey
ToolTip
Return
Gsplit()
{
ToolTip
}
Banayat
Posts: 10
Joined: 08 Jul 2018, 14:29

Re: Do not allow double key being pressed once again

18 Aug 2018, 06:36

Now I understand what I did wrong at the very beginning. Thank you for your help Rohwedder, it works like a charm :) Now, I wonder why there are two "#MaxThreadsPerHotkey" functions indicated. What if I leave one of it out?
Rohwedder
Posts: 7628
Joined: 04 Jun 2014, 08:33
Location: Germany

Re: Do not allow double key being pressed once again

18 Aug 2018, 09:09

Hallo,
try, comment or delete "#MaxThreadsPerHotkey 2" and you will never get: "Key got clicked thrice".
Because there is the "Sleep, 1000" in the part "Key got clicked twice" one need a second thread.

Return to “Ask for Help (v1)”

Who is online

Users browsing this forum: Anput, Descolada, RandomBoy, ShatterCoder and 303 guests