Hold key if window is active Topic is solved

Ask gaming related questions (AHK v1.1 and older)
Denizen 38
Posts: 6
Joined: 16 May 2022, 16:43

Hold key if window is active

Post by Denizen 38 » 20 May 2022, 12:36

Hi all,

Seemingly simple task here but I can't quite figure it out (am quite a novice with AHK). How do I create a script that holds down a key simply by virtue of a window being active? I'm already using #IfWinActive to rebind various other keys here, but I can't figure out how to just have a persistent "key down" action without assigning to some kind of physical key press on my part first.

Any help will be appreciated.

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

Re: Hold key if window is active  Topic is solved

Post by mikeyww » 20 May 2022, 13:21

Code: Select all

#SingleInstance Force
Loop {
 WinWaitActive, ahk_exe notepad.exe
 Send {x down}
 SoundBeep, 1500
 WinWaitNotActive
 Send {x up}
 SoundBeep, 1000
}

Denizen 38
Posts: 6
Joined: 16 May 2022, 16:43

Re: Hold key if window is active

Post by Denizen 38 » 20 May 2022, 15:35

Hey Mikey, thanks for the speedy response. The code you provided does not seem to be working though (tested with Notepad using the code provided and in-game).

Any chance there's something that's been omitted?

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

Re: Hold key if window is active

Post by mikeyww » 20 May 2022, 16:38

Did this script alone work in Notepad, with no other code? It shows a single "x" when you switch to Notepad.

Denizen 38
Posts: 6
Joined: 16 May 2022, 16:43

Re: Hold key if window is active

Post by Denizen 38 » 20 May 2022, 17:06

I was just about to report back that it actually does seem to work when the I ran the script in a completely new file without any other scripts in there.

The issue is that ideally I'd need to get it working as part of my AHK master file which contains various other scripts. Is there any easy fix for this? I can send through the full contents of the master file if that's of help. Nothing super complicated in there, just a list of different #IfWinActive with a handful of remaps for each one.

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

Re: Hold key if window is active

Post by mikeyww » 20 May 2022, 17:23

Sure, post it. As I think you proved, the issue is how you assembled the script. In the meantime, you can read documentation about auto-execute. Unlabeled code following Return is unreachable and will never execute.

Denizen 38
Posts: 6
Joined: 16 May 2022, 16:43

Re: Hold key if window is active

Post by Denizen 38 » 22 May 2022, 10:20

Hey Mikey,

I managed to arrive at a solution that doesn't require any further changes to your code in the end, so marking this as the answer. Thanks again for providing the solution!

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

Re: Hold key if window is active

Post by mikeyww » 22 May 2022, 10:29

Good to hear! :thumbup: :wave:

Post Reply

Return to “Gaming Help (v1)”