Pressing X Triggers A, Holding X Triggers B

Get help with using AutoHotkey (v1.1 and older) and its commands and hotkeys
koolestani
Posts: 76
Joined: 15 Jun 2020, 04:22

Pressing X Triggers A, Holding X Triggers B

27 Nov 2021, 21:09

Hey there
Sorry if this is being repeated for the thousandth time, because it probably is but i can't seem to get the answer to my situation, so basically I need my XButton1 to perform in the following way,
If Pressed, Behavior 1
If Held, Behavior 2 it acts as if Ctrl key is being held, allowing me to then drag a selection box (which requires LButton to be held as well of course) over files (non contiguous selection), Ideally this would start after some time has elapsed since XButton1 was held, say 500 ms

So just to phrase it in another manner, as long as XButton1 is held it acts like Ctrl key is held; and when tapped it should perform another action.
I know I'm further complicating things by asking this, but can the above be integrated with Double Pressing XButton1 as a third hotkey
Thank you
User avatar
mikeyww
Posts: 26596
Joined: 09 Sep 2014, 18:38

Re: Pressing X Triggers A, Holding X Triggers B

27 Nov 2021, 21:28

You're right. Some earlier examples are below.

viewtopic.php?p=227361#p227361

viewtopic.php?f=76&t=83364&p=364993#p364993

For your Ctrl, you can Send {Ctrl down}, then KeyWait for your other key, and then send your Ctrl up again.
koolestani
Posts: 76
Joined: 15 Jun 2020, 04:22

Re: Pressing X Triggers A, Holding X Triggers B

27 Nov 2021, 23:43

hi mikey and thanks for the quick response
i tried the following:

Code: Select all

$a::
If !Ta
	SetTimer, Ta, -300
Ta++
Return
Ta:
If GetKeyState("XButton1","P")
	Send {Ctrl down}
	KeyWait XButton1
	Send {Ctrl up}
Else If Ta = 2
	Send, b
Else If Ta = 1
	Send, a
KeyWait, XButton1
Ta = 
Return
But this results in an error stating ELSE with no matching IF
Last edited by koolestani on 28 Nov 2021, 05:20, edited 2 times in total.
koolestani
Posts: 76
Joined: 15 Jun 2020, 04:22

Re: Pressing X Triggers A, Holding X Triggers B

27 Nov 2021, 23:50

and weirdly enough this code snippet leads to the same result (types out Regular when tested in notepad) regardless of whether XButton1 was pressed once, twice or held down

Code: Select all

XButton1::
KeyWait, %A_ThisHotkey%, T0.2    ; Wait for release
If ErrorLevel {                  ; Button was HELD instead
 SoundBeep, 1500, 20
 Send Held `
 Return
}
KeyWait, %A_ThisHotkey%, D T0.2  ; Wait for second press
If Errorlevel {                  ; Second press did not occur = REGULAR click
 SoundBeep, 1200, 20
 Send Regular `
 Return
}
SoundBeep, 900, 20               ; Second press occurred = DOUBLE-click
Send Double `
Return
User avatar
mikeyww
Posts: 26596
Joined: 09 Sep 2014, 18:38

Re: Pressing X Triggers A, Holding X Triggers B

28 Nov 2021, 07:01

I just tested this exact script in Notepad, and it worked perfectly. Did you save and reload? This is your exact script, with no other code? You have no other scripts running? What AHK version do you run, and what operating system?
koolestani
Posts: 76
Joined: 15 Jun 2020, 04:22

Re: Pressing X Triggers A, Holding X Triggers B

28 Nov 2021, 10:22

I did save and reload, this is the entire code no; extra code anywhere, i did have other scripts running earlier so i tried after pausing them and nothing changed then i tried after closing them and nothing changed, I'm on Windows 10 Version 20H2 (OS Build 19042.1320) 64bits, AutoHotkey Version 1.1.30.01
Last edited by koolestani on 28 Nov 2021, 10:43, edited 1 time in total.
User avatar
mikeyww
Posts: 26596
Joined: 09 Sep 2014, 18:38

Re: Pressing X Triggers A, Holding X Triggers B

28 Nov 2021, 10:30

Strange! A fix for the first script is below.

Code: Select all

XButton1::
If !Ta
 SetTimer, Ta, -300
Ta++
Return
Ta:
Switch
{ Case GetKeyState("XButton1", "P"): SendInput Held`n
  Case Ta = 2                      : SendInput Double`n
  Case Ta = 1                      : SendInput Single`n
}
KeyWait, XButton1
Ta =
Return
koolestani
Posts: 76
Joined: 15 Jun 2020, 04:22

Re: Pressing X Triggers A, Holding X Triggers B

28 Nov 2021, 10:46

Error at line 7.
Line Text: Switch
Error: This line does not contain a recognized action.
User avatar
mikeyww
Posts: 26596
Joined: 09 Sep 2014, 18:38

Re: Pressing X Triggers A, Holding X Triggers B

28 Nov 2021, 10:55

It sounds like you are using an old AHK version. Would upgrade to the latest.
koolestani
Posts: 76
Joined: 15 Jun 2020, 04:22

Re: Pressing X Triggers A, Holding X Triggers B

29 Nov 2021, 10:28

Hi, sorry for the late response, I updated to the latest version and although the script doesn't throw the error it did previously. The "held" part just doesn't work, single and double are working fine. However this seems to be the case with XButton1, i replaced XButton1 with a and all three types of inputs are getting detected.
User avatar
mikeyww
Posts: 26596
Joined: 09 Sep 2014, 18:38

Re: Pressing X Triggers A, Holding X Triggers B

29 Nov 2021, 10:46

I presume that you are testing this exact script with no other code, and no other scripts running. It could be something different about how your mouse device is handling that button. When the button is pressed, the timer routine executes after 300 ms. If GetKeyState is set at that time, then the button is held. This is straightforward. You might want to see whether AHK can identify the key state for your button, via GetKeyState as well as KeyWait. Another possibility is that the button is firing repeatedly, but that would be unexpected.

You can determine what is happening in the Switch by displaying values of your key states & variables.
koolestani
Posts: 76
Joined: 15 Jun 2020, 04:22

Re: Pressing X Triggers A, Holding X Triggers B

29 Nov 2021, 12:59

I was beginning to think so too, it's either at the hardware level or perhaps the accompanying software that acts as an obstacle in detecting the held state, either way i need to adapt. any ideas on how i can make double press of XButton1 toggle Ctrl held down/released?
User avatar
mikeyww
Posts: 26596
Joined: 09 Sep 2014, 18:38

Re: Pressing X Triggers A, Holding X Triggers B

29 Nov 2021, 14:05

Code: Select all

XButton1::
^XButton1::
If !Ta
 SetTimer, Ta, -300
Ta++
Return
Ta:
Switch
{ Case GetKeyState("XButton1", "P"): SendInput Held`n
  Case Ta = 2                      : Send % "{Ctrl " (GetKeyState("Ctrl") ? "up" : "down") "}"
                                     SoundBeep, 1000 + 500 * GetKeyState("Ctrl")
  Case Ta = 1                      : SendInput Single`n
}
KeyWait, XButton1
Ta =
Return
koolestani
Posts: 76
Joined: 15 Jun 2020, 04:22

Re: Pressing X Triggers A, Holding X Triggers B

29 Nov 2021, 14:20

Thank you so much mikey, this is working liking a charm :D , i took the liberty of adding a triple click to toggle Shift key into this script as follows:

Code: Select all

  Case Ta = 3                      : Send % "{Shift " (GetKeyState("Shift") ? "up" : "down") "}"
                                     SoundBeep, 1000 + 500 * GetKeyState("Shift")
It is able to toggle Shift on but not off.
User avatar
mikeyww
Posts: 26596
Joined: 09 Sep 2014, 18:38

Re: Pressing X Triggers A, Holding X Triggers B

29 Nov 2021, 14:34

I think you would need to add hotkey +XButton1 at the top.
koolestani
Posts: 76
Joined: 15 Jun 2020, 04:22

Re: Pressing X Triggers A, Holding X Triggers B

29 Nov 2021, 14:50

Thank you again. As you can probably see, i don't have the slightest clue about what's going on, so forgive me for asking too much. In mikey we trust. :salute:
User avatar
mikeyww
Posts: 26596
Joined: 09 Sep 2014, 18:38

Re: Pressing X Triggers A, Holding X Triggers B

29 Nov 2021, 14:55

It's Rohwedder's routine. Ta just increments by one with each button press. After 300 ms elapse, the main section executes, and then resets the counter. You need the extra hotkey because you want the routine to trigger even when Shift is down.
koolestani
Posts: 76
Joined: 15 Jun 2020, 04:22

Re: Pressing X Triggers A, Holding X Triggers B

29 Nov 2021, 15:04

Thank you for the quick and short, I'm just too illiterate in AHK syntax to make things happen, I get the general idea and feel of what the code is doing, but that's not enough to write my own scripts.
User avatar
mikeyww
Posts: 26596
Joined: 09 Sep 2014, 18:38

Re: Pressing X Triggers A, Holding X Triggers B

29 Nov 2021, 15:06

Keep at it. This one is not necessarily a "beginner" kind of a script. Look at examples & documentation whenever you have time! Learn by doing. :thumbup:
koolestani
Posts: 76
Joined: 15 Jun 2020, 04:22

Re: Pressing X Triggers A, Holding X Triggers B

29 Nov 2021, 15:09

Thanks again!
Is there any way to remove the sound beep with a tooltip that stays on while the toggle is on and vice versa?

Return to “Ask for Help (v1)”

Who is online

Users browsing this forum: Rohwedder, Xtra and 138 guests