By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
459,270 Members | 1,301 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 459,270 IT Pros & Developers. It's quick & easy.

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

P: n/a
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!
Oct 30 '07 #1
Share this Question
Share on Google+
2 Replies


P: n/a
For example, I found a sample online which works for Keyboard inputs. It is
here: http://split-s.blogspot.com/2005/01/...n-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?
Oct 30 '07 #2

P: n/a
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
Oct 30 '07 #3

This discussion thread is closed

Replies have been disabled for this discussion.