Can anyone review this code, and maybe see why it's not working consistently?

Ask gaming related questions (AHK v1.1 and older)
TiesA
Posts: 4
Joined: 06 May 2021, 13:05

Can anyone review this code, and maybe see why it's not working consistently?

14 May 2021, 18:56

Hi,

I've got an issue with my script. In terms of functionality, it does exactly what I wants most of the time, but unfortunately it isn't 100% consistent.
I am terrible at AHK, so I am likely doing something wrong with syntax

The functionality I wanted out of my script are as follow:
(Note: I've Remapped mouse keys to keyboard keys due to the game not recognizing the script when I used mouse keys)
  • XButton1/Mouse Button 4 rebound as key p
  • Clicking the RButton sends down key p, then after a short delay sends down key o
  • Clicking the RButton also disables Xbutton1, to prevent XButton1 release (= key p release)
  • When RButton is released, send o back up. Then, before sending p back up, check if the XButton1 is now being held down as well
  • If XButton1 is being held down, remove the disable so that XButton1 release sends p up.
  • If XButton1 was not held when the RButton was released, p can be send up
Basically, I want mouse button 4 to act like p, and right click to act as p+o, without them interfering with eachother and being able to alternate between pressing them.
The script accomplishes this, but the issue is that sometimes a RButton press does not seem to send down p+o down correctly, and it only sends down p and gets stuck there. This happens about 1/10 times

This is my current script:

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.


f12::
Suspend,Toggle
Soundbeep 1000
Return

*LShift::l				; rebind

*LAlt::0				; rebind

XButton1Disabled := 0
#if XButton1Disabled == 0		; if not disabled, make mouse button act like p
*XButton1::p

#if XButton1Disabled == 1		; if disabled, do nothing
*XButton1::return
#if

*RButton::
XButton1Disabled := 1  			; disable side mouse button after clicking the right mouse button
Send {p down}
Sleep, 10
Send {o down}
KeyWait, RButton 			; wait until right mouse button is released
Send {o up}
   if GetKeyState("XButton1", "p") {	; check if the side button is being held by now
	XButton1Disabled := 0		; if held, lift up the disable and do nothing
	return
	} else {
	Send {p up}			; if not held, send p up and lift the disable afterwards
	XButton1Disabled := 0
	}
return
Any help would be greatly appreciated :D
User avatar
mikeyww
Posts: 26847
Joined: 09 Sep 2014, 18:38

Re: Can anyone review this code, and maybe see why it's not working consistently?

14 May 2021, 20:57

This is similar to yours, but perhaps the Up routine helps.

Code: Select all

RButton::
disable := True
Send {p down}
Sleep, 100
Send {o down}
KeyWait, RButton
Send % "{o up}" ((disable := !GetKeyState("XButton1", "P")) ? "{p up}" : "")
Return

XButton1::
Send {p down}
SoundBeep, 1500
Return
XButton1 Up::
Send {p up}
SoundBeep, 1000
Return
#If disable
XButton1 Up::Return
#If
TiesA
Posts: 4
Joined: 06 May 2021, 13:05

Re: Can anyone review this code, and maybe see why it's not working consistently?

15 May 2021, 06:41

    Hi Mikey,

    Thanks for your reply!

    Your version of the script seems to handle the RButton behaviour I want well, but something weird is happening when I press the XButton1.


    It seems that when I press the XButton1, XButton1 is only being send Up when, while still holding down the XButton1, I click and release the RButton and thén release the XButton1 afterwards
    If I just press the XButton1 by itself, it seems to get stuck on down (which means p is being pressed down constantly).
    In this state, further presses of XButton1 don't do anything (no soundbeeps, p still pressed); the only way to set p up again and reset is to quickly press and release the RButton


    I'm way to novice to AHK to understand this line, could you maybe explain what you did here so I could fix the above issue?

    Code: Select all

    Send % "{o up}" ((disable := !GetKeyState("XButton1", "P")) ? "{p up}" : "")
    User avatar
    mikeyww
    Posts: 26847
    Joined: 09 Sep 2014, 18:38

    Re: Can anyone review this code, and maybe see why it's not working consistently?

    15 May 2021, 08:21

    Sure. Well, you did say, "Clicking the RButton also disables Xbutton1, to prevent XButton1 release (= key p release)"-- so that is what you have! Nonetheless, I can see that you want something different and, yes, it is probably in the line that you cited. This line sets disable according to the XButton key state, but perhaps you instead want to set disable to False at all times after the RButton is released. You can decide. The line you cited uses the ternary operator, which is like "If... then... else...". If XButton1 is physically released (not pressed) at that very moment, the key state is 0, so disable = 1. Therefore {p up} is sent. Otherwise, nothing is sent.

    Return to “Gaming Help (v1)”

    Who is online

    Users browsing this forum: mamo691 and 45 guests