c# Sending keyboard commands to another window / process(c# 将键盘命令发送到另一个窗口/进程)
问题描述
我正在尝试编写一个程序,它将获取一行数据并将其传递到另一个窗口/进程.
I am trying to write a program that will take a line of data and pass it into another window / process.
这是我目前拥有的代码,但我无法弄清楚如何将键盘命令发送到 OUTLOOK 进程.
This is the code I have so far, but I have not been able to work out how I would send the keyboard command to the OUTLOOK process.
我希望能够使用 Tab 命令/键和 Enter 命令/键.
I would like to be able to use the Tab command / key and the Enter command / key.
这是我迄今为止尝试过的
This is what I have tried so far
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
using System.Threading;
using System.Runtime.InteropServices;
using System.Diagnostics;
using System.Windows.Forms;
namespace Config
{
class Program
{
[STAThread]
static void Main(string[] args)
{
System.Threading.Thread.Sleep(30);//300000
TextReader tr = new StreamReader("config.txt");
Clipboard.SetText(tr.ReadLine());
tr.Close();
var proc = Process.GetProcessesByName("OUTLOOK").FirstOrDefault();
if (proc != null && proc.MainWindowHandle != IntPtr.Zero)
{
SetForegroundWindow(proc.MainWindowHandle);
//SendKeys.Send("{ENTER}");
// Clipboard.GetText();
}
}
[DllImport("user32")]
private static extern bool SetForegroundWindow(IntPtr hwnd);
}
}
推荐答案
[DllImport("user32.dll")]
private static extern IntPtr FindWindow(string lpClassName, string lpWindowName);
[DllImport("user32.dll")]
private static extern bool SetForegroundWindow(IntPtr hWnd);
public void Start()
{
IntPtr zero = IntPtr.Zero;
for (int i = 0; (i < 60) && (zero == IntPtr.Zero); i++)
{
Thread.Sleep(500);
zero = FindWindow(null, "YourWindowName");
}
if (zero != IntPtr.Zero)
{
SetForegroundWindow(zero);
SendKeys.SendWait("{TAB}");
SendKeys.SendWait("{TAB}");
SendKeys.SendWait("{ENTER}");
SendKeys.Flush();
}
}
像这样试试.
这篇关于c# 将键盘命令发送到另一个窗口/进程的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:c# 将键盘命令发送到另一个窗口/进程


基础教程推荐
- 如何激活MC67中的红灯 2022-01-01
- rabbitmq 的 REST API 2022-01-01
- 将 XML 转换为通用列表 2022-01-01
- 为什么Flurl.Http DownloadFileAsync/Http客户端GetAsync需要 2022-09-30
- 将 Office 安装到 Windows 容器 (servercore:ltsc2019) 失败,错误代码为 17002 2022-01-01
- MS Visual Studio .NET 的替代品 2022-01-01
- 如何在 IDE 中获取 Xamarin Studio C# 输出? 2022-01-01
- c# Math.Sqrt 实现 2022-01-01
- 有没有办法忽略 2GB 文件上传的 maxRequestLength 限制? 2022-01-01
- SSE 浮点算术是否可重现? 2022-01-01