user90726
user90726

Reputation: 975

Store key modifier as variable

Is there way to use variable for key modifier, for example:

var = +
%var%c:: do something  ; Equal to Shift-C

... Well, StackOverflow trying to force me to write something more about the task. But I'm really don't know what to add to what have been already said. Also I've already read AHK forums, but can't find answer there.

Upvotes: 1

Views: 1138

Answers (1)

Dave Horner
Dave Horner

Reputation: 423

Dynamic hotkeys are defined using the Hotkey command.

From the AHK website on hotkeys: https://autohotkey.com/docs/Hotkeys.htm

By means of the Hotkey command, hotkeys can be created dynamically while the script is running. The Hotkey command can also modify, disable, or enable the script's existing hotkeys individually.

This faq page on dynamic variables provides something close to what you are asking for: https://autohotkey.com/board/topic/97097-faq-variables-dynamic-variables-literal-strings-and-stuff-like-that/

keys = abcdefghijklmnopqrstuvwxyz
StringSplit, keys, keys

Loop, %keys0%
    Hotkey, % keys%A_Index%, keydown

return
keydown:
ToolTip, %A_ThisHotkey% was pressed

I've verified that the following works as expected:

var = +c
Hotkey, %var%, keydown
return
keydown:
ToolTip, %A_ThisHotkey% was pressed

Upvotes: 1

Related Questions