5

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?

TreantBG
  • 1,192
  • 6
  • 25
  • 44
  • "What "Using" should i add". If you use VS, just write in the code InputSimulator and intellisence will suggest you namespaces. Isn't it? – Tigran Jul 07 '11 at 13:40

4 Answers4

6

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.

Web
  • 1,735
  • 2
  • 22
  • 36
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
        }
    }
}
Răzvan Flavius Panda
  • 21,730
  • 17
  • 111
  • 169
1

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;"
Arturski
  • 1,142
  • 3
  • 14
  • 26
0

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?

Kevin Wienhold
  • 331
  • 1
  • 4