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

Help with SendInput

P: n/a
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
Share this Question
Share on Google+
2 Replies


P: n/a
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

P: n/a
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 discussion thread is closed

Replies have been disabled for this discussion.