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

G

Guest

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!
 
G

Guest

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?
 
G

Guest

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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top