473,573 Members | 2,829 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Help with SendInput

Hi All,

I am trying to use the SendInput API call in C# to put a keypress in the
keyboard buffer and have beeting my head against the wall all morning.
Could someone help me out with this, I keep getting a WIN32 errorcode of 6.

I am simply trying to simulate the TAB key being pressed when the user
presses the ENTER key in a custom control.
thanks
*************** *************** ****

using System;
using System.Drawing;
using System.Windows. Forms;
using System.Runtime. InteropServices ;
namespace KeybTest
{

/// <summary>
/// Description of MainForm.
/// </summary>
public class MainForm : System.Windows. Forms.Form
{
private System.Windows. Forms.Button button1;
private System.Windows. Forms.TextBox textBox1;

private const int INPUT_MOUSE = 0;
private const int INPUT_KEYBOARD = 1;
private const int INPUT_HARDWARE = 2;
private const int VK_TAB = 0x09;

[DllImport("user 32.dll")]
static extern uint SendInput(uint nInputs, ref INPUT pInputs,
int cbSize);
public MainForm()
{
//
// The InitializeCompo nent() call is required for Windows Forms
designer support.
//
InitializeCompo nent();

//
// TODO: Add constructor code after the InitializeCompo nent() call.
//
}

[STAThread]
public static void Main(string[] args)
{
Application.Run (new MainForm());
}

#region Windows Forms Designer generated code
/// <summary>
/// This method is required for Windows Forms designer support.
/// Do not change the method contents inside the source code editor.
The Forms designer might
/// not be able to load this method if it was changed manually.
/// </summary>
private void InitializeCompo nent() {
this.textBox1 = new System.Windows. Forms.TextBox() ;
this.button1 = new System.Windows. Forms.Button();
this.SuspendLay out();
//
// textBox1
//
this.textBox1.L ocation = new System.Drawing. Point(32, 24);
this.textBox1.N ame = "textBox1";
this.textBox1.S ize = new System.Drawing. Size(72, 20);
this.textBox1.T abIndex = 0;
this.textBox1.T ext = "textBox1";
//
// button1
//
this.button1.Lo cation = new System.Drawing. Point(120, 104);
this.button1.Na me = "button1";
this.button1.Si ze = new System.Drawing. Size(120, 72);
this.button1.Ta bIndex = 1;
this.button1.Te xt = "button1";
this.button1.Cl ick += new
System.EventHan dler(this.Butto n1Click);
//
// MainForm
//
this.AutoScaleB aseSize = new System.Drawing. Size(5, 13);
this.ClientSize = new System.Drawing. Size(292, 266);
this.Controls.A dd(this.button1 );
this.Controls.A dd(this.textBox 1);
this.Name = "MainForm";
this.Text = "MainForm";
this.ResumeLayo ut(false);
}
#endregion
void Button1Click(ob ject sender, System.EventArg s e)
{
KEYBDINPUT keyevent = new KEYBDINPUT();
INPUT inStruc = new INPUT();

inStruc.type = INPUT_KEYBOARD;
keyevent.dwFlag s = 0; // press the key down
keyevent.dwExtr aInfo = IntPtr.Zero ;
keyevent.wScan = 0; // not needed
keyevent.time = 0; // use the default
keyevent.wVk = VK_TAB;
inStruc.ki = keyevent;

Console.WriteLi ne(SendInput(1, ref inStruc,
Marshal.SizeOf( inStruc)));

Console.WriteLi ne(Marshal.GetL astWin32Error() );
}

}
[StructLayout(La youtKind.Sequen tial)]
struct MOUSEINPUT
{
public int dx;
public int dy;
public int mouseData;
public int dwFlags;
public int time;
public IntPtr dwExtraInfo;
}

[StructLayout(La youtKind.Sequen tial)]
struct KEYBDINPUT
{
public short wVk;
public short wScan;
public int dwFlags;
public int time;
public IntPtr dwExtraInfo;
}

[StructLayout(La youtKind.Sequen tial)]
struct HARDWAREINPUT
{
int uMsg;
short wParamL;
short wParamH;
}

//[StructLayout(La youtKind.Explic it)]
//struct INPUT
//{
// [FieldOffset(0)] int type;
// [FieldOffset(4)] MOUSEINPUT mi;
// [FieldOffset(4)] KEYBDINPUT ki;
// [FieldOffset(4)] HARDWAREINPUT hi;
//}

[StructLayout(La youtKind.Explic it)]
struct INPUT
{
[FieldOffset(0)] public int type;
[FieldOffset(4)] public KEYBDINPUT ki;
}

}
Nov 17 '05 #1
2 6414
How about instead, catch the ENTER on KEY_UP or whatever and then do
something like this:

this.Parent.Sel ectNextControl( this, true, true, true,true);

Nov 17 '05 #2
wa********@yaho o.com wrote:
How about instead, catch the ENTER on KEY_UP or whatever and then do
something like this:

this.Parent.Sel ectNextControl( this, true, true, true,true);


That is what I am currently doing but I have a grid on a tabpage that is
marked Tabstop = false and it still gets the focus when using the Enter
key. I figured the easiest thing to do would be to simulate the Tabkey
press since it works correctly.
Nov 17 '05 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
1582
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
15634
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 suggested simply queueing two click events, but this is not working for me, regardless of the delay I put between clicks. So give this as a click (where...
0
3710
by: fisj | last post by:
Has anyone managed to get the sendinput api to work with vb.net? I've tried converting the vb6 code from allapi.net, but its proving extremely difficult. Example and def is here: http://www.mentalis.org/apilist/SendInput.shtml Infact, I'm pretty sure this is a copymemory api problem, I'm certainly getting the same object reference error...
7
5727
by: aam | last post by:
I'm trying to find a code sample on how to use the SendInput Function to simulate a mouse click on a button of another form. I've looked all over and can't find one. Thanks.
0
1181
by: Trammel | last post by:
Hi. Im trying to make a vb.net aplication click location 200,350 on an window who's name is "Blah" (Not owned/started by my application). This has to be done using SendInput as Im coding it for Win2k. Could anyone help by creating a small function like: public function ClickWindow(byVal hwnd as inptr, byVal posX as integer, byVal posY...
2
2135
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 needed by the SendInput. But the mouse clicks are always reported at the "current cursor location" (I am testing it with the notepad) irrespective...
1
2317
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 needed by the SendInput. But the mouse clicks are always reported at the "current cursor location" (I am testing it with the notepad) irrespective...
1
5490
by: ^Def_JaM^ | last post by:
I need a working sample of how to use the "SendInput" (as described in the WinAPI) method to emulate Unicode keystrokes. Most important is the fact that SendInput sample must be able to send Unicode strings even when the according keyboard layout isn't loaded or not present. For example, with the old SendKeys method I used to write "SendKeys...
2
9476
by: =?Utf-8?B?TXJOb2JvZHk=?= | last post by:
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...
0
7781
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main...
0
8027
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. ...
0
8066
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the...
1
5594
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes...
0
5292
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert...
0
3733
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2216
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
1
1304
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
1041
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating...

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.