Prevent multiple "actions" when action is running Topic is solved

Ask gaming related questions (AHK v1.1 and older)
average_person
Posts: 4
Joined: 28 Sep 2022, 15:12

Prevent multiple "actions" when action is running

Post by average_person » 28 Sep 2022, 15:41

Hi,

I'm kinda new to this and have ran into a problem. As I'm new I don't know the correct terminology so sorry for that.
I'm probably not good enough at searching either as the answers I've found doesn't really fit my scenario so I can't make them work.

Essentially I have a pretty simple script that rebinds WASD to move the mouse to the edge of the screen while the specific key is held down and then back once released.
This script works well enough until I hold more than one of the specified keys down.

As an example lets just use A and W. If I hold down A and let go, everything works as intended. But if I hold down A and then hold down W while A is being held down the "W instance" will trigger and MouseGetPos will "record" the mouse at the position it is currently in (the position when A is being held down).

My solution to this would be to only allow either A or W to be held down but not both at the same time, so if I hold down A and then W it will ignore the W "command" until I let go of A.

I hope my explanation makes sense. Let me know if I should clarify anything.
Thank you for any input or tips on how to search for an answer.

Code: Select all

#NoEnv  ; Recommended for performance and compatibility with future AutoHotkey releases.
; #Warn  ; Enable warnings to assist with detecting common errors.
SendMode Input  ; Recommended for new scripts due to its superior speed and reliability.
SetWorkingDir %A_ScriptDir%  ; Ensures a consistent starting directory.

a::
BlockInput, MouseMove
MouseGetPos, MouseX, MouseY
MouseMove, 8, 540
KeyWait, a
MouseMove, MouseX, MouseY
BlockInput, MouseMoveOff
return

w::
BlockInput, MouseMove
MouseGetPos, MouseX, MouseY
MouseMove, 1000, 8
KeyWait, w
MouseMove, MouseX, MouseY
BlockInput, MouseMoveOff
return

s::
BlockInput, MouseMove
MouseGetPos, MouseX, MouseY
MouseMove, 1000, 1072
KeyWait, s
MouseMove, MouseX, MouseY
BlockInput, MouseMoveOff
return

d::
BlockInput, MouseMove
MouseGetPos, MouseX, MouseY
MouseMove, 1910, 540
KeyWait, d
MouseMove, MouseX, MouseY
BlockInput, MouseMoveOff
return

Esc::ExitApp

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

Re: Prevent multiple "actions" when action is running  Topic is solved

Post by mikeyww » 28 Sep 2022, 19:29

Welcome to this AutoHotkey forum!

Code: Select all

a::
Send x
KeyWait, a
Send y
Return

w::Send 1
#If GetKeyState("a", "P")
w::Return

average_person
Posts: 4
Joined: 28 Sep 2022, 15:12

Re: Prevent multiple "actions" when action is running

Post by average_person » 29 Sep 2022, 01:53

mikeyww wrote:
28 Sep 2022, 19:29
Welcome to this AutoHotkey forum!

Code: Select all

a::
Send x
KeyWait, a
Send y
Return

w::Send 1
#If GetKeyState("a", "P")
w::Return
Thank you Mikey
I'll have to try it when I get home from work.
So I'm guessing the "P" simply states if the button is (P)ressed?

average_person
Posts: 4
Joined: 28 Sep 2022, 15:12

Re: Prevent multiple "actions" when action is running

Post by average_person » 29 Sep 2022, 02:30

Nevermind. I found it now. "Physical state".
Will dive into it a bit and see if I can implement it in my code.

average_person
Posts: 4
Joined: 28 Sep 2022, 15:12

Re: Prevent multiple "actions" when action is running

Post by average_person » 29 Sep 2022, 14:52

Worked like a charm! This was eventually the complete script (for this part):

Code: Select all

#NoEnv  ; Recommended for performance and compatibility with future AutoHotkey releases.
#Warn  ; Enable warnings to assist with detecting common errors.
SendMode Input  ; Recommended for new scripts due to its superior speed and reliability.
SetWorkingDir %A_ScriptDir%  ; Ensures a consistent starting directory.

a::
If GetKeyState("w", "P")
return
If GetKeyState("s", "P")
return
If GetKeyState("d", "P")
return
BlockInput, MouseMove
MouseGetPos, MouseX, MouseY
MouseMove, 8, 540
KeyWait, a
MouseMove, MouseX, MouseY
BlockInput, MouseMoveOff
return

w::
If GetKeyState("a", "P")
return
If GetKeyState("s", "P")
return
If GetKeyState("d", "P")
return
BlockInput, MouseMove
MouseGetPos, MouseX, MouseY
MouseMove, 1000, 8
KeyWait, w
MouseMove, MouseX, MouseY
BlockInput, MouseMoveOff
return

s::
If GetKeyState("a", "P")
return
If GetKeyState("w", "P")
return
If GetKeyState("d", "P")
return
BlockInput, MouseMove
MouseGetPos, MouseX, MouseY
MouseMove, 1000, 1072
KeyWait, s
MouseMove, MouseX, MouseY
BlockInput, MouseMoveOff
return

d::
If GetKeyState("a", "P")
return
If GetKeyState("s", "P")
return
If GetKeyState("w", "P")
return
BlockInput, MouseMove
MouseGetPos, MouseX, MouseY
MouseMove, 1910, 540
KeyWait, d
MouseMove, MouseX, MouseY
BlockInput, MouseMoveOff
return
There are some minor kinks when I start implementing the rest of the script, it's a bit of an overambitious project for me but it's a learning curv.
This part works flawlessly on it's own though :D
Cheers!

Post Reply

Return to “Gaming Help (v1)”