开发者

C# InputSimulator wrapper - how to use it?

I want to simulate keyboard click for a external program.I've tried SendMessage, PostMessage, SendKeys but they do not send the key to one specific program. So i wanted to try SendInput and i have downloaded a good wrapper for SendInput - http://inputsimulator.codeplex.com/

i have added the assembly to my project but i cannot yet start using any of the functions...

What i have to do? What "Using" should 开发者_StackOverflow社区i add?


I believe you need a

Using WindowsInput; 

If that's not it, you can view it in the Object Browser and see the namespace. Just right click the reference in solution explorer and click browse.


You can simulate keyboard input to a program like this:

  • bring the program you want to send keys to the foreground using SetForegroundWindow from user32.dll

  • use the SendKeys.SendWait Method to send the actual key to the program window

Example code (launch notepad before testing):

using System;
using System.Windows.Forms;
using System.Runtime.InteropServices;

namespace SendKeyboardInput
{
    public class SendKey
    {
        [DllImport("user32.dll")]
        public static extern bool SetForegroundWindow(IntPtr hWnd);

        public void Send()
        {
            System.Diagnostics.Process[] p = System.Diagnostics.Process.GetProcessesByName("notepad"); //search for process notepad
            if (p.Length > 0) //check if window was found
            {
                SetForegroundWindow(p[0].MainWindowHandle); //bring notepad to foreground
            }

            SendKeys.SendWait("a"); //send key "a" to notepad
        }
    }
}


I had the same problem but i managed to do it by following these 3 steps

  1. Extract the InputSimulator contents somewhere sensible like your project URL

  2. In Visual Studio Click Project->Add Reference and browse to the InputSimulator DLL file

  3. Add the WindowsInput Namespace to the project by adding "using WindowsInput;"


While I can't tell you what using directives you might need, I'm not sure this tool is going to allow you to send input to a specific window. The "History" section of the page you linked states:

It was originally written for use in the WpfKB (WPF Touch Screen Keyboard) project to simulate real keyboard entry to the active window.

The only solution to this problem I am aware of involves SendMessage, maybe you could further explain where the problem was with that?

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜