Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsFree MagazinesWhite PapersSubmit Content
Discussion GroupsASP.NETWindows FormsLanguages.NET FrameworkVisual Studio.NET
Articles.NET FrameworkASP.NETToolsWindows Forms
.NET DirectoryOpen Source ProjectsUser GroupsWeb Resources
Related Topics
Visual Basic 6SQL ServerMS AccessOther DB ProductsMS Server ProductsMore Topics ...

.NET Forum / Languages / C# / October 2007

Tip: Looking for answers? Try searching our database.

Anyone have a good example on using P/Invoke SendInput ?

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
MrNobody - 30 Oct 2007 16:26 GMT
I am trying to find a good example of SendInput. Doing a search on google I
found two, one is incomplete and vague from the start and the other I copied
the code and tried running it and it was first of all incomplete (compiler
errors) which I had to resolve by guessing on some things like how some
variables were instantiated, and then finally it doesn't work. Ok, maybe I
guessed wrong, but I shouldn't be guessing on what the code should have been
doing if they didn't post everything. This was an example on pinvoke.net. Not
a good example!
MrNobody - 30 Oct 2007 18:47 GMT
For example, I found a sample online which works for Keyboard inputs. It is
here: http://split-s.blogspot.com/2005/01/emulating-keystrokes-in-windows.html

But I need to do both keyboard/mouse inputs. I found other samples (which
are either incomplete or dont work when tried directly) which do both
keyboard and mouse but they do something with making a kind of generic struct
called "INPUT" which has properties for both keyboard and mouse, so you
apparently set the values for the input type you want and then send that
INPUT struct to SendInput.

Here's what I copied/adapted:

[StructLayout(LayoutKind.Sequential)]
    internal struct KEYBOARDINPUT
    {
        public uint type;
        public ushort vk;
        public ushort scanCode;
        public uint flags;
        public uint time;
        public uint extrainfo;
        public uint padding1;
        public uint padding2;
    }

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

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

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

[DllImport("User32.dll")]
        private static extern uint SendInput(uint numberOfInputs, ref INPUT input,
int structSize);

private void sendKey(int scanCode, bool press)
        {           

            INPUT input = new INPUT();

            input.type = Win32Consts.INPUT_KEYBOARD;
            input.ki = new KEYBOARDINPUT();
            input.ki.flags = KEY_SCANCODE;

            if ((scanCode & 0xFF00) == 0xE000)
            { // extended key?
                input.ki.flags |= KEY_EXTENDED;
            }

            if (press)
            { // press?
                input.ki.scanCode = (ushort) (scanCode & 0xFF);
            }
            else
            { // release?
                input.ki.scanCode = (ushort)scanCode;
                input.ki.flags |= KEY_UP;
            }

            uint result = SendInput(1, ref input, Marshal.SizeOf(input));

            if (result != 1)
            {
                throw new Exception("Could not send key: " + scanCode);
            }
        }

When I changed the original example from the one in the link I posted above
to this code I pasted where it uses the INPUT struct, it keeps failing. I
really don't understand why... Any ideas?
MrNobody - 30 Oct 2007 19:07 GMT
OK i got it, I mixed up a struct for first example with structs for others
without understanding how it all played into the functionality. turns out the
keyboard struct i was using had fields to encompass whole request so it was
causing a problem when i combined it with the others. had to change the
struct to one with only keyboard specific fields

Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.