Automating keyboard keypress and mouse clicks with AutoIt3

Many years ago I was trying to implement a “turbo controller” for ePSXe (Playstation emulator). I attempted to use `SendKeys` with no luck. Later I heard about `SendInput` which didn’t work on the emulator but worked on some other games.

Today, after 5 years or so, while I was browsing Stack Overflow unanswered questions for c# page 96 I saw this question: Invoke keyboard event in c#

I saw comments saying that he tried SendKeys, SentInput and it didn’t work, but then he tried AutoIt3 and magic! it worked!

I downloaded it, installed, created a project and added a reference to:

\Program Files\AutoIt3\AutoItX\AutoItX3.dll

And started coding as fast as I could. It is quite simple actually, you just have to call the Send command.

AutoItX3Lib.IAutoItX3 a = new AutoItX3Lib.AutoItX3();

a.Send("{NUMPAD1}");

And YEAH! BITCH! MAGIC! OHH!

It worked, after 5 years!

I highly recommend this software/DLL if you are interested in automating anything, it has tons of methods to manipulate windows, mouse, keyboard.


And just for the record, the class I created to use SendInput, which is not so great, but anyway:

public class KeyboardManager
{
    public const int INPUT_KEYBOARD = 1;
    public const int KEYEVENTF_KEYUP = 0x0002;

    public struct KEYDBINPUT
    {
        public Int16 wVk;
        public Int16 wScan;
        public Int32 dwFlags;
        public Int32 time;
        public Int32 dwExtraInfo;
        public Int32 __filler1;
        public Int32 __filler2;
    }

    public struct INPUT
    {
        public Int32 type;
        public KEYDBINPUT ki;
    }

    [DllImport("user32")]
    public static extern int SendInput(int cInputs, ref INPUT pInputs, int cbSize);

    public static void HoldKey(Keys vk)
    {
        INPUT input = new INPUT();
        input.type = INPUT_KEYBOARD;
        input.ki.dwFlags = 0;
        input.ki.wVk = (Int16)vk;
        SendInput(1, ref input, Marshal.SizeOf(input));
    }

    public static void ReleaseKey(Keys vk)
    {
        INPUT input = new INPUT();
        input.type = INPUT_KEYBOARD;
        input.ki.dwFlags = KEYEVENTF_KEYUP;
        input.ki.wVk = (Int16)vk;
        SendInput(1, ref input, Marshal.SizeOf(input));
    }

    public static void PressKey(Keys vk)
    {
        HoldKey(vk);
        ReleaseKey(vk);
    }
}
Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s