C# InputSimulator wrapper - how to use it?
Asked Answered
G

4

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?

Gonagle answered 7/7, 2011 at 13:34 Comment(1)
"What "Using" should i add". If you use VS, just write in the code InputSimulator and intellisence will suggest you namespaces. Isn't it?Ingenerate
W
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.

Waitabit answered 7/7, 2011 at 13:37 Comment(1)
Yes that is it. I tank you very much :)Gonagle
N
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
        }
    }
}
Needlework answered 7/7, 2011 at 14:31 Comment(0)
E
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;"
Entomologize answered 16/8, 2013 at 15:3 Comment(0)
M
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?

Maxilliped answered 7/7, 2011 at 13:47 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.