Pressing X Triggers A, Holding X Triggers B

Get help with using AutoHotkey (v1.1 and older) and its commands and hotkeys
User avatar
mikeyww
Posts: 26848
Joined: 09 Sep 2014, 18:38

Re: Pressing X Triggers A, Holding X Triggers B

Post by mikeyww » 29 Nov 2021, 15:17

Code: Select all

#If !GetKeyState("Alt", "P") & !GetKeyState("LWin", "P")
*XButton1::
SetTimer, Ta, -300
Ta++
Return
Ta:
Switch
{ Case GetKeyState("XButton1", "P") : SendInput Held`n
  Case Ta = 1 : SendInput Single`n
  Case Ta = 2 : Send % "{Ctrl "  (GetKeyState("Ctrl")  ? "up" : "down") "}"
  Case Ta = 3 : Send % "{Shift " (GetKeyState("Shift") ? "up" : "down") "}"
}
ToolTip, % (GetKeyState("Ctrl") ? "Ctrl" : "") (GetKeyState("Shift") ? "Shift" : "")
KeyWait, XButton1
Ta =
Return
#If

koolestani
Posts: 76
Joined: 15 Jun 2020, 04:22

Re: Pressing X Triggers A, Holding X Triggers B

Post by koolestani » 06 Jul 2022, 14:35

Hi,
Can this script be facelifted to behave in the following manner?
Press XButton1 triggers XButton1
Hold XButton1 for 1.5 seconds - Enter Hold Ctrl mode. Holding XButton1 in this mode should do nothing. Press XButton1 once to exit this mode.
Hold XButton1 for 2.0 seconds - Enter Hold Shift mode. Holding XButton1 in this mode should do nothing. Press XButton1 once to exit this mode.
Hold XButton1 for 2.5 seconds - Enter Hold Ctrl+Shift mode. Holding XButton1 in this mode should do nothing. Press XButton1 once to exit this mode.
Hold XButton1 for 3.0 seconds or longer - Do Nothing or maybe just let XButton1 go through.
Thanks for the help! :D

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

Re: Pressing X Triggers A, Holding X Triggers B

Post by mikeyww » 06 Jul 2022, 20:09

Your turn! You could use a series of KeyWait commands if you like. Another approach is to look at the A_TimeSinceThisHotkey.

Code: Select all

XButton1::
ToolTip, % last := 0
For each, sec in [1.5, 2.0, 2.5, 3.0] {
 KeyWait, XButton1, % "T" sec - last
 If ErrorLevel { ; Held
  SoundBeep, 900 + 100 * sec
  ToolTip, %sec%
 }
 last := sec
}
Return

koolestani
Posts: 76
Joined: 15 Jun 2020, 04:22

Re: Pressing X Triggers A, Holding X Triggers B

Post by koolestani » 07 Jul 2022, 01:15

I tried this code and when I press XButton1, all I get is the tooltip displaying 0. It doesn't matter how long I hold the button or if I simply press it, the result is same.

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

Re: Pressing X Triggers A, Holding X Triggers B

Post by mikeyww » 07 Jul 2022, 05:54

Close other scripts. Have no other code in this script. If you hold for 3 seconds, you should see the tooltip changing. My AHK version: 1.1.34.03.

koolestani
Posts: 76
Joined: 15 Jun 2020, 04:22

Re: Pressing X Triggers A, Holding X Triggers B

Post by koolestani » 07 Jul 2022, 10:00

Updated my AHK version and closed every other script, and the script has no other code. Still no luck.

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

Re: Pressing X Triggers A, Holding X Triggers B

Post by mikeyww » 07 Jul 2022, 10:11

Is the script running? Are you pressing XButton1 instead of another button? Holding the button for 3-4 seconds? Never hear a beep? Mouse works? No other mouse utility software is running? You exactly copied and pasted the script into your AHK text file?

Check KeyHistory to see what is happening with the keys.

A simplified test script is below.

Code: Select all

XButton1::
For each, sec in [0.5] {
 KeyWait, XButton1, % "T" sec
 If ErrorLevel
  SoundBeep, 1500
}
Return
If nothing works, then copy and paste your script below.

koolestani
Posts: 76
Joined: 15 Jun 2020, 04:22

Re: Pressing X Triggers A, Holding X Triggers B

Post by koolestani » 07 Jul 2022, 10:28

I am pressing XButton1. Holding it long enough without a doubt. I don't hear a beep in any case. Mouse works. Not exactly sure about the mouse utility software question. I'd guess Logitech Options would fall into that category, but then again it hasn't bothered me with my other XButton related scripts before. I used the Download button to save your code snippet, so it is exactly the same code without a doubt.

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

Re: Pressing X Triggers A, Holding X Triggers B

Post by mikeyww » 07 Jul 2022, 10:36

If the last script that I posted never beeps, then you have a problem that is probably not directly related to AutoHotkey. Would test on another computer.

A simpler test yet:

Code: Select all

XButton1::
KeyWait, XButton1, T.3
SoundBeep, 1500
Return
You can also try a different hotkey, like F3 or LButton.

Code: Select all

#InstallKeybdHook
F3::
LButton::
XButton1::
KeyWait, %A_ThisHotkey%, T.3
SoundBeep, 1500
Return

koolestani
Posts: 76
Joined: 15 Jun 2020, 04:22

Re: Pressing X Triggers A, Holding X Triggers B

Post by koolestani » 07 Jul 2022, 10:47

This code beeps!
mikeyww wrote:
07 Jul 2022, 10:36

Code: Select all

XButton1::
KeyWait, XButton1, T.3
SoundBeep, 1500
Return

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

Re: Pressing X Triggers A, Holding X Triggers B

Post by mikeyww » 07 Jul 2022, 10:48

What is your AHK version? Double-click the tray icon.

koolestani
Posts: 76
Joined: 15 Jun 2020, 04:22

Re: Pressing X Triggers A, Holding X Triggers B

Post by koolestani » 07 Jul 2022, 10:49

1.1.34.03

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

Re: Pressing X Triggers A, Holding X Triggers B

Post by mikeyww » 07 Jul 2022, 10:50

If you get no beep with the following script, then I have no explanation for it.

Code: Select all

XButton1::
For each, sec in [0.5] {
 KeyWait, XButton1, % "T" sec
 If ErrorLevel
  SoundBeep, 1500
}
Return

koolestani
Posts: 76
Joined: 15 Jun 2020, 04:22

Re: Pressing X Triggers A, Holding X Triggers B

Post by koolestani » 07 Jul 2022, 10:56

Unfortunately this last code snippet didn't result in a beep!
I'm really sorry for bothering, can you suggest something with the A_TimeSinceThisHotkey method?

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

Re: Pressing X Triggers A, Holding X Triggers B

Post by mikeyww » 07 Jul 2022, 11:27

Code: Select all

XButton1::
start := A_TickCount
SetTimer, Tip, 500
KeyWait, %A_ThisHotkey%
SetTimer, Tip, Off
MsgBox, 64, Time, %held%
Return

Tip:
held := A_TickCount - start
SoundBeep, held
Switch
{ Case held > 3000: ToolTip, %held%
  Case held > 2500: SetTimer,, Off
                    ToolTip, %held%
  Case held > 2000: ToolTip, %held%
  Case held > 1500: ToolTip, %held%
  Default         : ToolTip, %held%
}
Return

koolestani
Posts: 76
Joined: 15 Jun 2020, 04:22

Re: Pressing X Triggers A, Holding X Triggers B

Post by koolestani » 07 Jul 2022, 12:15

This is really strange but I just get an empty dialog box as soon as I press XButton1, even if I hold it for a longer duration. Also, no beeps with this either.
https://imgur.com/a/VEPt7n0

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

Re: Pressing X Triggers A, Holding X Triggers B

Post by mikeyww » 07 Jul 2022, 13:09

True, the first test box is empty. If you never hear beeps, I have no explanation; sorry.

Maybe something with KeyWait at your end? I do not know. Can try with button Up, though it's slightly different.

Code: Select all

XButton1::
start := A_TickCount
SetTimer, Tip, 500
Return

XButton1 Up::
SetTimer, Tip, Off
MsgBox, 64, Time, %held%
Return

Tip:
held := A_TickCount - start
SoundBeep, held
Switch
{ Case held > 3000: ToolTip, %held%
  Case held > 2500: SetTimer,, Off
                    ToolTip, %held%
  Case held > 2000: ToolTip, %held%
  Case held > 1500: ToolTip, %held%
  Default         : ToolTip, %held%
}
Return
I guess I will sign off here, because I have no additional ideas for you. Others here may.

koolestani
Posts: 76
Joined: 15 Jun 2020, 04:22

Re: Pressing X Triggers A, Holding X Triggers B

Post by koolestani » 07 Jul 2022, 20:15

This method worked!! I got the tooltips to display 500, 1000, 1500, 2000, 2500 and 3000. Although I'm getting a dialog box displaying 500 even at the slightest tap of the button, is that because of the default statement inside the switch function? How can this be adjusted to fit my needs? Can the tooltip be made to actively follow my cursor?

koolestani
Posts: 76
Joined: 15 Jun 2020, 04:22

Re: Pressing X Triggers A, Holding X Triggers B

Post by koolestani » 07 Jul 2022, 20:56

I found a way to make the tooltip follow my cursor, please let me know if there is something wrong in the implementation.
XButton1::
start := A_TickCount
SetTimer, Tip, 500
Return

XButton1 Up::
SetTimer, Tip, Off
Return

Tip:
held := A_TickCount - start
SoundBeep, held

SetTimer, FollowMouse, 50
Return

FollowMouse:
MouseGetPos, px, py

Switch
{ Case held > 3000: ToolTip, %held%
Case held > 2500: SetTimer,, Off
ToolTip, Shift, px+10, py+10
Case held > 2000: ToolTip, Alt, px+10, py+10
Case held > 1500: ToolTip, Ctrl, px+10, py+10
Default : ToolTip, TEST, px+10, py+10
}
Return

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

Re: Pressing X Triggers A, Holding X Triggers B

Post by mikeyww » 07 Jul 2022, 21:22

If your script works, then it means that there is nothing wrong with it.

Below is an adjustment from the original script. You can replace the tooltips with other commands. As with any hotkey (in most circumstances), the "Up" hotkey can interrupt another thread such as the timer thread. The interrupted thread will resume when the interrupting routine ends.

Code: Select all

XButton1 Up::
SetTimer, Tip, Off
ToolTip, %held%
MsgBox, 64, Time, %held%
Return

XButton1::
start := A_TickCount
SetTimer, Tip, 500
Tip:
held := A_TickCount - start
SoundBeep, held
Switch
{ Case held > 3000: ToolTip, %held%
  Case held > 2500: SetTimer,, Off
                    ToolTip, %held%
  Case held > 2000: ToolTip, %held%
  Case held > 1500: ToolTip, %held%
  Default         : ToolTip, %held%
}
Return

Post Reply

Return to “Ask for Help (v1)”