Using 'Input' for a kind of Hotstring / multi-Hotkey

Get help with using AutoHotkey (v1.1 and older) and its commands and hotkeys
supplementfacts
Posts: 41
Joined: 15 Jul 2018, 16:29

Using 'Input' for a kind of Hotstring / multi-Hotkey

16 Apr 2024, 14:32

Hi, I've had success with the following use of 'Input' for a kind of Hotstring / multi-Hotkey:

Code: Select all

Ctrl & PrintScreen::
Input, userKey, I L2
If userKey = dl
Run, C:\Users\XYZ\Downloads, , max
If userKey = do
Run, C:\Users\XYZ\Documents, , max
return
For example, all I have to do to open Downloads is press Ctrl+PrtSc, d, l. But what I can't seem to do is something like the following:

Code: Select all

Ctrl & PrintScreen::
Input, userKey, I L2
If userKey = {RAlt}{RAlt}
Run, C:\Users\XYZ\Pictures, , max
return
In other words, I can't manage to use buttons like 'Alt' and 'Ctrl' and 'Shift' (as opposed to 'd' and 'l' and 'o') for this kind of approach. Is there a way to use those buttons with this kind of approach? If not, is there a way to have Ctrl+PrtSc, Alt, Alt or even just PrtSc, Alt, Alt trigger an action? (I've tried to do it with KeyWait, but that command is currently impossible for me to understand.)

Thank you!
User avatar
mikeyww
Posts: 27010
Joined: 09 Sep 2014, 18:38

Re: Using 'Input' for a kind of Hotstring / multi-Hotkey

16 Apr 2024, 15:09

Code: Select all

#Requires AutoHotkey v1.1.33.11
ih := InputHook()
ih.KeyOpt("dl{RAlt}", "N")
ih.OnKeyUp := Func("ih_KeyUp")
Global keys

Ctrl & PrintScreen::
KeyWait Ctrl
KeyWait PrintScreen
ih.Start()
SoundBeep 1500
ih.Wait()
Switch keys {
 Case "d,l"      : MsgBox 1
 Case "RAlt,RAlt": MsgBox 2
 Default         : SoundBeep 1000
}
Return

ih_KeyUp(ih, VK, SC) {
 Static str := "", n := 0
 str .= (str = "" ? "" : ",") GetKeyName(Format("vk{:x}sc{:x}", VK, SC)), n++
 ToolTip % str
 If (n = 2)
  keys := str, str := "", n := 0, ih.Stop()
}
supplementfacts
Posts: 41
Joined: 15 Jul 2018, 16:29

Re: Using 'Input' for a kind of Hotstring / multi-Hotkey

16 Apr 2024, 16:16

Thank you for this code. It seems to work, although I must say I find it completely baffling. One question: is there an easy way to revise it so that PrtSc, Alt, Alt (or some similar sequence) works? I tried changing this:

Code: Select all

Ctrl & PrintScreen::
KeyWait Ctrl
KeyWait PrintScreen
ih.Start()
to this:

Code: Select all

PrintScreen::
KeyWait PrintScreen
ih.Start()
But the change seems to have disabled the entire script. Thanks again!
User avatar
mikeyww
Posts: 27010
Joined: 09 Sep 2014, 18:38

Re: Using 'Input' for a kind of Hotstring / multi-Hotkey

16 Apr 2024, 17:34

This continues to capture keys until the notification keys are released. This can be modified in various ways, to terminate input earlier, to make keys visible, etc.

The script starts to collect keyboard input when the hotkey is triggered. Specific keys are defined as "notification keys". When these keys are released, a function is called. The function looks to see whether two such keys have been released. If so, the input is terminated. This returns flow to the Switch statement in the hotkey subroutine. The Switch statement acts based on the two keys that were released. In this example, only the Alt keys are defined as notification keys, but this could be altered such that additional or even all keys become notification keys.

A reason to use this approach is that you want to use a sequence of keys, where one or more of the keys in the sequence are not visible (i.e., do not produce text). There are alternative approaches. For example, to look for a small number of keys, one could simply use a couple of KeyWait commands.

Issues that one could consider may include whether there is a role for any intervening keys, or a role for specific timing or time window during which the key sequence must be completed. This demonstration script proceeds regardless of those factors.

Rohwedder previously posted a script that defines a hotkey for each key of interest. When any of them is pressed, a string concatenates the key names. After a timer expires, the final string is then compared to a key sequence of interest.

Code: Select all

#Requires AutoHotkey v1.1.33.11
ih := InputHook()
ih.KeyOpt("{RAlt}{LAlt}", "N") ; Notification keys (change as needed)
ih.OnKeyUp := Func("ih_KeyUp") ; Releasing any notification key will call this function
Global keys

PrintScreen Up::               ; Initial trigger
ih.Start()                     ; Start to collect keyboard input
SoundBeep 1500
ih.Wait()                      ; Wait until input is terminated
Switch {                       ; Act based on the two released keys
 Case keys ~= "[LR]Alt,[LR]Alt" : MsgBox 64, Result, Alt Alt!
 Default: SoundBeep 1000       ; Can add more lines if needed
}
Return

ih_KeyUp(ih, VK, SC) {         ; Called when a notification key is released
 Static str := "", n := 0
 str .= (str = "" ? "" : ",") GetKeyName(Format("vk{:x}sc{:x}", VK, SC)) ; List of released keys
 n++                           ; Number of keys in the list
 ToolTip % str
 If (n = 2)                    ; When two notification keys have been released, assign to "keys", stop input
  keys := str, str := "", n := 0, ih.Stop()
}

Code: Select all

#Requires AutoHotkey v1.1.33.11

PrintScreen Up::
SoundBeep 1500
KeyWait LAlt, DT1
If !ErrorLevel {
 KeyWait LAlt, T1
 If !ErrorLevel {
  KeyWait LAlt, DT1
  If !ErrorLevel
   MsgBox
 }
}
SoundBeep 2500
Return

Code: Select all

#Requires AutoHotkey v1.1.33.11
Gosub Reset

PrintScreen::
~Alt::
str .= A_ThisHotkey
SetTimer Act, -800
Return

Act:
If (str = "PrintScreen~Alt~Alt")
 MsgBox 64, Result, Alt Alt!
Reset:
str := ""
SoundBeep 2500
Return

Return to “Ask for Help (v1)”

Who is online

Users browsing this forum: AlFlo, GEOVAN, mikeyww, ShatterCoder and 213 guests