如何将击键发送到窗口而无需使用 Windows API 激活它?

发布于 2024-07-29 08:03:49 字数 426 浏览 10 评论 0 原文

    我已经制作了一个将命令发送到激活窗口的应用程序。 我希望能够在进程运行时使用计算机,因为一旦我将焦点切换到另一个窗口,通过发送键发送的击键将转到我刚刚切换到的窗口。

    目前我使用 Windows API 中的 FindWindow、IsIconic 和 ShowWindow。 我必须使用 FindWindow 检查窗口是否存在,并将我的对象设置为该调用返回的特定窗口,然后检查它是否使用 IsIconic 最小化并调用 ShowWindow 如果是,最后我必须调用Interaction.AppActivate 将焦点设置到该窗口。 所有这些都是在我发送击键之前完成的。 似乎应该有一种方法可以只发送击键,而不必显示窗口并激活它。 最重要的是,当我的应用程序运行击键时,我无法在计算机上执行任何操作。

    I have made an application already that sends commands to an activated window. I want to be able to use the computer while my process is running because as soon as I switch focus to another window the key strokes being sent via send keys will go to the window I just switched to.

    Currently I use FindWindow, IsIconic, and ShowWindow from the Windows API. I have to check to see if the window is there with FindWindow and set my object to the specific window that is returned with that call, I then check if it's minimized with IsIconic and call ShowWindow if it is, and then finally I have to call Interaction.AppActivate to set focus to that window. All of this is done before I even send key strokes. Seems like there should be a way to just send key strokes without having to show the window and activate it. The big thing is while my application is running the key strokes I can't do anything on my computer.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

夏天碎花小短裙 2024-08-05 08:03:49

好吧,我确信这有点令人失望,但从根本上来说您无法以 100% 的可靠性做到这一点

Windows 假定活动窗口是接收键盘输入的窗口。 伪造键盘输入的正确方法是使用 SendInput ,您会注意到它仅将消息发送到活动窗口。

话虽这么说,您可以 SendMessage WM_KEYUPWM_CHARWM_KEYDOWN 消息(取决于接收它们的 WndProc)可能会逃脱惩罚。 但请记住,在某些情况下,它会崩溃

Alright, this is kind of disappointing I'm sure, but you fundamentally cannot do this with 100% reliability.

Windows assumes that the active window is the one getting keyboard input. The proper way to fake keyboard input is with SendInput, and you'll notice that it sends messages to the active window only.

That being said, you can SendMessage WM_KEYUP, WM_CHAR, and WM_KEYDOWN messages and (depending on the WndProc receiving them) maybe get away with it. But remember, its going to break under some circumstances, period.

谁的新欢旧爱 2024-08-05 08:03:49

听起来您正在使用 keybd_event() 或 SendInput(),它们都将击键发送到当前活动窗口。 要将击键定向到特定窗口,无论该窗口是否获得焦点,您都需要首先找到其 HWND 句柄,然后将适当格式的 WM_KEYUP/DOWN 和 WM_CHAR 消息直接发送给它。

Sounds like you are using keybd_event() or SendInput(), which both send keystrokes to the currently active window. To direct keystrokes to a specific window, regardless of whether that widnow is focused or not, you need to find its HWND handle first, and then post appropriately-formatted WM_KEYUP/DOWN and WM_CHAR messages directly to it.

纵山崖 2024-08-05 08:03:49

一旦你有了Windows HWND,你可以直接SendMessage()将WM_KEYDOWN和WM_KEYUP消息发送到它的消息队列。 该窗口不必处于活动状态。

但是,请理解这取决于目标应用程序处理键盘输入的方式。 有几种不同的方法可以处理它。

WM_KEYUP/WM_KEYDOWN 最常见,某些应用程序仅处理其中之一(通常是 WM_KEYDOWN)。

WM_CHAR 也相当常见。

一些程序使用 GetAsyncKeyState、GetKeyState 或 GetKeyboardState。 这是极其不寻常的,但可以有效地防止使用 SendMessage() 进行按键注入。 如果是这种情况,则回退到由键盘驱动程序直接处理的 keybd_event() 。 当然,窗口必须处于活动状态

once you have the windows HWND, you can directly SendMessage() the WM_KEYDOWN and WM_KEYUP messages to its message queue. The window does not have to be active.

However, understand that this depends on how the target application processes keyboard input. There are several different ways to handle it.

WM_KEYUP/WM_KEYDOWN is most common and some applications only process one or the other (usually WM_KEYDOWN).

WM_CHAR is also fairly common

Some programs use GetAsyncKeyState, GetKeyState, or GetKeyboardState. This is extremely unusual, but effectively prevents keypress injection with SendMessage(). If this is the case fall back to keybd_event() which is directly handled by the keyboard driver. Of course the window will have to be active

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文