How to convert a Right Control into AltGr?

Get help with using AutoHotkey (v1.1 and older) and its commands and hotkeys
User avatar
jackcalico
Posts: 6
Joined: 18 Sep 2021, 08:42

How to convert a Right Control into AltGr?

29 Nov 2021, 06:10

Hi, I would like to convert the right Control keypress into a AltGr keypress.

I thought this would work, but it doesn't:

Code: Select all

; RightControl as AltGr
RCtrl::Send <^>!
Am I doing something wrong here? Thanks!
User avatar
mikeyww
Posts: 26972
Joined: 09 Sep 2014, 18:38

Re: How to convert a Right Control into AltGr?

29 Nov 2021, 08:19

Code: Select all

RCtrl::
Send {LCtrl down}{RAlt down}
SoundBeep, 1500
Return
<^>!RCtrl Up::
Send {RAlt up}{LCtrl up}
SoundBeep, 1000
Return
Three principles:
1. With Send, those symbols are modifiers-- meaning that they modify other keys that must be listed. Explained: Send
The characters ^+!# represent the modifier keys Ctrl, Shift, Alt and Win. They affect only the very next key. To send the corresponding modifier key on its own, enclose the key name in braces. To just press (hold down) or release the key, follow the key name with the word "down" or "up" as shown below.
2. Without a dedicated sending of "down", the key will be sent down and then up.
3. While modifier keys are held or sent down, other hotkeys must "recognize" (include) them.
User avatar
jackcalico
Posts: 6
Joined: 18 Sep 2021, 08:42

Re: How to convert a Right Control into AltGr?

29 Nov 2021, 10:04

Not sure if this makes sense, but rather than using the Send function I just have done a simple remapping:

RCtrl::RAlt

Note: I have used RAlt and not AltGr, but it seems to be working just fine.

What I am noticing, however, is that all the instructions I add below break that previous instruction.

For example, if my .ahk file looks like:

Code: Select all

RCtrl::RAlt
When I press RCtrl and E I get the € symbol.

However, if my .ahk file looks like:

Code: Select all

RCtrl::RAlt
>^Esc::Send \
What ends up happening is that if I press RCtrl and Esc I get the \ symbol, but I no longer get the € symbol when pressing RCtrl and E.

Am I missing something here?
User avatar
mikeyww
Posts: 26972
Joined: 09 Sep 2014, 18:38

Re: How to convert a Right Control into AltGr?

29 Nov 2021, 10:43

My understanding is that RCtrl has some special handling in Windows that may relate to built-in Windows docking hotkeys.
lexikos
Posts: 9592
Joined: 30 Sep 2013, 04:07
Contact:

Re: How to convert a Right Control into AltGr?

30 Nov 2021, 03:45

As far as I can recall, I have never before heard of any special handling of RCtrl in Windows.

RAlt is special when the active keyboard layout has AltGr. AltGr presents as a combination of LCtrl and RAlt, but this is achieved by the system injecting LCtrl key-down and key-up events at a low level whenever an RAlt event is processed, even if it is artificial (i.e. sent). It is also possible to send LCtrl manually before RAlt, but it is more likely to result in a modifier stuck in the wrong state.

The issue with >^Esc is due to some (probably) undocumented behaviour that I recently came to understand better: >^Esc:: marks RCtrl as a "prefix key" in the same sense as RCtrl & Esc::.
Custom Combinations
You can define a custom combination of two keys (except joystick buttons) by using " & " between them. In the below example, you would hold down Numpad0 then press the second key to trigger the hotkey:

Code: Select all

Numpad0 & Numpad1::MsgBox You pressed Numpad1 while holding down Numpad0.
Numpad0 & Numpad2::Run Notepad
The prefix key loses its native function: In the above example, Numpad0 becomes a prefix key; but this also causes Numpad0 to lose its original/native function when it is pressed by itself.
...
Fire on release: The presence of one of the above custom combination hotkeys causes the release of Numpad0 to perform the indicated action, but only if you did not press any other keys while Numpad0 was being held down.

The presence of >^Esc causes RCtrl to become a "prefix key", which causes it to fire on release. If you replace RAlt with MsgBox, you should notice that the message is displayed when you release RCtrl, unless you remove >^Esc::Send \.

It is also worth noting that the "loses its native function" part does not apply to the standard modifier keys as they have special handling. In other words, AppsKey & Esc:: would prevent AppsKey from showing a context menu (which is its normal function), but RCtrl & Esc:: would not prevent you from using system- or application-defined shortcuts, or using RCtrl itself, such as in a game.

Applying modifier symbols such as >^ and ! to hotkeys causes them to require a specific logical key state. If you remap RCtrl to AltGr, the logical state while it is held should be LCtrl and RAlt pressed, with RCtrl not pressed. So if these special cases didn't exist, >^Esc:: would not work at all with RCtrl::RAlt, unless you have some other key::RCtrl (and unlike the current behaviour, it would be consistent with keyboard shortcuts defined by other scripts or applications).

The solution is to use <^>!Esc::. Because you will no longer have a hotkey with >^, RCtrl::RAlt will work as intended. When you press RCtrl, it will put LCtrl and RAlt into effect, the same as if you pressed AltGr.
User avatar
mikeyww
Posts: 26972
Joined: 09 Sep 2014, 18:38

Re: How to convert a Right Control into AltGr?

30 Nov 2021, 07:44

OK. That is very helpful. I had read something earlier about RAlt, but I guess it was incorrect. It could be useful to get some of these details into the AHK documentation in some form.

Return to “Ask for Help (v1)”

Who is online

Users browsing this forum: Araphen, Google [Bot], Peiya, ShatterCoder and 301 guests