SendKeys到无效的应用程序 [英] SendKeys to inactive application

查看:64
本文介绍了SendKeys到无效的应用程序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图弄清楚如何使我的C#应用​​程序将键发送到另一个应用程序窗口,而无需关注它.我有一个单词列表和3个记事本文件.想象一下,我已经按顺序打开了所有3个记事本窗口,我的程序将把列表框中的第一个单词写到第一个记事本窗口中.第二个记事本窗口中的第二个单词,第三个记事本窗口中的第三个单词.然后重新开始并继续.因此,我希望它在每个单词中张贴1个单词,然后一遍又一遍地继续下去.

I am trying to figure out how I can make my C# application to send keys to another application window, without focusing on it. I have a list of words and 3 notepads files. Imagine I have all 3 notepad windows opened in order, and my program would take the first word in the listbox and write it in the first Notepad window. The second word in the second Notepad window and third one in the third Notepad window. Then start over and continue. So I want it to post 1 word in each and continue like that over and over.

但是我只想出了如何使用1个记事本窗口(处于活动状态)进行操作.

But I only figured out how to do it with 1 notepad window (while having it active).

int i = 0;
i = listBox1.SelectedIndex;
i = i + 1;
if (i > listBox1.Items.Count - 1)
    i = 0;
listBox1.SelectedIndex = i;
string message = listBox1.SelectedItem.ToString();

SendKeys.Send(message);
SendKeys.Send("{ENTER}");

这将需要我先将焦点放在记事本窗口上,启动计时器,然后再将焦点放在记事本窗口上.然后,它将仅遍历列表并键入单词(每行1个).而且效果很好.但我希望能够在3个窗口上完成该操作.

This would require me to first focus on the notepad window, start the timer and then keep focus on the notepad window. It would then just loop through the list and type the word (1 on each line). And it works fine. But I want to be able to do it on 3 windows.

然后我需要以某种方式获取窗口标题,而不是进程名称.然后,我将有3个名为Notepad的进程.但是,如果将它们分别命名为Note1,Note2,Note3,我该怎么做?

And then I need to get the window title somehow, not the process name. I would have 3 processes called Notepad then. But if they are named Note1, Note2, Note3 how would I do that?

我需要帮助来列出要写入哪些程序的列表:

I need help to make some kind of list of what programs to write to:

listofprograms = ["Note1", "Note2", "Note3"];

它将发现使用这些名称打开的应用程序窗口,然后以某种方式将文本写入这些窗口.

And it would find the application windows opened with those names, then somehow write the text into those windows.

有人可以帮我吗?到目前为止,还没有发现任何东西,请相信我,我已经环顾四周了!

Could anyone help me out? Haven't found anything about this so far and trust me I've looked around!

推荐答案

不幸的是,没有很好的方法可以做到这一点. SendKeys 是一个非常简单且理想的API,但仅适用于活动窗口.无法使其在不活动的窗口上工作,也没有与在不活动的窗口上工作一样容易访问的API.

Unfortunately there is no great way to do this. SendKeys is a really simple and desirable API but it only applies to the active window. There is no way to make it work on an inactive window nor is there an API with the same ease of access that works on inactive windows.

通常,当人们遇到这个问题时,他们必须走两条路线之一

Generally when people run up against this problem they have to go one of two routes

  1. 您希望一次一次将密钥发送给一个的应用程序处于活动状态
  2. 下拉至更低级别的 SendMessage PostMessage 发送键盘事件
  1. Physically active the apps you want to send the keys to one at a time
  2. Drop down to a much lower level of SendMessage or PostMessage to send keyboard events

第二个选项通常更可靠,但更难以实现.

The second option is generally more reliable but harder to implement.

这篇关于SendKeys到无效的应用程序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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