KeyWait former:ErrorLevel now:?

Get help with using AutoHotkey (v2 or newer) and its commands and hotkeys
Nod1234
Posts: 62
Joined: 13 Dec 2021, 12:10

KeyWait former:ErrorLevel now:?

Post by Nod1234 » 02 Feb 2023, 05:15

Hey, i'm struggling with the 2.0 syntax espacally with the Error handling from KeyWait:

Code: Select all

KeyWait "c", "T.5"
	if KeyWait==0 ;former: ErrorLevel
The task of the code should be:
press c --> immedatly send c
hold c --> normal behaviour of c-button (means: ccccccc, each f.e. 0.5s)
double tab c --> send cr
tripple tab c --> send crr

Code: Select all

SendMode "Input"
SetKeyDelay 1
#Requires AutoHotkey v2.0

Global title := "AutoHotkey vers.: " A_AhkVersion
Global cKeyCount := 0
global SoundFreq := 600
global SoundDur := 100

;tooltip title

c::
{
SoundBeep SoundFreq, SoundDur
	if (cKeyCount > 0) 
	{
	Global cKeyCount += 1
	} 
	else 
	{
	SendEvent "{c}" ;immediatly send c
	Global cKeyCount := 1
	KeyWait "c", "T.5"
	if KeyWait==0 ;former: ErrorLevel if c is hold down, send c each 0.5s
	{
		while (GetKeyState("c", "P")) 
		{
			SendEvent "{c}"
			SoundBeep SoundFreq*2, SoundDur/2
		}
	}
	SetTimer cKey, -350
	}

	cKey() ;double tab c = cr, trippe tab c = crr - input time window 350ms
	{
		SendEvent (cKeyCount == 2 ? "{r}" : cKeyCount == 3 ? "{r 2}" : "")
		Global cKeyCount := 0
	}
}
Do you have an idea to fix my issue? Thanks

User avatar
mikeyww
Posts: 26871
Joined: 09 Sep 2014, 18:38

Re: KeyWait former:ErrorLevel now:?

Post by mikeyww » 02 Feb 2023, 08:08

Code: Select all

; This script records a key hold, or multiple consecutive presses
#Requires AutoHotkey v2.0
$c:: { 
 Static presses := 0, squelch := 500
 hk := StrReplace(ThisHotkey, "$")    ; Strip the dollar sign
 If KeyWait(hk, 'T.5')                ; If key was quickly released,
  SetTimer(done, -squelch), presses++ ;  then reset timer, and increment counter
 Else Send(hk), presses := -1         ; If held, send the key
 done() {                             ; Key presses have ended
  Switch presses {
   Case 1: Send 'c'                   ; Single
   Case 2: Send 'cr'                  ; Double
   Case 3: Send 'crr'                 ; Triple
  }
  presses := 0
 }
}

Nod1234
Posts: 62
Joined: 13 Dec 2021, 12:10

Re: KeyWait former:ErrorLevel now:?

Post by Nod1234 » 02 Feb 2023, 09:42

Thank you for your answer, but i feel a delay when i hit c once quick and i also feel a delay by spamming c if c-key is hold down...
May it helps for understanding if i give you the original 1.1 code?

Code: Select all

cKeyCount := 0

#If (!GetKeyState("LButton", "P")) ;&& editStateOn = False)
c::
   if (cKeyCount > 0)
       cKeyCount += 1
   else
   {
       SendEvent {c}
       cKeyCount := 1
       KeyWait, c, T0.5
       if (ErrorLevel)
           while GetKeyState("c", "P")
               SendEvent {c}         
       SetTimer, cKey, -350
   }
Return
	cKey:
   SendEvent, % cKeyCount = 2 ? "r" : cKeyCount = 3 ? "{r 2}" : ""
   cKeyCount := 0
	Return
Return
#If
Last edited by Nod1234 on 02 Feb 2023, 09:44, edited 1 time in total.

Nod1234
Posts: 62
Joined: 13 Dec 2021, 12:10

Re: KeyWait former:ErrorLevel now:?

Post by Nod1234 » 02 Feb 2023, 09:42

the difference to your 2.0-code is defenitly the response.

User avatar
mikeyww
Posts: 26871
Joined: 09 Sep 2014, 18:38

Re: KeyWait former:ErrorLevel now:?

Post by mikeyww » 02 Feb 2023, 09:54

If you don't like the delay, then you can backspace if needed.

Code: Select all

; This script records a key hold, or multiple consecutive presses
#Requires AutoHotkey v2.0
$c:: { 
 Static presses := 0, squelch := 350
 hk := StrReplace(ThisHotkey, "$")    ; Strip the dollar sign
 Send 'c'
 If !KeyWait(hk, 'T.5') {
  While GetKeyState(hk, "P")
   SendEvent hk
  done()
 } Else SetTimer(done, -squelch), presses++
 done() {                             ; Key presses have ended
  Switch presses {
   Case 2: Send '{BS}r'               ; Double
   Case 3: Send '{BS 2}rr'            ; Triple
  }
  presses := 0
 }
}

Nod1234
Posts: 62
Joined: 13 Dec 2021, 12:10

Re: KeyWait former:ErrorLevel now:?

Post by Nod1234 » 02 Feb 2023, 10:16

Thank you! The response is great, i removed BS, so that it sends now ccr (on double tap) and cccrr (on tripple tab), its not exactly that what i want but it should work in game also. I can live with that!

Post Reply

Return to “Ask for Help (v2)”