1
votes

I think this must be possible but I can't find it in the documentation anywhere on autohotkey

I want to do something like:

[a-z0-9]::
SoundPlay, %A_WinDir%\Media\[\1].wav
SoundPlay *-1 

i.e. listen for all keys a to z and 0-9 find the relevant wav file (a.wav etc..) and play it. If it can't be found Play a default sound.

Is this way out of AHK's league? Should I stick to doing this in python??! Many thanks in advance

3

3 Answers

3
votes

You can use the Hotkey Command to assign all possible hotkeys to the label.

The first loop here uses a trick with converting values to their ASCII codes, doing math, and converting them back to characters. The logic works like, "What is the 5th character after 'a'?" -- to which it replies 'f'.

#Persistent 

Loop, 26
  Hotkey, % Chr(Asc("a") + A_Index - 1), MyLabel

Loop, 10
   Hotkey, % A_Index - 1, MyLabel
return

MyLabel:
MsgBox You pressed %A_ThisHotkey%
return
0
votes

I'm not aware of a way to use a regex to specify the hotkey, but this is how I've done it in one of my scripts:

@::
a::
b::
c::
; (all other alpha keys...)
    ; Pass key on, if not in subscription list
    If(in_edit_screen())
    {
        ; Handle caps lock
        If(GetKeyState("Capslock", "T"))
        {
            ; Send uppercase letter
            StringUpper key, A_ThisHotkey
            Send %key%
        }
        Else
        {
            Send %A_ThisHotkey%
        }
        Return
    }
    ; etc.
    Return

This is part of a routine that's called whenever a certain window is focused, but the window in question has a few different modes; in one of those modes (indicated by the in_edit_screen flag), I want to just pass the keys through, while in another mode, I want the keys to do something special. A_ThisHotkey is the variable that tells the routine which key/key combination the user actually pressed.

0
votes

Why not launch a script where you use:

Input, CI_KeyVar, I L1

and then read-out which key was pressed (variable = CI_KeyVar), do your thing (check if the ascii code falls between 0 and Z because this input will act on any input) and re-launch the script to wait for the next key press....

The way out would be to trigger on e.g. the esc key, so the script stops.