How can I bind several keys to the same command in AutoHotKey? - autohotkey

How can I bind several keys to the same command in AutoHotKey?
Example: I have this command:
spamLimit(limitTime)
{
StringReplace, key, A_ThisHotkey, $, , All
send %key%
sleep limitTime
}
I would like to bind several to this command, with the same parameter value:
a::spamLimit(500)
b::spamLimit(500)
c::spamLimit(500)
p::spamLimit(500)
d::spamLimit(500)
e::spamLimit(500)

Simplest solution:
a::
b::
c::
p::
d::
e::
spamLimit(500)
return

You can use Hotkey to define hotkeys on the fly:
; Thanks engunneer: autohotkey.com/board/topic/45636-script-to-prevent-double-typing/?p=284048
; Thanks throwaway_ye: https://www.reddit.com/r/AutoHotkey/comments/54g40q/how_can_i_bind_several_keys_to_the_same_command/d81j0we
; The following part must be located in the auto-execute section,
; i.e. the top part of the AHK script.
keylist = 1234567890qwertzuiopasdfghjklyxcvbnm
Loop, parse, keylist
{
Hotkey, $%A_LoopField%, spamLimitNoParam
}
Return
; This can be located anywhere in the AHK file
spamLimitNoParam:
spamLimit(200)
Return
This is equivalent to the solution Bob pointed to:
$a::
$b::
$c::
$d::
$e::
$f::
$g::
$h::
$i::
$j::
$k::
$l::
$m::
$n::
$o::
$p::
$q::
$r::
$s::
$t::
$u::
$v::
$w::
$x::
$y::
$z::
$0::
$1::
$2::
$3::
$4::
$5::
$6::
$7::
$8::
$9::
spamLimit(200)

Related

Remap `AltGr & someKey`

How do I remap e.g. AltGr + i?
I know how to remap other combinations like alt + i (!i::Send, test) or shift + i (+i::Send, test). But this doesn't work for AltGr.
I know how to map AltGr isolated: LControl & RAlt::Send, test works. But LControl & RAlt & i::Send, test doesn't work.
So, how do I go about it?
EDIT: SOLVED
This solution worked
From the post that OP linked:
Use a context sensitive #IF statement to check whether AltGr is held down, then create a standard one-button hotkey.
#user3419297's example:
LControl & RAlt:: AltGr := true ; assign the boolean value "true" to the variable 'AltGr''
LControl & RAlt Up:: AltGr := false
; The #If directive creates context-sensitive hotkeys and hotstrings
#If (AltGr) ; If this variable has the value "true"
a:: MsgBox AltGr+a
a & b:: MsgBox AltGr+a+b
b & a:: MsgBox AltGr+b+a
h & l:: Send Hello
i & e:: Run iexplore.exe
n & p:: Run notepad
w & p:: Run wordpad
#If ; turn off context sensitivity
So for OP's specific question:
#If (AltGr)
i::Send, test
#If
More info: Context Sensative #IF statements

Long-press = Capitalize

Intended Hotkey Function: Capitalize character if key pressed longer than 0.2s
Occurring Problem: When typing "vbnm" in a row in a fast manner (which means I am pressing a the next key while still holding down the previous one) then AHK outputs just x-times the key that was pressed first, resulting here in "vvvv".
That is the code. Please help me out (y) :-)
$y::
$x::
$c::
$v::
$b::
$n::
$m::
key := SubStr(A_ThisHotkey, 2)
;MsgBox, %key% ;it recognizes/shows all keys pressed correctly,
;but in the end it prints just x-times the key that was pressed first
;whereby x is the number of keys pressed very quickly in a row
KeyWait, %key%, T0.2 ;Long press = capitalize
If ErrorLevel
SendInput +%key%
Else
SendInput %key%
Return
So finally this code seems to work, beside the little inconvenience when holding the key much too long, resulting in e.g. "Oo".
;For normal characters ......
$x::
$c::
$v::
$b::
$n::
$m::keyFunc(SubStr(A_ThisHotkey, 2))
keyFunc(key) {
Critical
KeyWait, %key%, T0.3 ;Long press = capitalize
SendInput % ErrorLevel ? Format("{:U}", key) : key
Return
}
;For special characters ......
$2::
$3::
$4::
$5::
$6::
$7::
$8::
$9::
$sc01A:: ;ü
$sc027:: ;ö
$sc028:: ;ä
$sc00C:: ;ß
$sc033:: ;,
$sc034:: ;.
$sc035:: ;-
$sc01B:: ;+
$sc02B:: ;#
$sc00D:: keyFunc2(SubStr(A_ThisHotkey, 2))
keyFunc2(key) {
Critical
KeyWait, %key%, T0.3 ;Long press = capitalize
If ErrorLevel
SendInput +{%key%}
Else
SendInput {%key%}
Return
}
try:
$y::
$x::
$c::
$v::
$b::
$n::
$m::keyFunc(SubStr(A_ThisHotkey, 2))
keyFunc(key) {
KeyWait, %key%, T0.3 ;Long press = capitalize
SendInput % ErrorLevel ? Format("{:U}", key) : key
Return
}

Send only if key was pressed alone

I'd like to remap my windows key to something else, but also I'd like to keep all windows key based shortcut.
In pseudo code it would be something like this:
when LWin Down
until LWin Up
if not LWin down
abort
else
execute command
Release the left windows key within 0,3 seconds after pressing it, to do something else (e.g. to send a):
~LWin::
KeyWait, LWin
return
~LWin Up::
Send {LWin Up}
If (A_PriorHotKey = "~LWin" AND A_TimeSincePriorHotkey < 300)
Send, a
; else ; another action after long press (not recommendet)
; Send, b
return
EDIT:
Try also this:
LWin up::
If (A_PriorKey = "LWin")
Send a
return
; In this case its necessary to define a custom combination by using "&" or "<#"
; to avoid that LWin loses its original function as a modifier key:
<#d:: Send #d ; <# means LWin

AuthoHotKey exclusive double tap shift followed by action key

I'm trying to complete an autohotkey script where you can double tap any SHIFT key twice and follow it by a certain letter to activate a macro.
I have the following script but i have two "bugs" i can't figure out how to solve. I've tried other forums with no luck. Hoping someone can help me out.
I'm a little new at this.
Bug 1: The macro is activated if you press any letter in between the two SHIFTs where it should only be activated if you press the SHIFTs exclusively. For example, pressing SHIFT, s, SHIFT, d will enable the macro.
Bug 2: I'm not sure how this happens but with the following code, I periodically get all or some of the macros to activate while i'm typing. It seems to happen when I type the first capital letter in a sentence. But only sometimes. For example... "bla bla bla profiles. S"
I've fooled around with the timeouts but that doesn't seem to make much of a difference. Any help is appreciated.
Thanks,
Jeff.
~Shift Up::
If (A_ThisHotkey == A_PriorHotkey && A_TimeSincePriorHotkey < 400)
{
Double_SHIFT := true
Sleep, 2000
Double_SHIFT := false
}
return
; Press a key within two seconds after double tapping the Shift key, to activate an action:
#If (Double_SHIFT)
d::
FormatTime, CurrentDateTime,,MM/dd/yy - hh:mmtt
SendInput %CurrentDateTime%
Double_SHIFT :=false
return
a:: MsgBox, Test
s:: MsgBox, Test
f:: MsgBox, Test
return
Fix for Bug 1:
endKeys := "{BS}{Enter}{Insert}{Home}{Pgup}{PdDwn}End}{Delete}"
. "{F1}{F2}{F3}{F4}{F5}{F6}{F7}{F8}{F9}{F10}{F11}{F12}"
. "{LShift}{RShift}{Tab}{Esc}{CAPSLOCK}{Ctrl}{PrintScreen}{NumLock}"
. "{Numpad0}{Numpad0}{Numpad0}{Numpad0}{Numpad0}{Numpad0}{Numpad0}"
. "{Numpad7}{Numpad8}{Numpad9}{NumpadDel}{Up}{Down}{Left}{Right}"
. "{LAlt}{RAlt}{.}{,}{/}"
~Alt Up::
Input, key, V L1 t0.5 E, % endKeys
If (Errorlevel ~= "Alt") {
Double_ALT := true
Sleep 2000
Double_ALT := false
}
return
; Press a key within two seconds after double tapping the Alt key, to activate an action:
#If (Double_ALT)
d::
FormatTime, CurrentDateTime,,MM/dd/yy - hh:mmtt
SendInput %CurrentDateTime%
Double_ALT :=false
return
a:: MsgBox, Test
s:: MsgBox, Test
f:: MsgBox, Test
return

Shift Key Training Wheels and Shift Parenthesis Remap

I'm looking to use AutoHotKey to modify the functionality of my shift keys. The functionality is described in Steve Losh's Blog entry here. Specifically, I'd like my shift keys to do the following:
If LShift or RShift is pressed and released in under 300 ms with no other keys being pressed in between, send ( or ), respectively.
If LShift and RShift are "rolled" together (press LShift, press RShift, release LShift, release RShift, etc.) in under 300ms, send () or )(.
If a shift key is used improperly (LShift and S, RShift and K, etc.) then nothing happens.
I've been having issues with the 300ms requirement and the "rolling" functionality. Specifically, I'm having issues with only being able to detect when the key is released due to the hotkey combos such as:
LShift & 0:: return
This is where I'm at so far:
LShift::
Send {LShift Down}
KeyWait, LShift
Send {LShift Up}
if (A_TimeSinceThisHotkey < 300){
if (A_PriorKey = "LShift")
{
Send {)}
}
}
return
I don't see a reason to use a 300 ms timeout anyway, it seems unreliable and unnecessary.
Have a look at this commented code, it is short and efficient, and seems to meet all of your requirements:
LShift::Send, (
RShift::Send, )
LShift & RShift:: Send, ()
RShift & LShift:: Send, )(
/* Put your unwanted combinations here
* and let them do nothing
*/
LShift & q::return
RShift & y::return
Edit:
Since LShift and RShift already are prefix hotkeys, I left out the trick described here.
MCL's answer is close but when I tested it I found that shift-clicking didn't select text. Here's a version with a passthrough to allow shift-clicking to work.
;shift to parens
LShift::Send, (
RShift::Send, )
LShift & RShift:: Send, ()
RShift & LShift:: Send, )(
;passthrough for shift-click
LShift & LButton::
Send, {LShift Down}{LButton}
Send, {LShift Up}
RShift & LButton::
Send, {RShift Down}{LButton}
Send, {RShift Up}
I don't think the 300ms timeout is possible without either very deep understanding of autohotkey's implementation or actual modification to autohotkey. When I tried to get it to work (using http://www.autohotkey.com/board/topic/98742-remapping-shift-key/) I found that A_PriorHotkey was not consistently populated. I don't think that variable was meant to work with modifier keys this way.
I felt compelled to figure this one out. Here you go!
I basically created a hotkey for every Shift + Letter key combination in order to send the correct key case and also set the Abort value. The Abort value is then referenced whenever one of the Shift keys is pressed in order to determine whether or not to send the corresponding ( or ).
The "Rolling" was accomplished by creating a Hotkey for LShift + RShift (and the opposite). It then looks to see which key is released first to determine () or )(.
Accept if this was what you were looking for!
Loop 26
{
Hotkey, % "~LShift & " Chr(A_Index+96), LetterKey ; Hotkeys for A-Z
Hotkey, % "~RShift & " Chr(A_Index+96), LetterKey ; Hotkeys for A-Z
}
Return
RShift::
LShift::
Abort := 0
keyDown := A_TickCount
Keywait, % A_ThisHotkey
duration := A_TickCount - keyDown
If (duration < 200) and (Abort = 0)
SendInput % (A_ThisHotkey = "LShift") ? "(" : ")"
Send {LShift Up}
Return
RShift & LShift::
LShift & RShift::
Keywait, LShift
If GetKeyState("RShift", "P")
{
Keywait, RShift
Send ()
}
Else
Send )(
Return
LetterKey:
SendInput, % "+" SubStr(A_ThisHotKey, 0, 1)
Abort := 1
Return
EDIT:
Hmm, I seem to be having the same problem as you. I always get a duration of 0 due to the hotkeys.
I found and modified this script on the AutoHotKey Forums. (The original script was prone to type "K(" when you meant to type "K" if you type too quickly, so I've modified it so that shouldn't happen any more)
$LShift Up::send, % getkeystate("LShift") ? "{LShift Up}" : ""
$RShift Up::send, % getkeystate("RShift") ? "{RShift Up}" : ""
~$LShift::
KeyWait, LShift, T0.1 ; wait 100ms to check shift state
if (A_PriorKey = "LShift")
{
send, % getkeystate("LShift") ? "{LShift Down}" : "("
}
KeyWait, LShift
return
~$RShift::
KeyWait, RShift, T0.1 ; wait 100ms to check shift state
if (A_PriorKey = "RShift")
{
send, % getkeystate("RShift") ? "{RShift Down}" : ")"
}
KeyWait, RShift
return