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
#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.


Messages In This Thread
RE: Listen for key combinations, such as Ctrl+C and Ctrl+Win+Right Arrow - by Gintaras - 02-25-2025, 11:50 AM

Forum Jump:


Users browsing this thread: 1 Guest(s)