RAlt Hotkey Issues v2.15-beta

Get help with using AutoHotkey (v2 or newer) and its commands and hotkeys
Elequosoraptor
Posts: 18
Joined: 11 Oct 2017, 09:04

RAlt Hotkey Issues v2.15-beta

Post by Elequosoraptor » 02 Dec 2022, 06:46

Hello. I want to make RAlt function as Control, unless it is simply tapped without pressing any other keys, in which case I want it to run a custom function. So while holding it down, it acts as if Control is being held down, but on release if no other keys have been pressed it runs a custom function.

This seems straight forward, but I have run into a host of issues. I am using the keyboard hook.

Here is my current iteration of the hotkey that has the fewest bugs. Previous bugs have included either Control not working, the custom function never triggering, or Control or Alt (for some reason I couldn't figure out) being stuck down. Including KeyWait and not specifying the custom function on a RAlt up:: hotkey make things work better, though exactly why I cannot figure out.

All my keys are hot keys, which is why I'm tracking A_ThisHotkey, but the issues are present even if I leave my other scripts off.

Code: Select all

RAlt::{
    SendInput("{RControl down}")
    if(KeyWait(ThisHotkey, "T0.18")){
        KeyWait(ThisHotkey)
        SendInput("{RControl up}")
        if(ThisHotkey = A_ThisHotkey){
            ; Custom function
            toggleLayer("Numpad")
        }
    } else {
        KeyWait(ThisHotkey)
        SendInput("{RControl up}")
    }
}
For some reason, this hotkey as written has the following behavior: When I press RAlt and quickly press another key (within the timeout of KeyWait, from what I can tell), RAlt acts like Control. If I tap RAlt and release without pressing another key, the custom function runs. So far so good.

If I hold down RAlt, wait a few seconds, and then press a key (like s to save for example), nothing happens.

Experimenting in VS code I notice that when I hold down RAlt, briefly it behaves as normal, but if I continue to hold it down VSCode's menu becomes highlighted as if I was sending {RAlt down}, but never {RAlt up}. I believe that the code above, for some reason, sends {Control down}, but after the KeyWait times out, sends {RAlt down} instead. That would explain why RAlt & s sends ^s if I press the keys quickly, but not if I wait a few seconds.

I have no idea why this would be though, since I thought the point of a hotkey was to block the native function of the key unless explicitly told otherwise. I've tried variations with GetKeyState, timers, and up hotkeys, but these never seem to work out as I want. I even tried a separate higher input level script that remaps RAlt::Control, but this caused a Lot of issues with stuck down modifiers for some reason.

I do not want to remap the key in the registry, and I don't think that should be necessary given what I'm trying to do should be straightforward. I haven't tried creating a hotkey for every RAlt & key::^key combination, and frankly that seems like overkill. Any advice, suggestions, or even totally different code that accomplishes my core goal is welcome, I am well aware this probably not the most elegant way to implement this. However, it does almost work and the failures are incomprehensible.

lexikos
Posts: 9583
Joined: 30 Sep 2013, 04:07
Contact:

Re: RAlt Hotkey Issues v2.15-beta

Post by lexikos » 02 Dec 2022, 16:26

KeyHistory should give you a more accurate picture of what is happening.
I believe that the code above, for some reason, sends {Control down}, but after the KeyWait times out, sends {RAlt down} instead.
What code sends RAlt down, instead of what? A key isn't going to be sent arbitrarily at a random time.

You are holding RAlt, so after the key-repeat delay, if you have not physically pressed any other keys, the OS sends another RAlt-down event. When this occurs, RCtrl is logically down, so RAlt:: without modifiers is not eligible to fire. You probably just need to change it to *RAlt:: and adjust KeyWait to compensate for the "*".

I would suggest removing the redundant calls.

Code: Select all

*RAlt::{
    SendInput("{RControl down}")
    if !(released := KeyWait("RAlt", "T0.18"))
        KeyWait("RAlt")
    SendInput("{RControl up}")
    if(released && ThisHotkey = A_ThisHotkey){
        ; Custom function
        toggleLayer("Numpad")
    }
}
This is another way to implement it:

Code: Select all

*RAlt::{
    SendInput("{Blind}{RControl downR}")
    KeyWait("RAlt") ; Prevent key-repeat from resetting the timing
}
*RAlt up::{
    SendInput("{Blind}{RControl up}")
    if(A_PriorHotkey = "*RAlt" && A_TimeSincePriorHotkey <= 180){
        ; Custom function
        toggleLayer("Numpad")
    }
}
DownR allows the hotkey to act more like the real RCtrl in combination with other hotkeys which Send, while Blind allows it to be used in combination with other modifier keys.


Also, "v2.15-beta" doesn't exist; I presume you are using v2.0-beta.15, and would wonder why, given that it has a few serious bugs that were fixed by v2.0-rc.1.

Elequosoraptor
Posts: 18
Joined: 11 Oct 2017, 09:04

Re: RAlt Hotkey Issues v2.15-beta

Post by Elequosoraptor » 03 Dec 2022, 14:53

The wildcard modifier was perfect, and I appreciate the refactoring! Looks to be working as expected now thank you.

I just switched to the updated version, I haven't been closely tracking the v2 development, which is why I was still on .15

Post Reply

Return to “Ask for Help (v2)”