0

Basically what I'm trying to do is switch between different keyboard layouts by using different hotkeys.

So when I press

^+!F4::

these keys are remapped

d::Space
f::j
j::k
k::l

and doing this

^+!F8::

will remap

a::s
s::d
d::f
f::Space
j::Left
k::Numpad4
l::Numpad5
;::Numpad6

and finally

^+!F7::

Will revert everything to default

Seems whatever I try the script doesn't compile do to duplicate hotkeys. Is this possible to achieve with autohotkey?

2 Answers2

0

You can use the Hotkey command to change hotkeys dynamically. Here's a small example how you can remap your keys:

#Persistent
#UseHook

keyMap := {d: "SPACE", f: "j", j: "k", k: "l"}
; Associative objects are AHK_L only!
; for a pseudo array, do something like this:
; keyMapd := "SPACE"
; keymapf := "j"
; ...

Exit

F4::
    for orig, new in keyMap
    {
        Hotkey, %orig%, RemapKey
    }
return

RemapKey:
    newKey := keyMap[A_ThisHotkey]
    ; to retrieve from a pseudo array, do this:
    ; newKey := keyMap%A_ThisHotkey%
    Send, {%newKey%}
return

keyMapis an associative array, mapping the original key with its new target. Pressing F4 will take every key defined in keyMap and assign it a subroutine called RemapKey, hence it will be called, when any key in keyMap (d, f, j or k) is pressed. RemapKey takes the latest hotkey by accessing A_ThisHotkey, finds out what it's mapped to and sends that key. The #UseHook is essential in order to prevent the Send command to trigger another hotkey (in our case, pressing **F would trigger **J, J would then trigger K and so on). Note, that the use of associative objects if AutoHotkey_L only. If you're using another version, you'll have to define your map another way (e.g. a pseudo array like keyMapd:="SPACE").

MCL
  • 3,985
  • 3
  • 27
  • 39
0

This is an alternative way, also requiring AutoHotKey_L.

#SingleInstance Force
Flag:=0
Return

^+!F7::
Flag:=0
TrayTip, AutoHotKey, Keyboard setting Default, 1
Return

^+!F4::
Flag:=1
TrayTip, AutoHotKey, Keyboard setting A, 1
Return

^+!F8::
Flag:=2
TrayTip, AutoHotKey, Keyboard setting B, 1
Return

#If (Flag=1)
x::SoundBeep, 500, 100
d::Space
f::j
j::k
k::l
#If

#If (Flag=2)
x::SoundBeep, 2000, 100
a::s
s::d
d::f
f::Space
j::Left
k::Numpad4
l::Numpad5
;::Numpad6
#If
Robert Ilbrink
  • 7,738
  • 2
  • 22
  • 32