Search
 
 

Display results as :
 


Rechercher Advanced Search

Latest topics
» ~=?{The future is here, TastyExploit officially open to the public}?=~
Tue Mar 02, 2010 11:00 pm by Dami

» My first aa script
Tue Mar 02, 2010 7:00 am by wafflemaster

» the real story behind Maverin
Tue Mar 02, 2010 1:33 am by SoundOfDeat

» Random spam
Mon Mar 01, 2010 11:52 pm by Danny1994

» [Request] EMS v56 Exe
Mon Mar 01, 2010 12:39 am by Dami

» [Blacklist] NX-Trading Blacklist.
Mon Mar 01, 2010 12:38 am by Danny1994

» I have a question regarding the meso farmer
Sat Feb 27, 2010 10:30 pm by Dami

» What are you listening to now?
Sat Feb 27, 2010 7:57 pm by Noname111111

» Video(s) Of The Day
Sat Feb 27, 2010 7:37 pm by Noname111111

Navigation
 Portal
 Index
 Memberlist
 Profile
 FAQ
 Search
Affiliates
free forum
 



[C#] Sending keys with the SendInput API

Go down

[C#] Sending keys with the SendInput API

Post by wafflemaster on Tue Dec 22, 2009 7:06 am

The SendInput API is useful for synthesizing and sending input.
Through these inputs are global, they are send at the driver level; no other
input should interfere with them. Using this, you can do stuff you normally can’t
do with PostMessage, such as holding down certain keys.

-------------------------------------------------------------------------------------------------------

Below this is a premade class for SendInput. Just add it
into your project and change the namespace name. You can use the different
functions using “SIClass.whatever(parameters)”. Below is an example of a click,
a Z-keypress, and 2 different functions.

-------------------------------------------------------------------------------------------------------

First, you have to import the SendInput API, since it is not part of the .NET Framework.
It is defined in “WinUser.h”, which is a header file located in the folder “C:\Program
Files\Microsoft SDKs\Windows\v7.0A\Include”. If you don’t feel like converting
from standard C data types to C# data types, then just copy and paste it from
pinvoke.net.

Once you do that, you realize that the API asks for a data type that has not yet been
defined: INPUT[]. This just means INPUT array, so if you
define what INPUT means, then you define what the former means. The
structure should be defined in the file “WinUser.h”. However, the INPUT
structure references more structures, so just define them all (they should be HARDWAREINPUT,
KEYBDINPUT,
and MOUSEINPUT).

After that, you must enumerate the different variables that must be used. In C , you
can just use the variable names, since they are already defined in the header
file, but in C#, you have to define them yourself. Just copy them directly from
“WinUser.h”. While declaring KEYEVENTF, you might run into a
barrier: KEYEVENTF.KEYDOWN is never defined! Just guess on this one;
it is zero. Once you have that, you’ve done almost all of the hard work!

Now, all that is left is to put all of this shit in a method. Make a method that is
either a uint or a void. Inside it, declare a new INPUT
variable. After that, define its components: its types, flags, etc. You should look
at “WinUser.h” while doing this to make sure you do it right. After that, add
that INPUT to a INPUT[]. Then, use the SendInput API
to send that INPUT. The first parameter should be the number of INPUT
structures in your INPUT[]. The next parameter is the actual INPUT[]. The
last parameter is the size of the input; this is not the length. Use Marshal.SizeOf, with
your INPUT variable as the parameter, to get this value.

-------------------------------------------------------------------------------------------------------

Your code should look similar to this when you’re done:


Code:
using System;
using System.Runtime.InteropServices;

namespace NAMESPACE
{
    internal class SIClass
    {
        #region Imports

        [DllImport("user32.dll", SetLastError = true)]
        public static extern IntPtr GetMessageExtraInfo();

        [DllImport("user32.dll", SetLastError = true)]
        public static extern uint SendInput(uint nInputs, INPUT[] pInputs, int cbSize);

        [DllImport("user32.dll")]
        public static extern short VkKeyScan(char ch);

        [DllImport("kernel32.dll", SetLastError = true)]
        public static extern IntPtr WaitForSingleObject(IntPtr hHandle, uint dwMilliseconds);

        #endregion

        public static uint Click()
        {
            INPUT structure = new INPUT();
            structure.mi.dx = 0;
            structure.mi.dy = 0;
            structure.mi.mouseData = 0;
            structure.mi.dwFlags = 2;
            INPUT input2 = structure;
            input2.mi.dwFlags = 4;
            INPUT[] pInputs = new INPUT[] { structure, input2 };
            return SendInput(2, pInputs, Marshal.SizeOf(structure));
        }

        public static uint Z()
        {
            INPUT structure = new INPUT();
            structure.type = (int)InputType.INPUT_KEYBOARD;
            structure.ki.wVk = VkKeyScan((char)System.Windows.Forms.Keys.Z);
            structure.ki.dwFlags = (int)KEYEVENTF.KEYDOWN;
            structure.ki.dwExtraInfo = GetMessageExtraInfo();

            INPUT input2 = new INPUT();
            structure.type = (int)InputType.INPUT_KEYBOARD;
            structure.ki.wVk = VkKeyScan((char)System.Windows.Forms.Keys.Z);
            input2.mi.dwFlags = (int)KEYEVENTF.KEYUP;
            input2.ki.dwExtraInfo = GetMessageExtraInfo();

            INPUT[] pInputs = new INPUT[] { structure, input2 };

            return SendInput(2, pInputs, Marshal.SizeOf(structure));
        }

        public static void SendKeyAsInput(System.Windows.Forms.Keys key)
        {
            INPUT structure = new INPUT();
            structure.type = (int)InputType.INPUT_KEYBOARD;
            structure.ki.wVk = (short)key;
            structure.ki.dwFlags = (int)KEYEVENTF.KEYDOWN;
            structure.ki.dwExtraInfo = GetMessageExtraInfo();

            INPUT input2 = new INPUT();
            input2.type = (int)InputType.INPUT_KEYBOARD;
            input2.ki.wVk = (short)key;
            input2.mi.dwFlags = (int)KEYEVENTF.KEYUP;
            input2.ki.dwExtraInfo = GetMessageExtraInfo();

            INPUT[] pInputs = new INPUT[] { structure, input2 };

            SendInput(2, pInputs, Marshal.SizeOf(structure));
        }

        public static void SendKeyAsInput(System.Windows.Forms.Keys key, int HoldTime)
        {
            INPUT INPUT1 = new INPUT();
            INPUT1.type = (int)InputType.INPUT_KEYBOARD;
            INPUT1.ki.wVk = (short)key;
            INPUT1.ki.dwFlags = (int)KEYEVENTF.KEYDOWN;
            INPUT1.ki.dwExtraInfo = GetMessageExtraInfo();
            SendInput(1, new INPUT[] { INPUT1 }, Marshal.SizeOf(INPUT1));

            WaitForSingleObject((IntPtr)0xACEFDB, (uint)HoldTime);

            INPUT INPUT2 = new INPUT();
            INPUT2.type = (int)InputType.INPUT_KEYBOARD;
            INPUT2.ki.wVk = (short)key;
            INPUT2.mi.dwFlags = (int)KEYEVENTF.KEYUP;
            INPUT2.ki.dwExtraInfo = GetMessageExtraInfo();
            SendInput(1, new INPUT[] { INPUT2 }, Marshal.SizeOf(INPUT2));
           
        }

        [StructLayout(LayoutKind.Explicit)]
        public struct INPUT
        {
            [FieldOffset(4)]
            public HARDWAREINPUT hi;
            [FieldOffset(4)]
            public KEYBDINPUT ki;
            [FieldOffset(4)]
            public MOUSEINPUT mi;
            [FieldOffset(0)]
            public int type;
        }

        [StructLayout(LayoutKind.Sequential)]
        public struct MOUSEINPUT
        {
            public int dx;
            public int dy;
            public int mouseData;
            public int dwFlags;
            public int time;
            public IntPtr dwExtraInfo;
        }

        [StructLayout(LayoutKind.Sequential)]
        public struct KEYBDINPUT
        {
            public short wVk;
            public short wScan;
            public int dwFlags;
            public int time;
            public IntPtr dwExtraInfo;
        }

        [StructLayout(LayoutKind.Sequential)]
        public struct HARDWAREINPUT
        {
            public int uMsg;
            public short wParamL;
            public short wParamH;
        }

        [Flags]
        public enum InputType
        {
            INPUT_MOUSE = 0,
            INPUT_KEYBOARD = 1,
            INPUT_HARDWARE = 2
        }

        [Flags]
        public enum MOUSEEVENTF
        {
            MOVE = 0x0001, /* mouse move */
            LEFTDOWN = 0x0002, /* left button down */
            LEFTUP = 0x0004, /* left button up */
            RIGHTDOWN = 0x0008, /* right button down */
            RIGHTUP = 0x0010, /* right button up */
            MIDDLEDOWN = 0x0020, /* middle button down */
            MIDDLEUP = 0x0040, /* middle button up */
            XDOWN = 0x0080, /* x button down */
            XUP = 0x0100, /* x button down */
            WHEEL = 0x0800, /* wheel button rolled */
            MOVE_NOCOALESCE = 0x2000, /* do not coalesce mouse moves */
            VIRTUALDESK = 0x4000, /* map to entire virtual desktop */
            ABSOLUTE = 0x8000 /* absolute move */
        }

        [Flags]
        public enum KEYEVENTF
        {
            KEYDOWN = 0,
            EXTENDEDKEY = 0x0001,
            KEYUP = 0x0002,
            UNICODE = 0x0004,
            SCANCODE = 0x0008,
        }
    }
}

Mirrored at:
http://wafflemaster.pastebin.com/f28aab5cb

-------------------------------------------------------------------------------------------------------

Credits: I wrote this tutorial. The people at pinvoke.net created the imports. Some korean person made the Click function.

BTW, SendInput is essential to creating bots that can move your character. My smartbot uses this to move to + attack mobs. Too bad it only works for flat maps. Sad
I need to work on adding logic X-X

wafflemaster
V.I.P
V.I.P

Posts : 123
Join date : 2009-10-27

Back to top Go down

Re: [C#] Sending keys with the SendInput API

Post by Dami on Tue Dec 22, 2009 5:26 pm

wafflemaster i appreciate your effort on posting tutorials and such on dafo, even if you would post them on other sites as well.
avatar
Dami
Dragon
Dragon

Posts : 1414
Join date : 2009-08-10
Age : 29
Location : Finland

Back to top Go down

Re: [C#] Sending keys with the SendInput API

Post by wafflemaster on Wed Dec 23, 2009 4:52 am

Dami wrote:wafflemaster i appreciate your effort on posting tutorials and such on dafo, even if you would post them on other sites as well.

Well, most of these tutorials are made primarily for knowledge; it would be counter-intuitive not to post them. But I'll keep the little snippets here Smile.

wafflemaster
V.I.P
V.I.P

Posts : 123
Join date : 2009-10-27

Back to top Go down

Re: [C#] Sending keys with the SendInput API

Post by Dami on Wed Dec 23, 2009 1:44 pm

wafflemaster wrote:
Dami wrote:wafflemaster i appreciate your effort on posting tutorials and such on dafo, even if you would post them on other sites as well.

Well, most of these tutorials are made primarily for knowledge; it would be counter-intuitive not to post them. But I'll keep the little snippets here Smile.

Exactly, its a shame we wont have more people with some knowledge for this forum subjects, or will to post relevant stuff.
avatar
Dami
Dragon
Dragon

Posts : 1414
Join date : 2009-08-10
Age : 29
Location : Finland

Back to top Go down

Re: [C#] Sending keys with the SendInput API

Post by Sponsored content


Sponsored content


Back to top Go down

Back to top

- Similar topics

 
Permissions in this forum:
You cannot reply to topics in this forum