static void _Register(ref RegisteredHotkey rk, ERegisteredHotkeyId id, string keys, string menu) //ref, not in! { if (!keys.NE()) { try { if (!rk.Register((int)id, keys, App.Hmain)) { print.warning($"Failed to register hotkey {keys}. Look in Options -> Hotkeys.", -1); keys = null; } } catch (Exception ex) { print.it(ex); keys = null; } } App.Commands[menu].MenuItem.InputGestureText = keys; }
/// <summary> /// Registers a temporary hotkey and waits for it. /// </summary> /// <param name="secondsTimeout">Timeout, seconds. Can be 0 (infinite), >0 (exception) or <0 (no exception). More info: [](xref:wait_timeout).</param> /// <param name="hotkey">Hotkey. Can be: string like "Ctrl+Shift+Alt+Win+K", tuple <b>(KMod, KKey)</b>, enum <b>KKey</b>, enum <b>Keys</b>, struct <b>KHotkey</b>.</param> /// <param name="waitModReleased">Also wait until hotkey modifier keys released.</param> /// <returns>Returns true. On timeout returns false if <i>secondsTimeout</i> is negative; else exception.</returns> /// <exception cref="ArgumentException">Error in hotkey string.</exception> /// <exception cref="AuException">Failed to register hotkey.</exception> /// <exception cref="TimeoutException"><i>secondsTimeout</i> time has expired (if > 0).</exception> /// <remarks> /// Uses <see cref="RegisteredHotkey"/> (API <msdn>RegisterHotKey</msdn>). /// Fails if the hotkey is currently registered by this or another application or used by Windows. Also if F12. /// <note>Most single-key and Shift+key hotkeys don't work when the active window has higher UAC integrity level (eg admin) than this process. Media keys may work.</note> /// </remarks> /// <example> /// <code><![CDATA[ /// keys.waitForHotkey(0, "F11"); /// keys.waitForHotkey(0, KKey.F11); /// keys.waitForHotkey(0, "Shift+A", true); /// keys.waitForHotkey(0, (KMod.Ctrl | KMod.Shift, KKey.P)); //Ctrl+Shift+P /// keys.waitForHotkey(0, Keys.Control | Keys.Alt | Keys.H); //Ctrl+Alt+H /// keys.waitForHotkey(5, "Ctrl+Win+K"); //exception after 5 s /// if(!keys.waitForHotkey(-5, "Left")) print.it("timeout"); //returns false after 5 s /// ]]></code> /// </example> public static bool waitForHotkey(double secondsTimeout, [ParamString(PSFormat.Hotkey)] KHotkey hotkey, bool waitModReleased = false) { if (s_atomWFH == 0) { s_atomWFH = Api.GlobalAddAtom("Au.WaitForHotkey"); } using (RegisteredHotkey rhk = default) { if (!rhk.Register(s_atomWFH, hotkey)) { throw new AuException(0, "*register hotkey"); } if (!wait.forPostedMessage(secondsTimeout, (ref MSG m) => m.message == Api.WM_HOTKEY && m.wParam == s_atomWFH)) { return(false); } } if (waitModReleased) { return(waitForNoModifierKeys(secondsTimeout, hotkey.Mod)); } return(true); }