繁体   English   中英

c# 向另一个发送键盘命令 window / 进程

[英]c# Sending keyboard commands to another window / process

我正在尝试编写一个程序,它将获取一行数据并将其传递给另一个 window / 进程。

这是我到目前为止的代码,但我无法弄清楚如何将键盘命令发送到 OUTLOOK 进程。

我希望能够使用 Tab 命令/键和 Enter 命令/键。

到目前为止,这是我尝试过的

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();
    }
}

试试这样的smth。

我编写了几个向后台窗口发送按键的程序,我通常实现了PostMessage / SendMessage。 在这里记录了我的所有发现!

但是你基本上会使用低级别的c调用将消息放入windows消息队列,以允许应用程序获取按键。

PostMessage的

发信息

如果您有任何问题,请告诉我,我的图书馆是用C#编写的,我很乐意与您分享。 此方法还允许在后台窗口中使用鼠标:)

所有代码都被检入GitHub: https//github.com/EasyAsABC123/Keyboard

SendMessage是您正在寻找的。 也许你的问题在这里已经解决了: 如何在C#/ Win32中将文本发送到记事本?

考虑到你知道什么时候什么键盘命令你要发送到Outlook过程中,你需要使用的SendMessage Windows API函数。

只是一个样本

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM