toddeTV
toddeTV

Reputation: 1517

AutoHotkey key shortcuts with disabling Alt key

setup

AutoHotkey v 1.1.32.00 on Windows 10 (current version) with US keyboard layout.

goal

Extending the US keyboard layout with non-existing keys by using key shortcuts with the right Alt key = RAlt. Therefore, the RAlt key should be disabled completely and only be used for the new shortcuts (the left Alt key = LAlt remains for program menu jumps and similar things).
Examples:

first working attempt

#MenuMaskKey vkFF

key_map_lower := {a: "ä", y: "¥"}

RAlt::
for key in key_map_lower {
    Hotkey, $%key%, hotkey_label_lower, On
}
return

RAlt Up::
for key in key_map_lower {
    Hotkey, $%key%, Off
}
return

hotkey_label_lower:
Send, % key_map_lower[SubStr(A_ThisHotkey, 2)]
return

This works fine, no problems so far.

problem

The combination RAlt+a=ä prints only the lowercase German special letter, but there is also a equivalent in uppercase. Therefore, a new rule is needed like so: RAlt+Shift+a=Ä.

My attempt for this:

#MenuMaskKey vkFF

; lowercase letters

key_map_lower := {a: "ä", y: "¥"}

RAlt::
for key in key_map_lower {
    Hotkey, $%key%, hotkey_label_lower, On
}
return

RAlt Up::
for key in key_map_lower {
    Hotkey, $%key%, Off
}
return

hotkey_label_lower:
Send, % key_map_lower[SubStr(A_ThisHotkey, 2)]
return

; capital/uppercase letters

key_map_upper := {a: "Ä"}

RAlt & +::
for key in key_map_upper {
    Hotkey, $%key%, hotkey_label_upper, On
}
return

RAlt & + Up::
for key in key_map_upper {
    Hotkey, $%key%, Off
}
return

hotkey_label_upper:
Send, % key_map_upper[SubStr(A_ThisHotkey, 2)]
return

But this only works for lowercase letters as before. The uppercase letters do not appear.

alternative attempt

RAlt::
Hotkey, a, label_a, On
Hotkey, y, label_y, On
Hotkey, +a, label_a_, On
return

RAlt Up::
Hotkey, a, label_a, Off
Hotkey, y, label_y, Off
Hotkey, +a, label_a_, Off
return

label_a:
Send, ä
return

label_y:
Send, ¥
return

label_a_:
Send, Ä
return

This works for lowercase and also uppercase. But the uppercase only work when pressing RAlt before Shift. When pressing first Shift and then RAlt, the window menu gets focused.

question

How can either

  1. the second script be fixed so that it is not important whether to press Shift or RAlt first?
  2. Or how to fix the first script in order to work and also ignoring the order of RAlt and Shift?

The first attempt is more compact and more robust I guess because of less redundantly and also less lines.

(Also I think both attempts does not have any beauty in code at all, but combining lowercase and uppercase in one data structure in the first attempt only caused even more problems. With this doubling at least the lowercase letters work. But I am thankful for all optimizations as well.)

additional question

I also want to map RAlt+`=°
This is possible in the second attempt because the label are manually assigned. But with the first attempt this is not possible. So this is a next problem with the first attempt.

additional

In some sources they use >! for RAlt, but no luck with this either.

sources

original source on autohotkey.com and some discussions about RAlt key combinations here.

Upvotes: 1

Views: 897

Answers (1)

Lifeweaver
Lifeweaver

Reputation: 1042

Does this work for your purposes?

*RAlt::
Hotkey, *a, label_a, On
Hotkey, y, label_y, On
return

*RAlt Up::
Hotkey, *a, label_a, Off
Hotkey, y, label_y, Off
return

label_a:
if(GetKeyState("Shift", "P")) {
    Send, Ä
} else {
    Send, ä
}
return

label_y:
Send, ¥
return

The '*', wildcard, lets the hotkey fire even if extra modifiers are being held down. The inside the label it checks if the shift is pressed down or not.

Upvotes: 1

Related Questions