XButton2 press and release help

Get help with using AutoHotkey (v1.1 and older) and its commands and hotkeys
DanielWhy
Posts: 3
Joined: 15 Oct 2022, 06:53

XButton2 press and release help

Post by DanielWhy » 26 Jan 2023, 12:12

Im trying to set my side mouse button to have 2 functions. One when the key is pressed (and hold that action till that key is released), and another when that key is released. I've tried to use {Xbutton2 down} but it doesnt recognize it as a valid key. Any thought?

User avatar
BrockPlaysFortnite
Posts: 84
Joined: 06 Nov 2021, 08:47

Re: XButton2 press and release help

Post by BrockPlaysFortnite » 26 Jan 2023, 16:26

Ok I think I got it. Does this work?

Code: Select all

XButton2::
    While GetKeyState("XButton2","p")     ;while the 5th mouse button is held down
    {
        tooltip testing    ;we will get a loop of the tooltip over and over
    }
    tooltip the key was released    ;then we get out of the loop which means the key was released so you can make a new action on release :D 
    return
Fortnite Youtuber BrockPlaysFortnite :D | AHK completely redefined the way I do things. What used to take me hours to do over and over can now be done with the click of a button. Don't give up! It's worth learning it front to back :wave:
(Note: I'm still a noob so my code may suck sometimes and there is almost assuredly a better way to do it)

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

Re: XButton2 press and release help

Post by mikeyww » 26 Jan 2023, 16:45

Code: Select all

#Requires AutoHotkey v1.1.33
; #Requires AutoHotkey v2.0
XButton2::SoundBeep 1500
XButton2 Up::MsgBox 12345
Concepts:
1. Braces are omitted in specifying a hotkey.
2. Up is a hotkey modifier, but Down is not. Hotkeys without Up are triggered on the press (with some exceptions).

Explained: Introduction and simple examplesUp (has more examples)
Last edited by mikeyww on 26 Jan 2023, 16:49, edited 1 time in total.

User avatar
BrockPlaysFortnite
Posts: 84
Joined: 06 Nov 2021, 08:47

Re: XButton2 press and release help

Post by BrockPlaysFortnite » 26 Jan 2023, 16:49

mikeyww wrote:
26 Jan 2023, 16:45

Code: Select all

#Requires AutoHotkey v1.1.33
; #Requires AutoHotkey v2.0
XButton2::SoundBeep 1500
XButton2 Up::MsgBox 12345
Concepts:
1. Braces are omitted in specifying a hotkey.
2. Up is a hotkey modifier, but Down is not. Hotkeys without Up are triggered on the press.

Explained: Introduction and simple examples
Any idea why this wasn't working?

When I do this, the tooltip works correctly when I let go of Xbutton2

Code: Select all

;~ Xbutton2::
    ;~ tooltip testing down
    
Xbutton2 up::
    tooltip testing up
But when I do this, both tooltips instantly appear back to back on keydown but then also triggers the up tooltip again when I let go

Code: Select all

Xbutton2::
    tooltip testing down
    
Xbutton2 up::
    tooltip testing up
Last edited by BrockPlaysFortnite on 26 Jan 2023, 16:51, edited 1 time in total.
Fortnite Youtuber BrockPlaysFortnite :D | AHK completely redefined the way I do things. What used to take me hours to do over and over can now be done with the click of a button. Don't give up! It's worth learning it front to back :wave:
(Note: I'm still a noob so my code may suck sometimes and there is almost assuredly a better way to do it)

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

Re: XButton2 press and release help

Post by mikeyww » 26 Jan 2023, 16:50

A multi-line hotkey subroutine should end with a Return command. Without Return, the script proceeds to the next statement.

If you read the page that I cited in my post, you can see an example of a typical setup for a hotkey subroutine.

Code: Select all

#Requires AutoHotkey v1.1.33
F3::
Send x
F4::
Send y
Return
What do you believe will happen upon pressing F3?

Another idea is to try my XButton script to see if it works.

User avatar
BrockPlaysFortnite
Posts: 84
Joined: 06 Nov 2021, 08:47

Re: XButton2 press and release help

Post by BrockPlaysFortnite » 26 Jan 2023, 16:55

mikeyww wrote:
26 Jan 2023, 16:50
A multi-line hotkey subroutine should end with a Return command. Without Return, the script proceeds to the next statement.

If you read the page that I cited in my post, you can see an example of a typical setup for a hotkey subroutine.

Code: Select all

#Requires AutoHotkey v1.1.33
F3::
Send x
F4::
Send y
Return
What do you believe will happen upon pressing F3?

Another idea is to try my XButton script to see if it works.
Ahh you're right changing it to single lines works correctly :D I've spent so much time on silly mistakes like that over time

Code: Select all

Xbutton2::tooltip testing down
    
Xbutton2 up::tooltip testing up
Fortnite Youtuber BrockPlaysFortnite :D | AHK completely redefined the way I do things. What used to take me hours to do over and over can now be done with the click of a button. Don't give up! It's worth learning it front to back :wave:
(Note: I'm still a noob so my code may suck sometimes and there is almost assuredly a better way to do it)

DanielWhy
Posts: 3
Joined: 15 Oct 2022, 06:53

Re: XButton2 press and release help

Post by DanielWhy » 31 Jan 2023, 23:18

mikeyww wrote:
26 Jan 2023, 16:45

Code: Select all

#Requires AutoHotkey v1.1.33
; #Requires AutoHotkey v2.0
XButton2::SoundBeep 1500
XButton2 Up::MsgBox 12345
Concepts:
1. Braces are omitted in specifying a hotkey.
2. Up is a hotkey modifier, but Down is not. Hotkeys without Up are triggered on the press (with some exceptions).

Explained: Introduction and simple examplesUp (has more examples)
I did this but changed Soundbeep 1500 to 3 and MsgBox 12345 to l, Which is what i want my hotkeys to be. Yet I get an error saying Duplicate hotkey *XButton2 Up. Any ides on it maybe i just missed something. Your code worked perfectly till i changed the actions.

DanielWhy
Posts: 3
Joined: 15 Oct 2022, 06:53

Re: XButton2 press and release help

Post by DanielWhy » 31 Jan 2023, 23:27

mikeyww wrote:
26 Jan 2023, 16:45

Code: Select all

#Requires AutoHotkey v1.1.33
; #Requires AutoHotkey v2.0
XButton2::SoundBeep 1500
XButton2 Up::MsgBox 12345
Concepts:
1. Braces are omitted in specifying a hotkey.
2. Up is a hotkey modifier, but Down is not. Hotkeys without Up are triggered on the press (with some exceptions).

Explained: Introduction and simple examplesUp (has more examples)
I tried this and it worked but when i changed it to my desired keybinds:

Code: Select all

#Requires AutoHotkey v1.1.33
; #Requires AutoHotkey v2.0
XButton2::3
XButton2 Up::l
it returned an error *Duplicate keybind: *XButton2 Up. Any thoughts, did i miss something simple. I feel crazy

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

Re: XButton2 press and release help

Post by mikeyww » 31 Jan 2023, 23:48

Reading some documentation will quickly shed light on the problem and answer your question, as a remap of one key translates to a pair of hotkeys that includes both the key-down and key-up.

https://www.autohotkey.com/docs/v1/misc/Remap.htm#actually

You now know why the duplication error was generated.

The fix is easy, because you can use the Send command instead for both of your hotkeys. An example is provided on line 3 of my second post.

Post Reply

Return to “Ask for Help (v1)”