473,387 Members | 1,621 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

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

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
2 9452
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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: N.K. | last post by:
Hello, I'm trying to find a way to flexibly filter a column in a row of data read from a file. I will have data type of that column, the operation to be performed on it (like >, <, <= etc.) and the...
3
by: David Harris | last post by:
Is there a sendinput function in Visual Studio .net beta 2? I don't see the namespace that you must include for the sendinput function listed on msdn2.microsoft.com. David Harris
6
by: Richard A. Lowe | last post by:
I'm using P/Invoke to call SendInput (using code culled from these newsgroups!) to send mouse events to a window. But I'm unsure how to send double-clicks. A VB6 article I saw on SendInput...
0
by: Logan McKinley | last post by:
need my C# app to send a character to a different application. I believe I need to use the SendInput API call and to pass a KEYBDINPUT struct into that but I am lost past that point. Here is the...
11
by: ricolee99 | last post by:
Hi everyone, I'm trying to invoke my .exe application from a remote server. Here is the code: ManagementClass processClass = new ManagementClass ("\\\\" +"RemoteServerName" +...
1
by: kumar_subrahmanya | last post by:
Hi, I am facing problems in sending mouse clicks via SendInput API. Mouse clicks are being sent but at the X,Y co-ordinates. I am mapping my monitor to the (0,0,65535,65535) virtual monitor as...
3
by: John Dalberg | last post by:
I have an app that keeps popping up a windows with a 'Yes' or 'OK' button on it. I am trying to write a little app that automates hitting the enter key so I don't have to do it myself. I used...
18
by: Zytan | last post by:
I want the same function to be run whether you press Enter or double click the listbox. It seems really verbose to write both handlers to both events everytime, even if they both call the same...
23
by: tonytech08 | last post by:
What I like about the C++ object model: that the data portion of the class IS the object (dereferencing an object gets you the data of a POD object). What I don't like about the C++ object...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.