Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Listen for key combinations, such as Ctrl+C and Ctrl+Win+Right Arrow
#1
Hello,

I need to create some functions to listen for Windows key combinations, such as Ctrl+Win+Right Arrow (Desktop Next).
When I run the following program and press the keys manually, it works fine.
However, when I press the Desktop Next key(a special function key) on the keyboard, it seems only Ctrl is captured and blocked, while Win+Right Arrow is executed.
 
Code:
Copy      Help
var timeOutTime = -3;
if (keys.waitForKey(timeOutTime, KKey.Ctrl, up: false, block: true) &&
    keys.waitForKey(timeOutTime, KKey.Win, up: false, block: true) &&
    keys.waitForKey(timeOutTime, KKey.Right, up: false, block: true))
{
    print.it("Pass");
}
else
{
    print.it("Fail");
}

I can't use "
 
Code:
Copy      Help
if (keys.waitForHotkey(-3, "Ctrl+Win+Right"))
 "because it results in "Failed to register hotkey. Hot key is already registered (1409)."

Sorry, I’m a beginner. My code is based on examples from the cookbook and responses from ChatGPT.
Is there any other way to achieve this functionality? I would really appreciate it.
Thanks.
#2
Code:
Copy      Help
print.clear();
int i = C.WaitForHotkeys(-3, ["Ctrl+Win+Right", "Win+M", "Alt+9"]);
print.it(i);


static class C {
    /// <summary>
    ///
Waits for a hotkey.
    /// </summary>
    ///
<param name="timeout">Timeout in seconds. See <see cref="keys.waitForKeys"/>. If negative, no exception on timeout.</param>
    ///
<param name="hotkeys">One or more hotkeys, like <c>["Win+M", "Ctrl+Shift+Left"]</c>.</param>
    ///
<param name="block">Make the key down event invisible for other apps. Default true. Modifier keys are always visible, regardless of this.</param>
    ///
<returns>1-based index of the element in the array of hotkeys. On timeout returns 0 (if <i>timeout</i> negative; else exception).</returns>
    ///
<exception cref="TimeoutException"></exception>
    ///
<remarks>
    ///
Uses <see cref="keys.waitForKeys"/>. Works even if the hotkey is used by Windows or an app as a hotkey or trigger. Except <c>Win+L</c> and <c>Ctrl+Alt+Del</c>.
    /// </remarks>
    public static int WaitForHotkeys(Seconds timeout, string[] hotkeys, bool block = true) {
        var a = new (KKey k, KMod m)[hotkeys.Length];
        for (int i = 0; i < a.Length; i++) {
            if (!keys.more.parseHotkeyString(hotkeys[i], out a[i].m, out a[i].k)) throw new ArgumentException("Invalid hotkey string: " + hotkeys[i]);
        }

        
        int R = 0;
        keys.waitForKeys(timeout, k => {
            if (!k.IsUp && k.Mod == 0) {
                var mod = keys.getMod();
                for (int i = 0; i < a.Length; i++) {
                    if (k.Key == a[i].k && mod == a[i].m) {
                        R = i + 1;
                        return true;
                    }
                }
            }

            return false;
        },
block);
        
        return R;
    }
}

Or maybe you can use a trigger.
#3
Thank you for your assistance and detailed explanation.
This is extremely helpful to me.


Forum Jump:


Users browsing this thread: 1 Guest(s)