Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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 i add?

like image 332
TreantBG Avatar asked Jul 07 '11 13:07

TreantBG


People also ask

What is C in simple words?

C Introduction C is a general-purpose programming language created by Dennis Ritchie at the Bell Laboratories in 1972. It is a very popular language, despite being old. C is strongly associated with UNIX, as it was developed to write the UNIX operating system.

Is C language easy?

C is a general-purpose language that most programmers learn before moving on to more complex languages. From Unix and Windows to Tic Tac Toe and Photoshop, several of the most commonly used applications today have been built on C. It is easy to learn because: A simple syntax with only 32 keywords.

What is C language used for?

C programming language is a machine-independent programming language that is mainly used to create many types of applications and operating systems such as Windows, and other complicated programs such as the Oracle database, Git, Python interpreter, and games and is considered a programming foundation in the process of ...

What is %d in C programming?

In C programming language, %d and %i are format specifiers as where %d specifies the type of variable as decimal and %i specifies the type as integer. In usage terms, there is no difference in printf() function output while printing a number using %d or %i but using scanf the difference occurs.


2 Answers

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.

like image 122
Web Avatar answered Sep 29 '22 23:09

Web


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
        }
    }
}
like image 23
Răzvan Flavius Panda Avatar answered Sep 29 '22 23:09

Răzvan Flavius Panda