c# Sending keyboard commands to another window / process

后端 未结 3 1366
温柔的废话
温柔的废话 2020-12-01 07:58

I am trying to write a program that will take a line of data and pass it into another window / process.

This is the code I have so far, but I have not been able to w

相关标签:
3条回答
  • 2020-12-01 08:35
    [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.

    0 讨论(0)
  • 2020-12-01 08:38

    Considering that you know when and what keyboard command you gonna send to Outlook process, you need to use SendMessage Windows API function.

    Just a sample

    0 讨论(0)
  • 2020-12-01 08:51

    I've written a couple of programs that send keystrokes to background windows, I generally implemented 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.

    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. This method also allows for mouse use in a background window :)

    All code was checked into GitHub: https://github.com/EasyAsABC123/Keyboard

    0 讨论(0)
提交回复
热议问题