Franck Dernoncourt
Franck Dernoncourt

Reputation: 83457

How can I bind several keys to the same command in 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)

Upvotes: 0

Views: 1296

Answers (2)

Franck Dernoncourt
Franck Dernoncourt

Reputation: 83457

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)

Upvotes: 0

Bob
Bob

Reputation: 431

Simplest solution:

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

Upvotes: 1

Related Questions