'AutoHotkey - Repetitive key
I'm trying to create an hotkey like Alt+44 to start a program C:/Path/Toto.exe The thing is that I have to release Numpad4 to press it again so I'm "losing" the hotkey. I thought it would not be so difficult, but in fact yes as I'm new to it... Can you help please? I have tried this:
Hotkey, !Numpad4, MyLabel
return
MyLabel:
If ...
Then several tests with Getkeystate or Keywait...
Edit:
This is what I finally get with Input command:
!Numpad3::
Input, AnyNumpadKey, L1
Gosub, Key3x
Key3x:
If GetKeyState("Numpad0", "P")
MsgBox,, State, Key30, 2
Else If GetKeyState("Numpad1", "P")
MsgBox,, State, Key31, 2
Else If GetKeyState("Numpad2", "P")
MsgBox,, State, Key32, 2
Else If GetKeyState("Numpad3", "P")
MsgBox,, State, Key33, 2
Else If GetKeyState("Numpad4", "P")
MsgBox,, State, Key34, 2
Else If GetKeyState("Numpad5", "P")
MsgBox,, State, Key35, 2
Else If GetKeyState("Numpad6", "P")
MsgBox,, State, Key36, 2
Else If GetKeyState("Numpad7", "P")
MsgBox,, State, Key37, 2
Else If GetKeyState("Numpad8", "P")
MsgBox,, State, Key38, 2
Else If GetKeyState("Numpad9", "P")
MsgBox,, State, Key39, 2
return
!Numpad4::
Input, AnyNumpadKey, L1
Gosub, Key4x
Key4x:
If GetKeyState("Numpad0", "P")
MsgBox,, State, Key40, 2
Else If GetKeyState("Numpad1", "P")
MsgBox,, State, Key41, 2
Else If GetKeyState("Numpad2", "P")
Run, c:/
Else If GetKeyState("Numpad3", "P")
MsgBox,, State, Key43, 2
Else If GetKeyState("Numpad4", "P")
MsgBox,, State, Key44, 2
Else If GetKeyState("Numpad5", "P")
MsgBox,, State, Key45, 2
Else If GetKeyState("Numpad6", "P")
MsgBox,, State, Key46, 2
Else If GetKeyState("Numpad7", "P")
MsgBox,, State, Key47, 2
Else If GetKeyState("Numpad8", "P")
MsgBox,, State, Key48, 2
Else If GetKeyState("Numpad9", "P")
MsgBox,, State, Key49, 2
return
Working fine. I can use Alt+32 or Alt+44 for example to start a program.
Solution 1:[1]
!NumPad4:: ; Alt+NumPad4
Keywait, NumPad4, T0.2 ; waits 0.2 seconds maximally for NumPad4 to be released
if (ErrorLevel) ; pressed for above that time
{
KeyWait, NumPad4 ; wait for NumPad4 to be released
Run Notepad ; Run C:/Path/Toto.exe
}
else ; released after below that time
Run, c:\ ; Open a folder.
Return
EDIT:
This detects single, double, and triple-presses of a hotkey:
!NumPad4::
if (AltNumPad_presses > 0) ; SetTimer already started, so we log the keypress instead.
{
AltNumPad_presses += 1
return
}
; Otherwise, this is the first press of a new series. Set count to 1 and start
; the timer:
AltNumPad_presses := 1
SetTimer, KeyAltNumPad, -400 ; Wait for more presses within a 400 millisecond window.
return
KeyAltNumPad:
if (AltNumPad_presses = 1) ; The key was pressed once.
{
Run, c:\ ; Open a folder.
}
else if (AltNumPad_presses = 2) ; The key was pressed twice.
{
Run Notepad ; Run C:/Path/Toto.exe
}
else if (AltNumPad_presses > 2)
{
MsgBox, Three or more times pressed.
}
; Regardless of which action above was triggered, reset the count to
; prepare for the next series of presses:
AltNumPad_presses := 0
return
EDIT2:
or this:
!NumPad4::
count++ ; for each press, increment a counter
If (count=1)
SetTimer AltNumPad_presses, -50 ; starts a timer off for 50ms (SetTimer is only used here to cause the code to be jumped to in a psuedo-thread)
return
AltNumPad_presses:
KeyWait, Alt, L ; takes no action until the key is released
If (count=1)
Run, c:\ ; Open a folder
If (count=2)
Run Notepad ; Run C:/Path/Toto.exe
If (count=3)
MsgBox, Three times pressed
; ...
count:=0 ; reset the counter
return
EDIT3:
make few programs start with Alt+12, Alt+13, etc.
!NumPad1:: Alt_NumPad1 := true
!NumPad1 Up:: Alt_NumPad1 := false
#If (Alt_NumPad1)
*NumPad2:: Run Notepad
*NumPad3:: Run c:\
; ...
#If
https://www.autohotkey.com/docs/commands/_If.htm
EDIT4:
The simplest and most effective solution is to use modifiers+key combinations, eg.
; Ctrl + Alt + Win + NumPadkey
^!#NumPad1:: Run Notepad
^!#NumPad2:: Run c:\
or:
; LCtrl + LAlt + LWin + NumPadkey
<^<!<#NumPad1:: Run Notepad
<^<!<#NumPad2:: Run c:\
or:
; RCtrl + RAlt + NumPadkey
>^>!NumPad1:: Run Notepad
>^>!NumPad2:: Run c:\
See Hotkey Modifier Symbols in the documentation.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 |
