问题
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 i add?
回答1:
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.
回答2:
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
}
}
}
回答3:
I had the same problem but i managed to do it by following these 3 steps
Extract the InputSimulator contents somewhere sensible like your project URL
In Visual Studio Click Project->Add Reference and browse to the InputSimulator DLL file
- Add the WindowsInput Namespace to the project by adding "using WindowsInput;"
回答4:
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?
来源:https://stackoverflow.com/questions/6611442/c-sharp-inputsimulator-wrapper-how-to-use-it