Differences in between these scripts Topic is solved

Get help with using AutoHotkey (v1.1 and older) and its commands and hotkeys
Prust
Posts: 2
Joined: 24 Jul 2021, 04:49

Differences in between these scripts

24 Jul 2021, 05:59

Hi,
I use AHK mostly for games and trying to learn from other people's solutions. These are two ways that I generally use. Both of them works in notepad since I test them in there first but not always in games and I would like the learn their differences. Can someone explain it to me or point me to where should I look in documentation?

First script:

Code: Select all

IF NOT A_IsAdmin
{
	Run *RunAs "%A_ScriptFullPath%"
	ExitApp
}
#SingleInstance force
#InstallKeybdHook
#UseHook
SetNumlockState On
#NoEnv  ; Recommended for performance and compatibility with future AutoHotkey releases.
;SendMode Input  ; Recommended for new scripts due to its superior speed and reliability.
SetWorkingDir %A_ScriptDir%  ; Ensures a consistent starting directory.
#IfWinActive, ahk_class DeadSpace2WndClass

#MaxThreadsPerHotkey 2
Delayp := "200"

QuickEventKey := "Xbutton1"
Hotkey, %QuickEventKey%, QuickEvent

QuickEvent:
While GetKeyState("Xbutton1", "P")
{
	send, {e down}
	sleep, %Delayp%
	send, {e up}
	sleep, %Delayp%
}
	return
Second script:

Code: Select all

IF NOT A_IsAdmin
{
	Run *RunAs "%A_ScriptFullPath%"
	ExitApp
}
#SingleInstance force
#InstallKeybdHook
#UseHook
SetNumlockState On
#NoEnv  ; Recommended for performance and compatibility with future AutoHotkey releases.
;SendMode Input  ; Recommended for new scripts due to its superior speed and reliability.
SetWorkingDir %A_ScriptDir%  ; Ensures a consistent starting directory.
#IfWinActive, ahk_class DeadSpace2WndClass

#MaxThreadsPerHotkey 2
Delayp := "200"

Xbutton1::
toggle1 := !toggle1
while toggle1
{
	send, {e down}
	sleep, %Delayp%
	send, {e up}
	sleep, %Delayp%
}
	return
I don't use #IfWinActive during testing. First one works in this game but second one is not yet I used second method in some other games. Would it be related to running a game in Fullscreen or Borderless windowed?

Excuse me for asking this part in bad wording since I don't know the correct terminology for this but how

Code: Select all

QuickEventKey := "Xbutton1"
Hotkey, %QuickEventKey%, QuickEvent
QuickEvent:
and

Code: Select all

Xbutton1::
are different from each other in this case.

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

Re: Differences in between these scripts

24 Jul 2021, 08:19

You would normally have Return before your subroutine, but it would rarely matter in the case of this script. The first routine is a key hold; the second is a key toggle.
Prust
Posts: 2
Joined: 24 Jul 2021, 04:49

Re: Differences in between these scripts

25 Jul 2021, 07:04

Thanks for your reply. Even though these subroutines are different from each other as one of them is a key hold and other is a key toggle, my guess is they are interchangeable. So,

Code: Select all

Xbutton1::
While GetKeyState("Xbutton1", "P")
{
	send, {e down}
	sleep, %Delayp%
	send, {e up}
	sleep, %Delayp%
}
also works at least in a notepad.

My lack of understanging is how sometimes using this way works

Code: Select all

abc := "Xbutton1"
Hotkey, %abc%, xyz
xyz:
and other times just this works in a game?

Code: Select all

Xbutton1::
Do you think it shouldn't be and/or because of an user error along the way or they are totaly different?
Thanks
User avatar
mikeyww
Posts: 26937
Joined: 09 Sep 2014, 18:38

Re: Differences in between these scripts  Topic is solved

25 Jul 2021, 07:35

You would normally have Return before your subroutine label (some exceptions for fall-through action).

These are generally equivalent, but if you have "#If" directives in the script, then note that the last such directive will apply to the hotkey defined through Hotkey, unless "Hotkey, If" is used. See documentation for a more detailed explanation.

If you want to enable the hotkey, then I recommend including the "On" option (again, some exceptions apply).

In many cases, coders make mistakes with sequencing directives and hotkeys. Feel free to post your revised script if needed.

If your script works in Notepad but not in your game, then the problem is perhaps in how the game responds to the script.

Return to “Ask for Help (v1)”

Who is online

Users browsing this forum: downstairs, Google [Bot], OrangeCat and 182 guests