Indicator for held modifiers/buttons Topic is solved

Get help with using AutoHotkey (v1.1 and older) and its commands and hotkeys
flubber42
Posts: 8
Joined: 03 Nov 2019, 09:58

Indicator for held modifiers/buttons

04 Aug 2020, 14:30

Hi all. I've written a program that uses tooltips to indicate when certain modifiers/mouse buttons are being held down. This is for use as a visual prompt when controlling my computer by voice. The tooltips should only fire when they have been held for a given length of time and accomodate any combination of listed keys. Although I have a working script, it feels a little flaky. Occasionally a tooltip will remain in place after release for example. I suspect the script is also hampered by the lack of multi-threading because simultaneously held modifiers don't display in unison.

Could anyone suggest improvements or perhaps a better solution entirely? Cheers

Code: Select all

Process,Priority,,L		; seems to be necessary for reliably capturing synthetic key presses (ie. via voice command)
Thread, Interrupt, 600		; allows for additional modifiers to show
#SingleInstance force
#NoEnv
SendMode Input
SetWorkingDir %A_ScriptDir%
CoordMode, ToolTip, Screen

~*LCtrl::

state1 := GetKeyState("LCtrl")
while (GetKeyState("LCtrl"))
 {
   sleep 10
   if state1 = 0
     {
       ToolTip,,,,1
       exit
     }
   if A_Index > 40
     {
       tooltip, Ctrl, 1000, 0, 1
       exit
     }
 }
return

~*LCtrl Up::
ToolTip,,,,1   ;LCtrl RELEASED
return

;~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

~*LButton::
state2 := GetKeyState("LButton")
while (GetKeyState("LButton"))
 {
   sleep 10
   if state2 = 0
     {
       ToolTip,,,,2
       exit
     }
   if A_Index > 40
     {
       tooltip, LButton, 1030, 0, 2
       exit
     }
 }
return

~*LButton Up::
ToolTip,,,,2    ;LButton RELEASED
return

;~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

~*Shift::
state3 := GetKeyState("Shift")
while (GetKeyState("Shift"))
 {
   sleep 10
   if state3 = 0
     {
       ToolTip,,,,3
       exit
     }
   if A_Index > 40
     {
       tooltip, Shift, 1080, 0, 3
       exit
     }
 }
return

~*Shift Up::
ToolTip,,,,3    ;Shift RELEASED
return

;~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

~*RAlt::
state4 := GetKeyState("RAlt")
while (GetKeyState("RAlt"))
 {
   sleep 10
   if state4 = 0
     {
       ToolTip,,,,4
       exit
     }
   if A_Index > 40
     {
       tooltip, RAlt, 965, 0, 4
       exit
     }
 }
return

~*RAlt Up::
ToolTip,,,,4    ;RAlt RELEASED
return

;~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

~*RButton::
state5 := GetKeyState("RButton")
while (GetKeyState("RButton"))
 {
   sleep 10
   if state5 = 0
     {
       ToolTip,,,,5
       exit
     }
   if A_Index > 40
     {
       tooltip, RButton, 1150, 0, 5
       exit
     }
 }
return

~*RButton Up::
ToolTip,,,,5    ;RButton RELEASED
return

;~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

~*LAlt::
state6 := GetKeyState("LAlt")
while (GetKeyState("LAlt"))
 {
   sleep 10
   if state6 = 0
     {
       ToolTip,,,,6
       exit
     }
   if A_Index > 40
     {
       tooltip, LAlt, 935, 0, 6
       exit
     }
 }
return

~*LAlt Up::
ToolTip,,,,6    ;LAlt RELEASED
return

;~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

~*LWin::
state7 := GetKeyState("LWin")
while (GetKeyState("LWin"))
 {
   sleep 10
   if state7 = 0
     {
       ToolTip,,,,7
       exit
     }
   if A_Index > 40
     {
       tooltip, LWin, 905, 0, 7
       exit
     }
 }
return

~*LWin Up::
ToolTip,,,,7    ;LWin RELEASED
return
[PS. Using latest Windows 10, regular AHK 1.1.33.02 / 32bit]
Rohwedder
Posts: 7612
Joined: 04 Jun 2014, 08:33
Location: Germany

Re: Indicator for held modifiers/buttons  Topic is solved

05 Aug 2020, 01:58

Hallo,
try:

Code: Select all

#Persistent
#SingleInstance force
#NoEnv
SendMode Input
SetWorkingDir %A_ScriptDir%
CoordMode, ToolTip, Screen
SetTimer, Timer, 100
Timer:
Ctrl := GetKeyState("Ctrl","P")?(Ctrl + 1):0
ToolTip,% Ctrl > 9?"Ctrl":, 1000, 0, 1
LButton := GetKeyState("LButton","P")?(LButton + 1):0
ToolTip,% LButton > 9?"LButton":, 1030, 0, 2
Shift := GetKeyState("Shift","P")?(Shift + 1):0
ToolTip,% Shift > 9?"Shift":, 1080, 0, 3
RAlt := GetKeyState("RAlt","P")?(RAlt + 1):0
ToolTip,% RAlt > 9?"RAlt":, 965, 0, 4
RButton := GetKeyState("RButton","P")?(RButton + 1):0
ToolTip,% RButton > 9?"RButton":, 1150, 0, 5
LAlt := GetKeyState("LAlt","P")?(LAlt + 1):0
ToolTip,% LAlt > 9?"LAlt":, 935, 0, 6
LWin := GetKeyState("LWin","P")?(LWin + 1):0
ToolTip,% LWin > 9?"LWin":, 905, 0, 7
Return
flubber42
Posts: 8
Joined: 03 Nov 2019, 09:58

Re: Indicator for held modifiers/buttons

05 Aug 2020, 14:10

Thank you so much Rohwedder. That works perfectly and what an elegant solution! My code is such an embarrassment... :oops:

Return to “Ask for Help (v1)”

Who is online

Users browsing this forum: haomingchen1998, mikeyww and 245 guests