3
votes

AHK allows to bind keys, that is us a::z t fire 'z' whenever 'a' is pressed. What if I want to fire 'z' whenever 'a', 'b', or 'c' is pressed?

I can obviously repeat my code:

  a::z
  b::z
  c::z 

I can probably use a Gosub like

a::Gosub, abc
b::Gosub, abc
c::Gosub, abc

abc:
send z
return

Is there a better way to say "if a,b, or c are pressed - fire z"?

3

3 Answers

1
votes

You can just use

a::
b::
c::z

i am not sure what is the exact synthax, but this works.

1
votes

We're at codegolf.stackexchange.com, right?

JFF, here's assigning A-Y to Z with just 61 characters, using the Hotkey command:

loop,25
    hotkey,% chr(a_index+64),z
return
z(){ 
    send z
}
0
votes

Another solution using Hotkey to define hotkeys on the fly, and parse so that the user can directly specify a list of keys:

; 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%, SendGivenKey
}    
Return


; This can be located anywhere in the AHK file
SendGivenKey:
StringReplace, key, A_ThisHotkey, $, , All
send %key%
Return