Press a key twice while RButton is held down

Ask gaming related questions (AHK v1.1 and older)
SKDN
Posts: 8
Joined: 20 Mar 2023, 10:49

Press a key twice while RButton is held down

Post by SKDN » 20 Mar 2023, 11:11

Hi guys,

I'm a total noob and need help with the following script I found.

Code: Select all

*RButton Up::

If (Toggle := !Toggle){

Send {Click Down Right}
}
Else{
Send {RButton up}
}

Return
	
~$*XButton1::    

if (Toggle) {
	Toggle := False
	send {RButton Up}
}
return

*Ins::ExitApp
This is a "toggle ads" script which allows for toggle aim in games not supporting this feature.
It works by pressing RButton once to aim and pressing it once again to cancel aiming instead of holding RButton down during the aim process.
Alternatively I can cancel aiming by pressing XButton1 (my sprint key).
I have to press XButton1 again to sprint though.
What I want: Only in case rbutton is held down, pressing XButton1 will trigger pressing XButton1 twice,
so I can cancel aiming and start sprinting with a single keystroke.
Can someone help?


[Mod action: Topic moved from "Gaming" section of "Ask for Help (v2)" since this is v1 code.]

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

Re: Press a key twice while RButton is held down

Post by mikeyww » 20 Mar 2023, 16:30

Welcome to this AutoHotkey forum!

Code: Select all

#Requires AutoHotkey v1.1.33

XButton1::
reset()
Click X1
Return

RButton Up::
If !reset() {
 Click R D
 SoundBeep 1500
}
Return

reset() {
 If GetKeyState("RButton") {
  Click R U
  SoundBeep 1000
  Return True
 }
}

SKDN
Posts: 8
Joined: 20 Mar 2023, 10:49

Re: Press a key twice while RButton is held down

Post by SKDN » 20 Mar 2023, 20:45

Soundbeep? You trolling me here?

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

Re: Press a key twice while RButton is held down

Post by mikeyww » 20 Mar 2023, 20:51

I'm not trolling anyone here. You are free to delete the beep if you don't like it. While you are at it, you could also report whether the script works.

SKDN
Posts: 8
Joined: 20 Mar 2023, 10:49

Re: Press a key twice while RButton is held down

Post by SKDN » 21 Mar 2023, 09:04

Thank you for your effort! I included your script into the existing one but unfortunatly I still need to press XButton1 2 times, in some cases even more (like 6 times) to sprint after aiming.
It looks as follows:

Code: Select all

*RButton Up::

If (Toggle := !Toggle){
Send {Click Down Right}
}
Else{
Send {RButton up}
}
Return

XButton1::
reset()
Click X1
Return

RButton Up::
If !reset() {
 Click R D
 }
Return

reset() {
 If GetKeyState("RButton") {
  Click R U
  Return True
 }
}

~$*XButton1::
if (Toggle) {
	Toggle := False
	send {RButton Up}
}
return

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

Re: Press a key twice while RButton is held down

Post by mikeyww » 21 Mar 2023, 09:54

Before you modify the script, try the script that I posted, with no changes, no other code, and no other scripts running. That will tell you whether you have a working starting point.

SKDN
Posts: 8
Joined: 20 Mar 2023, 10:49

Re: Press a key twice while RButton is held down

Post by SKDN » 21 Mar 2023, 10:28

Tested it: Toggle aim works, cancel aiming with XButton1 works, but it still takes several keystrokes to sprint again, in some cases I have to press the key 4 times.

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

Re: Press a key twice while RButton is held down

Post by mikeyww » 21 Mar 2023, 10:48

Is pressing XButton1 supposed to initiate the sprint?

SKDN
Posts: 8
Joined: 20 Mar 2023, 10:49

Re: Press a key twice while RButton is held down

Post by SKDN » 21 Mar 2023, 11:09

Yes, XButton1 is my sprint button. With my current script it goes as follows: I press RButton once to toggle aim, to initiate the sprint I press XButton1 to cancel aiming and press it again to trigger the actual sprint. The way I want it to be: I press RButton once to toggle aim, to initiate sprinting I press XButton1 once to both cancel aiming and sprint immediately afterwards.
In order to work properly XButton1 has to be sent twice by pressing it while RButton is active and once when RButton isn't active.

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

Re: Press a key twice while RButton is held down

Post by mikeyww » 21 Mar 2023, 11:22

OK. The script that I posted should be doing that. It issues the button click after resetting the RButton. You can try increasing the :arrow: SetKeyDelay (delay and press duration) and SetMouseDelay. A ballpark target would be 25 (ms) for all of them.

Others have found some of the other tips useful on the following page. viewtopic.php?f=7&t=11084

Those are my only ideas. Others here may know more.

Below is an option to test a longer button press duration.

Code: Select all

#Requires AutoHotkey v1.1.33

XButton1::
SetKeyDelay ,, PRESSDURATION := 25
reset()
Sleep 25
Send {XButton1}
Return

RButton Up::
If !reset() {
 Click R D
 SoundBeep 1500
}
Return

reset() {
 If GetKeyState("RButton") {
  Click R U
  SoundBeep 1000
  Return True
 }
}

SKDN
Posts: 8
Joined: 20 Mar 2023, 10:49

Re: Press a key twice while RButton is held down

Post by SKDN » 22 Mar 2023, 09:41

You nailed it, it works! Only one game seems to have issues since Xbutton won't be recognized on every single press but it doesn't matter. I have a last modest request: Like in my old script a series of other keys like R, LShift, Ctrl, 1, 2, 3 etc. would cancel aiming (when RButton is down). I tried to do it by myself but it doesn't seem to work:

Code: Select all

R::
reset()
send {R}
return
The command "reset()" cancels all ongoing inputs, do I get it right?

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

Re: Press a key twice while RButton is held down

Post by mikeyww » 22 Mar 2023, 10:07

The reset function in the script will "release" the RButton if it is in a down state when the function is called.

You can fiddle with the delay times, increase them, etc.

You have shown an example of a subroutine that "sends its own hotkey". This typically requires using the hook to avoid an unwanted self-triggering loop. You can force the hotkey to use the hook by adding a $ prefix to the hotkey's name ($r::). Mouse buttons do not require this because mouse buttons always use the hook automatically.

Explained: Dollar

Code: Select all

$r::
Send abc
Send r
Return

SKDN
Posts: 8
Joined: 20 Mar 2023, 10:49

Re: Press a key twice while RButton is held down

Post by SKDN » 22 Mar 2023, 11:21

It's weird because it works in Windows, but doesn't in games.
I tried as follows:

Code: Select all

$r::
reset()
sleep 20
send r
Return
In games it triggers RButton release but doesn't trigger itself. So no matter how often I press "R", I can't reload.

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

Re: Press a key twice while RButton is held down

Post by mikeyww » 22 Mar 2023, 11:44

Those pesky games! Always misbehaving!

Can increase the key press duration, try admin mode, etc. See what worked for others.

SKDN
Posts: 8
Joined: 20 Mar 2023, 10:49

Re: Press a key twice while RButton is held down

Post by SKDN » 22 Mar 2023, 13:02

Finally got it!

Code: Select all

$r::
SetKeyDelay ,, PRESSDURATION := 50 
reset()
sleep 20
send r
As you mentioned, the delays have to be tuned in every single game, but it works an I finally can play unbound. Thank you so much!

Post Reply

Return to “Gaming Help (v1)”