当使用`SendInput`时,如何处理按下的Windows标志键? [英] How can I deal with depressed Windows logo key when using `SendInput`?

查看:135
本文介绍了当使用`SendInput`时,如何处理按下的Windows标志键?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的应用程式使用 SendInput 在其他应用程式中合成键击。这几乎可以工作,除了由于情况,我不能改变,发送输入时,左Windows键将总是物理上按下。这意味着,例如,如果发送的击键是d键,那么Windows将击键作为其Win + D快捷方式,并显示桌面,并且d键击从未使其到达其预期目标。 / p>

文档提到了这个问题,但对于如何解决这个问题很困惑(重点是我):


此函数不会重置键盘的当前状态。调用函数时已按下的任何键可能会干扰此函数生成的事件。要避免此问题,请使用 GetAsyncKeyState 函数检查键盘状态,并根据需要更正


我应该如何根据需要更正?



我试过使用 SetKeyboardState 关闭 VK_LWIN 键的键盘状态,首先将我的线程附加到我发送击键的窗口的线程(与 AttachThreadInput ),但这似乎并不影响行为。



我也试过插入一个 KEYEVENTF_KEYUP 事件在输入开始时,我使用 SendInput 发送,但这会导致开始菜单弹出和抓取

当调用SendInput时,正确的处理按下的Windows标志键的方法是什么?



编辑



根据

但是,这具有不希望的效果,Windows键现在完全被禁用。我可以设想一些解决方案,我保留所有的钥匙在我的钥匙的状态,并模拟按下的Windows键,当其他键按下,这取决于他们是否是我的模拟,但这听起来像一个黑客我。或者,我可以使用我的keyhook来模拟重要的Windows键盘快捷键。



但是文档意味着它应该以更直接的方式工作。 Brian的解决方案是防止win键首先进入按下状态。但是文档声明,你应该能够检查它是否被按下后,如果是,请更正。



这是否可以做到这一点? p>

解决方案

:)我的代码是在C#,但它主要包括通过p / invoke的win32 API调用,所以应该直接翻译到C ++。



解决方案是使用低级键盘钩子拦截初始Windows键KeyDown事件(并告诉操作系统您处理它,以便它不会传递到其他应用程序和/或OS)。然后,根据操作系统,在输入之前模拟Windows键KeyUp事件。它似乎是你的中途,你只需要拦截初始的KeyDown。



我的解决方案的目标是Windows Vista和Windows 7,所以如果你' re在XP或以下,可能是你不需要发送KeyUp。这就是我将很快测试,它可能工作在两个;然而,Vista和7拦截Windows键比XP更积极,因此我的怀疑。


My application synthesises keystrokes in other apps by using SendInput. 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.

The documentation mentions this problem but is frustratingly vague about how to fix it (emphasis mine):

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 GetAsyncKeyState function and correct as necessary.

How should I "correct as necessary"?

I've tried using SetKeyboardState to turn off the keyboard state of the VK_LWIN key, after first attaching my thread to the thread of the window that I am sending the keystrokes to (with AttachThreadInput), but this doesn't seem to affect the behaviour at all.

I've also tried inserting a KEYEVENTF_KEYUP event at the start of the input I am sending with SendInput, but this causes the Start Menu to pop up and grab focus, which is obviously not acceptable.

What is the correct method of dealing with a depressed Windows logo key when calling SendInput?

EDIT:

As per @Brian's answer I can prevent the win key from interfering with my code by installing a low-level keyhook which traps win-key keydown events.

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.

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.

Is it actually possible to do this?

解决方案

I basically solved this exact problem yesterday :) My code is in C#, but it mostly consists of win32 API calls via p/invoke, so it should translate directly to C++.

The solution is to use a Low-Level Keyboard Hook to intercept the initial Windows key KeyDown event (and tell the OS that you handled it so that it won't be passed to other Applications and/or the OS). Then, depending on OS, simulate the Windows key KeyUp event before your input. It sounds like you're half-way there, you just need to intercept the initial KeyDown.

My solution is targeted at Windows Vista and Windows 7, so if you're in XP or below, it's possible that you don't need to send the KeyUp. That's what I'll be testing soon, it may work in both; however, Vista and 7 intercept the windows key more aggressively than XP, hence my suspicions.

这篇关于当使用`SendInput`时,如何处理按下的Windows标志键?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆