Note that there are some explanatory texts on larger screens.

plurals
  1. POHow can I deal with depressed Windows logo key when using `SendInput`?
    text
    copied!<p>My application synthesises keystrokes in other apps by using <code>SendInput</code>. This almost works, except that due to circumstances I cannot change, the left Windows key will always be physically depressed when sending the input. This means that if, for example, the keystroke being sent is the 'd' key, then Windows sees the keystroke as its Win+D shortcut and displays the Desktop, and the 'd' keystroke never makes it to its intended target.</p> <p>The documentation mentions this problem but is frustratingly vague about how to fix it (emphasis mine):</p> <blockquote> <p>This function does not reset the keyboard's current state. Any keys that are already pressed when the function is called might interfere with the events that this function generates. To avoid this problem, check the keyboard's state with the <code>GetAsyncKeyState</code> function and <strong>correct as necessary</strong>.</p> </blockquote> <p>How should I "correct as necessary"?</p> <p>I've tried using <code>SetKeyboardState</code> to turn off the keyboard state of the <code>VK_LWIN</code> key, after first attaching my thread to the thread of the window that I am sending the keystrokes to (with <code>AttachThreadInput</code>), but this doesn't seem to affect the behaviour at all.</p> <p>I've also tried inserting a <code>KEYEVENTF_KEYUP</code> event at the start of the input I am sending with <code>SendInput</code>, but this causes the Start Menu to pop up and grab focus, which is obviously not acceptable.</p> <p>What is the correct method of dealing with a depressed Windows logo key when calling SendInput?</p> <p><strong>EDIT:</strong></p> <p>As per <a href="https://stackoverflow.com/questions/2914989/how-can-i-deal-with-depressed-windows-logo-key-when-using-sendinput/2915406#2915406">@Brian's answer</a> I can prevent the win key from interfering with my code by installing a low-level keyhook which traps win-key keydown events.</p> <p>This, however, has the undesired effect that the Windows key is now disabled entirely. I can sort of envisage some solution to this where I keep a note of the state of all the keys within my keyhook, and simulate presses of the windows key when other keys are pressed, depending on whether or not they are the ones I'm simulating, but that sounds like a hack to me. Alternatively, I could use my keyhook to emulate important windows keyboard shortcuts.</p> <p>But the documentation implies it should all work in a much more straightforward way. Brian's solution is to prevent the win key from getting to the pressed down state in the first place. But the documentation states that you should be able to check if it's pressed down later on, and correct it if it is.</p> <p>Is it actually possible to do this?</p>
 

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