简体   繁体   English

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

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

I am trying to write a program that will take a line of data and pass it into another window / process.我正在尝试编写一个程序,它将获取一行数据并将其传递给另一个 window / 进程。

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.这是我到目前为止的代码,但我无法弄清楚如何将键盘命令发送到 OUTLOOK 进程。

I would like to be able to use the Tab command / key and the Enter command / key.我希望能够使用 Tab 命令/键和 Enter 命令/键。

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

Try smth like this. 试试这样的smth。

I've written a couple of programs that send keystrokes to background windows, I generally implemented PostMessage/SendMessage. 我编写了几个向后台窗口发送按键的程序,我通常实现了PostMessage / SendMessage。 I documented all my findings here ! 在这里记录了我的所有发现!

But you will basically be using a low level c call to put messages into the windows message queue to allow the application to pick up the key presses. 但是你基本上会使用低级别的c调用将消息放入windows消息队列,以允许应用程序获取按键。

PostMessage PostMessage的

SendMessage 发信息

Please let me know if you have any questions, my library is written in C# and i'd be happy to share it. 如果您有任何问题,请告诉我,我的图书馆是用C#编写的,我很乐意与您分享。 This method also allows for mouse use in a background window :) 此方法还允许在后台窗口中使用鼠标:)

All code was checked into GitHub: https://github.com/EasyAsABC123/Keyboard 所有代码都被检入GitHub: https//github.com/EasyAsABC123/Keyboard

SendMessage is what you're looking for. SendMessage是您正在寻找的。 Maybe your question was already solved here: How to send text to Notepad in C#/Win32? 也许你的问题在这里已经解决了: 如何在C#/ Win32中将文本发送到记事本?

Considering that you know when and what keyboard command you gonna send to Outlook process, you need to use SendMessage Windows API function. 考虑到你知道什么时候什么键盘命令你要发送到Outlook过程中,你需要使用的SendMessage Windows API函数。

Just a sample 只是一个样本

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

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