Hi all,
I am working with C# CF2 on windows mobile 5 phone edition on a Symbol MC70
device.
For some reason the KeyDown Event doesn't fire when you use the keybd_event
to send the VK_Return= 0x0D.
If I use the hard keyboard Return I get.
KeyDown
KeyPress
KeyUp
KeyUp
If I use the soft keyboard Return or keybd_event and VK_Return I get.
KeyPress
KeyUp
If I use any of the three above methods (Soft keyboard, Hard keyboard or
keybd_event) to input Numbers, letters or even Delete then I get.
KeyDown
KeyPress
KeyUp
Which is what I would have expected from all of these scenarios.
I have tried setting the Textbox allow returns property and it had no
effect.
I have also tried setting the form key preview property to see if that would
make any difference.
This is the only post I have been able to find that even resembles my
problem, it is for the desktyop and it is the richtextcontrol, but I don't
really understand what the resolution to the problem was.
http://www.thescripts.com/forum/thread249414.html I really need to be able to send the VK_Return using keybd_event and have it
fire the Key Down event.
Any idea on how this can be done?
I have already tried moving my code that is running in the KeyDown to the
KeyUp but this has not been an acceptable solution to the problem as it
introduces to many bugs.
I have attached the code I have used for testing to the bottom of this post
I create all the controls dynamically so all that you need to do to test is
start a new single form project and past all this code over the top of your
form class code. The way to test is uncomment lines from the
textBox1_GotFocus event.
Thanks in advanced.
ink
//######## START Of Code #####################
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Runtime.InteropServices;
namespace WMTestingApp
{
public partial class Form1 : Form
{
private System.Windows.Forms.TextBox textBox1;
private System.Windows.Forms.ListBox listBox1;
private System.Windows.Forms.Button button1;
[DllImport("coredll.dll", EntryPoint = "keybd_event", SetLastError =
true)]
internal static extern void keybd_event(byte bVk, byte bScan, int
dwFlags, int dwExtraInfo);
public Form1()
{
InitializeComponent();
this.textBox1 = new System.Windows.Forms.TextBox();
this.listBox1 = new System.Windows.Forms.ListBox();
this.button1 = new System.Windows.Forms.Button();
//
// textBox1
//
this.textBox1.AcceptsReturn = true;
this.textBox1.Location = new System.Drawing.Point(20, 32);
this.textBox1.Name = "textBox1";
this.textBox1.Size = new System.Drawing.Size(192, 21);
this.textBox1.TabIndex = 0;
this.textBox1.Text = "textBox1";
this.textBox1.GotFocus += new
System.EventHandler(this.textBox1_GotFocus);
this.textBox1.KeyUp += new
System.Windows.Forms.KeyEventHandler(this.textBox1_KeyUp);
this.textBox1.KeyPress += new
System.Windows.Forms.KeyPressEventHandler(this.textBox1_KeyPress);
this.textBox1.KeyDown += new
System.Windows.Forms.KeyEventHandler(this.textBox1_KeyDown);
//
// listBox1
//
this.listBox1.Location = new System.Drawing.Point(20, 73);
this.listBox1.Name = "listBox1";
this.listBox1.Size = new System.Drawing.Size(192, 72);
this.listBox1.TabIndex = 1;
//
// button1
//
this.button1.Location = new System.Drawing.Point(141, 151);
this.button1.Name = "button1";
this.button1.Size = new System.Drawing.Size(71, 23);
this.button1.TabIndex = 2;
this.button1.Text = "clear";
this.button1.Click += new
System.EventHandler(this.button1_Click);
//
// Form1
//
this.Controls.Add(this.button1);
this.Controls.Add(this.listBox1);
this.Controls.Add(this.textBox1);
}
private void textBox1_KeyDown(object sender, KeyEventArgs e)
{
listBox1.Items.Add("textBox1_KeyDown");
}
private void textBox1_KeyPress(object sender, KeyPressEventArgs e)
{
listBox1.Items.Add("textBox1_KeyPress");
}
private void textBox1_KeyUp(object sender, KeyEventArgs e)
{
listBox1.Items.Add("textBox1_KeyUp");
}
private void button1_Click(object sender, EventArgs e)
{
listBox1.Items.Clear();
}
private void textBox1_GotFocus(object sender, EventArgs e)
{
//PressKey(VirtualKeyCodes.VK_HOME);
//PressKey(VirtualKeyCodes.VK_DELETE);
//PressKey(VirtualKeyCodes.VK_END);
//PressKey(VirtualKeyCodes.VK_SPACE);
//PressKey(VirtualKeyCodes.VK_NUMPAD1);
//PressKey(VirtualKeyCodes.VK_SPACE);
//PressKey(VirtualKeyCodes.VK_NUMPAD2);
//PressKey(VirtualKeyCodes.VK_SPACE);
//PressKey(VirtualKeyCodes.VK_NUMPAD3);
//PressKey(VirtualKeyCodes.VK_RETURN);
}
public void PressKey(VirtualKeyCodes keyCode)
{
const int KEYEVENTF_EXTENDEDKEY = 0x1;
const int KEYEVENTF_KEYUP = 0x2;
const int KEYEVENTF_KEYDOWN = 0x0;
byte KeyCode = (byte)keyCode;
keybd_event(KeyCode, 0x45, KEYEVENTF_KEYDOWN |
KEYEVENTF_EXTENDEDKEY, 0);
keybd_event(KeyCode, 0x45, KEYEVENTF_EXTENDEDKEY |
KEYEVENTF_KEYUP, 0);
}
public enum VirtualKeyCodes
{
VK_LBUTTON = 0x01, //Left mouse button
VK_RBUTTON = 0x02, // Right mouse button
VK_CANCEL = 0x03, // Control-break processing
VK_MBUTTON = 0x04, // Middle mouse button on a three-button
mouse
VK_BACK = 0x08, // BACKSPACE key
VK_TAB = 0x09, // TAB key
VK_CLEAR = 0x0C, // CLEAR key
VK_RETURN = 0x0D, // ENTER key
VK_SHIFT = 0x10, // SHIFT key
VK_CONTROL = 0x11, //CTRL key
VK_MENU = 0x12, // ALT key
VK_PAUSE = 0x13, // PAUSE key
VK_CAPITAL = 0x14, // CAPS LOCK key
VK_ESCAPE = 0x1B, //ESC key
VK_SPACE = 0x20, //SPACEBAR
VK_PRIOR = 0x21, //PAGE UP key
VK_NEXT = 0x22, //PAGE DOWN key
VK_END = 0x23, //END key
VK_HOME = 0x24, //HOME key