Execute script while key pressed Topic is solved

Ask gaming related questions
Asteriii
Posts: 3
Joined: 01 Apr 2023, 13:19

Execute script while key pressed

Post by Asteriii » 01 Apr 2023, 14:09

Hello, I'm new to AHK and trying to make script that repeat sequence of keys while RAlt is down and instantly stop when RAlt is up. But the script doing something wrong and I don't understand what, can someone help me with that?

Code: Select all

WinActive ("ahk_exe Game.exe")
#SingleInstance
RAlt::  {
  While GetKeyState(ThisHotkey, "P")
        Send "{Space}" 
        sleep 50
        Send "{Space}"
        sleep 50
        Send "{RButton down}"
        sleep 50
        Send "E"
        sleep 50
        Send "["
        sleep 100
        Send "]"
        sleep 50
        Send "{RButton up}"
        sleep 30
        Send "{LButton down}"
        sleep 100
        Send "{LButton up}"    
        }
return

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

Re: Execute script while key pressed  Topic is solved

Post by mikeyww » 01 Apr 2023, 15:34

Welcome to this AutoHotkey forum!

Use a :arrow: block.

Code: Select all

#Requires AutoHotkey v2.0

RAlt:: {
 While GetKeyState(ThisHotkey, "P") {
  Send 123
  Sleep 50
 }
}

Asteriii
Posts: 3
Joined: 01 Apr 2023, 13:19

Re: Execute script while key pressed

Post by Asteriii » 01 Apr 2023, 19:10

Thanks!

Asteriii
Posts: 3
Joined: 01 Apr 2023, 13:19

Re: Execute script while key pressed

Post by Asteriii » 01 Apr 2023, 19:54

Also i got a problem with if WinActive, it gives me this error. Tried to change it, but couldn't get it to work

Code: Select all

If WinActive ("ahk_exe Game.exe")
`::  {
  While GetKeyState(ThisHotkey, "P") {
    Send "{Space}" 
    Sleep 50
   }
}
Error: Unexpected function

001: If WinActive ("ahk_exe Warframe.x64.exe")
▶ 002: {
003: While GetKeyState(ThisHotkey, "P")
003: {

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

Re: Execute script while key pressed

Post by mikeyww » 02 Apr 2023, 05:02

Hotkeys cannot be defined as part of a subroutine or If statement in this manner, but you can use :arrow: #HotIf to do it.

Function names must always be followed by (, not a space. WinActive is a function.

Post Reply

Return to “Gaming”