Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>I found a similar question that contained an answer that assisted <a href="https://stackoverflow.com/questions/18917716/windows-how-to-query-state-of-modifier-keys-within-low-level-keyboard-hook">here</a>. This led me down the right path to a solution.</p> <p>It appears Keyboard.Modifiers can't detect the windows key (at least on my Win8 instance).</p> <p>Instead I had to handle the windows key in a different way using <code>Keyboard.IsKeyDown</code>. This led to build a method to check whether the combination of a key pressed (from my LowLevelKeyboardProc) and the current modifier keys being pressed is the same as the defined key and modifiers in properties <code>HotKey</code> and <code>HotKeyModifiers</code> .</p> <p>Here's the C# of the LowLevelKeyboardProc:</p> <pre><code>/// &lt;summary&gt; /// Called by windows when a keypress occurs. /// &lt;/summary&gt; /// &lt;param name="nCode"&gt;A code the hook procedure uses to determine how to process the message. If nCode is less than zero, the hook procedure must pass the message to the CallNextHookEx function without further processing and should return the value returned by CallNextHookEx.&lt;/param&gt; /// &lt;param name="wParam"&gt;The identifier of the keyboard message. This parameter can be one of the following messages: WM_KEYDOWN, WM_KEYUP, WM_SYSKEYDOWN, or WM_SYSKEYUP. &lt;/param&gt; /// &lt;param name="lParam"&gt;A pointer to a KBDLLHOOKSTRUCT structure. &lt;/param&gt; private IntPtr HookCallback(int nCode, IntPtr wParam, IntPtr lParam) { if (nCode &gt;= 0 &amp;&amp; (wParam == (IntPtr)WM_KEYDOWN || wParam == (IntPtr)WM_SYSKEYDOWN)) { int vkCode = Marshal.ReadInt32(lParam); var keyPressed = KeyInterop.KeyFromVirtualKey(vkCode); if (IsKeyCombinationPressed(keyPressed)) OnKeyCombinationPressed(new EventArgs()); } return CallNextHookEx(_hookId, nCode, wParam, lParam); } </code></pre> <p>And the C# for the IsKeyCombinationPressed() method:</p> <pre><code>/// &lt;summary&gt; /// Returns true if the registered key combination is pressed /// &lt;/summary&gt; /// &lt;remarks&gt; /// Keyboard.Modifiers doesn't pick up the windows key (at least on Windows 8) so we use Keyboard.IsKeyDown to detect it (if required). /// &lt;/remarks&gt; bool IsKeyCombinationPressed(Key keyPressed) { if (keyPressed != HotKey) return false; //Handle windows key bool isWindowsKeyRequired = (HotKeyModifiers &amp; ModifierKeys.Windows) != 0; bool isWindowsKeyPressed = Keyboard.IsKeyDown(Key.LWin) || Keyboard.IsKeyDown(Key.RWin); //Remove windows key from modifiers (if required) ModifierKeys myModifierKeys = isWindowsKeyRequired ? HotKeyModifiers ^ ModifierKeys.Windows : HotKeyModifiers; bool isModifierKeysPressed = Keyboard.Modifiers == myModifierKeys; return isWindowsKeyRequired ? isWindowsKeyPressed &amp;&amp; isModifierKeysPressed : isModifierKeysPressed; } </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload