How to not trigger the script multiple times when a key is held?

Ask gaming related questions (AHK v1.1 and older)
Merudo
Posts: 2
Joined: 02 Mar 2022, 04:47

How to not trigger the script multiple times when a key is held?

Post by Merudo » 02 Mar 2022, 04:53

I want a script to trigger when the key is pressed, but not when it is held.

For example,

Code: Select all

9::
{
...
}
will trigger once on when "9" is first pressed, then will trigger continuously after if "9" is held - the later I do not want.

Is there a clean way for the script to ignore a key being held down?

Basically, I have the opposite problem of this lol: https://www.autohotkey.com/board/topic/87990-hold-down-key-to-trigger-script/
Last edited by BoBo on 02 Mar 2022, 05:11, edited 1 time in total.
Reason: Moved to Gaming section.

User avatar
boiler
Posts: 16931
Joined: 21 Dec 2014, 02:44

Re: How to not trigger the script multiple times when a key is held?

Post by boiler » 02 Mar 2022, 05:11

Use KeyWait:

Code: Select all

9::
	SoundBeep ; your code in place of this line
	KeyWait, 9
return

Using { } is not how you define the start and end of a hotkey routine (except in AHK v2 where they are functions). Remove the braces and end with a return as shown above.

Merudo
Posts: 2
Joined: 02 Mar 2022, 04:47

Re: How to not trigger the script multiple times when a key is held?

Post by Merudo » 02 Mar 2022, 06:52

boiler wrote:
02 Mar 2022, 05:11
Use KeyWait:

Code: Select all

9::
	SoundBeep ; your code in place of this line
	KeyWait, 9
return

Using { } is not how you define the start and end of a hotkey routine (except in AHK v2 where they are functions). Remove the braces and end with a return as shown above.
Thanks! This work for some of my scripts but not others.

For example if I have a loop that a keyboard press toggles, it won't work:

Code: Select all

9::
	T := !T
	if(T){
		SetTimer, Loop, -1
	}
	KeyWait, 9
Return

Loop:
While T
	{
	SoundBeep ;  your code in place of this line
	}
Return
With "KeyWait", the loop is successfully triggered, but cannot be stopped. Removing "KeyWait" fixes it, but then holding "9" rapidly turns the loop on and off.

User avatar
boiler
Posts: 16931
Joined: 21 Dec 2014, 02:44

Re: How to not trigger the script multiple times when a key is held?

Post by boiler » 02 Mar 2022, 08:55

It's because your While loop has taken control of the code flow, so it never returns to the hotkey subroutine to continue to the KeyWait line. You could add #MaxThreadsPerHotkey 2 at the top so that another press of the 9 hotkey will interrupt and toggle T, but it won't behave as you would like while you hold down the 9 key. If you take those kinds of things into account, you should be able to devise the logic necessary to do what you want.

Dasher
Posts: 2
Joined: 04 Jul 2022, 19:09

Re: How to not trigger the script multiple times when a key is held?

Post by Dasher » 04 Jul 2022, 19:13

boiler wrote:
02 Mar 2022, 08:55
It's because your While loop has taken control of the code flow, so it never returns to the hotkey subroutine to continue to the KeyWait line. You could add #MaxThreadsPerHotkey 2 at the top so that another press of the 9 hotkey will interrupt and toggle T, but it won't behave as you would like while you hold down the 9 key. If you take those kinds of things into account, you should be able to devise the logic necessary to do what you want.


so i have this script

Code: Select all

9::
   SoundBeep ; #NoEnv
#SingleInstance, Force
bWheel := False
Period := 250
Return ; end of auto-execute section


Wheeler: ; timer controlled
    ToolTip, %Period%, 0, 0
    Send, {%Key%}
Return


;-------------------------------------------------------------------------------
#If Not bWheel ; context
;-------------------------------------------------------------------------------

    WheelUp::   ; start auto-scrolling
    WheelDown:: ; start auto-scrolling
        bWheel := True
        Key := A_ThisHotkey
        SetTimer, Wheeler, %Period%
    Return


;-------------------------------------------------------------------------------
#If bWheel ; other context
;-------------------------------------------------------------------------------

    WheelUp::
    WheelDown::
        If (Key = A_ThisHotkey) {   ; same direction -> go faster
            If (Period -= 50) < 50
                Period := 50
        } Else                      ; other direction -> go slower
            If (Period += 50) > 1000
                Period := 1000
        SetTimer, Wheeler, %Period%
    Return

    Esc:: ; stop auto-scrolling
        bWheel := False
        ToolTip ; off
        SetTimer, Wheeler, Off
    Return


;-------------------------------------------------------------------------------
#If ; end of context
;-------------------------------------------------------------------------------
[Mod edit: [code][/code] tags added.]

it auto scrolls but whenever i press 9 again it never stops, why? You can also change the script to fix it but dont touch anything MAIN. So please fix this. I am new to coding.

User avatar
boiler
Posts: 16931
Joined: 21 Dec 2014, 02:44

Re: How to not trigger the script multiple times when a key is held?

Post by boiler » 04 Jul 2022, 21:45

I don't see why you're expecting 9 to stop it. It's the Esc hotkey that is meant to stop auto-scrolling. Even the comment says that.

Dasher
Posts: 2
Joined: 04 Jul 2022, 19:09

Re: How to not trigger the script multiple times when a key is held?

Post by Dasher » 05 Jul 2022, 07:36

boiler wrote:
04 Jul 2022, 21:45
I don't see why you're expecting 9 to stop it. It's the Esc hotkey that is meant to stop auto-scrolling. Even the comment says that.
oh damn im stupid

Post Reply

Return to “Gaming Help (v1)”