473,385 Members | 1,863 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,385 software developers and data experts.

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("user32.dll")]
static extern uint SendInput(uint nInputs, ref INPUT pInputs,
int cbSize);
public MainForm()
{
//
// The InitializeComponent() call is required for Windows Forms
designer support.
//
InitializeComponent();

//
// TODO: Add constructor code after the InitializeComponent() 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 InitializeComponent() {
this.textBox1 = new System.Windows.Forms.TextBox();
this.button1 = new System.Windows.Forms.Button();
this.SuspendLayout();
//
// textBox1
//
this.textBox1.Location = new System.Drawing.Point(32, 24);
this.textBox1.Name = "textBox1";
this.textBox1.Size = new System.Drawing.Size(72, 20);
this.textBox1.TabIndex = 0;
this.textBox1.Text = "textBox1";
//
// button1
//
this.button1.Location = new System.Drawing.Point(120, 104);
this.button1.Name = "button1";
this.button1.Size = new System.Drawing.Size(120, 72);
this.button1.TabIndex = 1;
this.button1.Text = "button1";
this.button1.Click += new
System.EventHandler(this.Button1Click);
//
// MainForm
//
this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);
this.ClientSize = new System.Drawing.Size(292, 266);
this.Controls.Add(this.button1);
this.Controls.Add(this.textBox1);
this.Name = "MainForm";
this.Text = "MainForm";
this.ResumeLayout(false);
}
#endregion
void Button1Click(object sender, System.EventArgs e)
{
KEYBDINPUT keyevent = new KEYBDINPUT();
INPUT inStruc = new INPUT();

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

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

Console.WriteLine(Marshal.GetLastWin32Error());
}

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

[StructLayout(LayoutKind.Sequential)]
struct KEYBDINPUT
{
public short wVk;
public short wScan;
public int dwFlags;
public int time;
public IntPtr dwExtraInfo;
}

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

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

[StructLayout(LayoutKind.Explicit)]
struct INPUT
{
[FieldOffset(0)] public int type;
[FieldOffset(4)] public KEYBDINPUT ki;
}

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

this.Parent.SelectNextControl(this, true, true, true,true);

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

this.Parent.SelectNextControl(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
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: 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: ...
7
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
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...
2
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...
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...
1
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...
2
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...
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:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
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...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
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
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.