将快速文本输入发送到另一个进程(窗口) [英] Send fast textinput to another process (Window)

查看:172
本文介绍了将快速文本输入发送到另一个进程(窗口)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在编写一个C#WPF程序,该程序将文本消息发送到另一个程序的窗口.我有一个宏程序作为键盘驱动程序(Logitech g15)的一部分,该程序已经做到了,尽管它不会将击键直接发送到进程,而是发送到当前聚焦的窗口.它运作良好,但我也需要能够从我的程序发送输入.还有其他人在使用该过程,因此从我的程序输入的文本消息必须足够快,以使我的文本不会干扰他们的输入. 问题是,当我尝试使用C#程序执行此操作时,我得到了太多的延迟.宏程序(Logitech G系列探查器)立即发送命令.我尝试了以下三个命令来发送消息以进行处理. (按从最慢到最快的顺序列出)

I am writing a C# WPF program which sends text messages to another program's window. I have a macro program as part of my keyboard drivers (Logitech g15) which already does this, though it does not send keystrokes directly to the process, but to the currently focused window. It works well but i need to be able to send inputs from my program as well. There are other people using the process so the input text messages from my program needs to be fast enough so that my text does not interfere with their input. The problem is that when I try to do this with a c# program I get too much delay. The macro program (Logitech G-Series Profiler) sends a command instantly. I have tried the following three commands for sending messages to process. (Listed by order of slowest to fastest)

SetForegroundWindow(_hWnd);
SendKeys.SendWait("{Enter}This is a keystroke input.{Enter}");

它可能是名字,但这执行命令的速度很慢,以至于我实际上可以随心所欲地跟踪文本,因为它是逐个字母地输入的.我尝试使用"SendKeys.Send"方法,但收到一条错误消息:"SendKeys无法在此应用程序内运行,因为该应用程序未处理Windows消息."

It is probably in the name, but this performs the command so slowly that I can actually follow with my eyes the text as it is input letter by letter. I have tried using the "SendKeys.Send" method but I get an error saying: "SendKeys cannot run inside this application because the application is not handling Windows messages."

PostMessage((IntPtr)_hWnd, (uint)WMessages.WM_KEYUP, (int)key, (int)key);

PostMessage有点快,但对于我的程序而言仍然不够快.除了方法在进程读取消息之前返回方法之外,这意味着两个连续的PostMessage调用可能不会发送连续的消息.

PostMessage is a bit faster but still not fast enough for the purpose of my program. Besides the method returns before the message has been read by the process, which means two sequential PostMessage calls may not send sequential messages.

SendMessage(_hWnd, 0x100, (int) VKeys.VK_2, (int) VKeys.VK_2);

这比PostMessage快,但不比Logitech的宏程序快.而且,接收程序奇怪地处理了输入,显然没有像从键盘上真正"输入那样对待它.

This is faster than the PostMessage but not nearly as fast as the macro program from Logitech. Also, the receiving program handles the input strangely, apparently not treating it the same way it does "genuine" input from the keyboard.

SetForegroundWindow(_hWnd);

const string text = "This is a keystroke input.";
IInputElement  target = Keyboard.FocusedElement;

IInputElement target = InputManager.Current.PrimaryKeyboardDevice.FocusedElement;

var routedEvent = TextCompositionManager.TextInputEvent;

target.RaiseEvent(new TextCompositionEventArgs(InputManager.Current.PrimaryKeyboardDevice, new TextComposition(InputManager.Current, target, text)) { RoutedEvent = routedEvent });

这是我尝试过的最后一件事.文本发送到进程的方式似乎是即时的.但是,由于只有将另一个程序设置为前景窗口时Keyboard.FocusedElement返回null,所以我只能将其发送到自己的程序. 如果有人可以告诉我如何获取另一个窗口的IInputElement,我当然想知道.另外,如果有人建议使用更好的发送输入方法,我非常希望听到它.

This is the last thing I have tried. It seems instant with the way the text is sent to a process. However, I have only been able to send this to my own program since Keyboard.FocusedElement returns null when I have another program set as foreground window. If someone can tell me how to get an IInputElement of another window I would sure like to know. Alternatively, if someone has a suggestion for a better method of sending input, I would dearly like to hear it.

规格:Windows 7、64位 Visual Studio 2010,框架4

Specs: Windows 7, 64bit Visual Studio 2010, Framework 4

推荐答案

首先,您是否故意在SendMessage示例中使用了WM_KEYDOWN (0x0100)而不是WM_KEYUP (0x0101)?这只会按下键,而永远不会释放它们,因此应用程序将无法正确处理它们.

First of all, are you intentionally using WM_KEYDOWN (0x0100) instead of WM_KEYUP (0x0101) in your SendMessage example? This would just press the keys, and never release them, so the application would not process them properly.

另一种值得尝试的方法是发送 WM_SETTEXT ,前提是该控件正确解释了该内容(例如编辑控件或组合框).

Another way worth trying would be to send WM_SETTEXT, assuming the control interprets it correctly (like edit controls or combo boxes).

最后一个选择是使用

A last option would be to use SendInput which synthesizes keyboard and mouse input on a very low level, but similarly to you keyboard's macro program, this requires you to activate the correct window and set the focus, which can be quite painful.

这篇关于将快速文本输入发送到另一个进程(窗口)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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