如何将击键发送到窗口而不必使用 Windows API 激活它? [英] How do I send key strokes to a window without having to activate it using Windows API?

查看:46
本文介绍了如何将击键发送到窗口而不必使用 Windows API 激活它?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

    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.

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

    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.

推荐答案

好吧,我敢肯定,这有点令人失望,但您从根本上不能以 100% 的可靠性做到这一点.

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

Windows 假定活动窗口是获取键盘输入的窗口.伪造键盘输入的正确方法是使用 SendInput,您会注意到它只向活动窗口发送消息.

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.

话虽如此,您可以SendMessage WM_KEYUP, WM_CHARWM_KEYDOWN 消息和(取决于接收它们的 WndProc)可能会侥幸逃脱.但请记住,它的在某些情况下会中断.

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.

这篇关于如何将击键发送到窗口而不必使用 Windows API 激活它?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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