Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsFree MagazinesWhite PapersSubmit Content
Discussion GroupsASP.NETWindows FormsLanguages.NET FrameworkVisual Studio.NET
Articles.NET FrameworkASP.NETToolsWindows Forms
.NET DirectoryOpen Source ProjectsUser GroupsWeb Resources
Related Topics
Visual Basic 6SQL ServerMS AccessOther DB ProductsMS Server ProductsMore Topics ...

.NET Forum / .NET Framework / Compact Framework / November 2007

Tip: Looking for answers? Try searching our database.

keybd_event wont fire KeyDown event  for  VK_Return keycode on textbox?

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
ink - 29 Nov 2007 12:54 GMT
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
           VK_LEFT = 0x25, // LEFT ARROW key
           VK_UP = 0x26, //UP ARROW key
           VK_RIGHT = 0x27, //RIGHT ARROW key
           VK_DOWN = 0x28, //DOWN ARROW key
           VK_SELECT = 0x29, // SELECT key
           VK_EXECUTE = 0x2B, //EXECUTE key
           VK_SNAPSHOT = 0x2C, //PRINT SCREEN key
           VK_INSERT = 0x2D, // INS key
           VK_DELETE = 0x2E, //DEL key
           VK_HELP = 0x2F, // HELP key
           VK_LWIN = 0x5B, //Left Windows key on a Microsoft Natural
Keyboard
           VK_RWIN = 0x5C, // Right Windows key on a Microsoft Natural
Keyboard
           VK_APPS = 0x5D, // Applications key on a Microsoft Natural
Keyboard
           VK_NUMPAD0 = 0x60, // Numeric keypad 0 key
           VK_NUMPAD1 = 0x61, // Numeric keypad 1 key
           VK_NUMPAD2 = 0x62, // Numeric keypad 2 key
           VK_NUMPAD3 = 0x63, // Numeric keypad 3 key
           VK_NUMPAD4 = 0x64, // Numeric keypad 4 key
           VK_NUMPAD5 = 0x65, // Numeric keypad 5 key
           VK_NUMPAD6 = 0x66, // Numeric keypad 6 key
           VK_NUMPAD7 = 0x67, //  Numeric keypad 7 key
           VK_NUMPAD8 = 0x68, // Numeric keypad 8 key
           VK_NUMPAD9 = 0x69, // Numeric keypad 9 key
           VK_MULTIPLY = 0x6A, // Multiply key
           VK_ADD = 0x6B, // Add key
           VK_SEPARATOR = 0x6C, // Separator key
           VK_SUBTRACT = 0x6D, // Subtract key
           VK_DECIMAL = 0x6E, // Decimal key
           VK_DIVIDE = 0x6F, //Divide key
           VK_F1 = 0x70, // F1 key
           VK_F2 = 0x71, // F2 key
           VK_F3 = 0x72, // F3 key
           VK_F4 = 0x73, // F4 key
           VK_F5 = 0x74, // F5 key
           VK_F6 = 0x75, // F6 key
           VK_F7 = 0x76, // F7 key
           VK_F8 = 0x77, //  F8 key
           VK_F9 = 0x78, //  F9 key
           VK_F10 = 0x79, //  F10 key
           VK_F11 = 0x7A, // F11 key
           VK_F12 = 0x7B, // F12 key
           VK_F13 = 0x7C, //  F13 key
           VK_F14 = 0x7D, // F14 key
           VK_F15 = 0x7E, //  F15 key
           VK_F16 = 0x7F, // F16 key
           VK_F17 = 0x80, //  F17 key
           VK_F18 = 0x81, //  F18 key
           VK_F19 = 0x82, // F19 key
           VK_F20 = 0x83, //  F20 key
           VK_F21 = 0x84, // F21 key
           VK_F22 = 0x85, // F22 key
           VK_F23 = 0x86, // F23 key
           VK_F24 = 0x87, // F24 key
           VK_NUMLOCK = 0x90, //  NUM LOCK key
           VK_SCROLL = 0x91, //  SCROLL LOCK key
           VK_LSHIFT = 0xA0, // Left SHIFT
           VK_RSHIFT = 0xA1, // Right SHIFT
           VK_LCONTROL = 0xA2, // Left CTRL
           VK_RCONTROL = 0xA3, // Right CTRL
           VK_LMENU = 0xA4, // Left ALT
           VK_RMENU = 0xA5, //Right ALT
           VK_ATTN = 0xF6, //  ATTN key
           VK_CRSEL = 0xF7, //  CRSEL key
           VK_EXSEL = 0xF8, //  EXSEL key
           VK_EREOF = 0xF9, //  Erase EOF key
           VK_PLAY = 0xFA, // PLAY key
           VK_ZOOM = 0xFB, //  ZOOM key
           VK_NONAME = 0xFC, // Reserved for future use
           VK_PA1 = 0xFD, // PA1 key
           VK_OEM_CLEAR = 0xFE, //CLEAR key
           VK_KEYLOCK = 0xF22  //Key used to lock device

       }

   }
}

//######## END Of Code  #####################
dbgrick - 29 Nov 2007 14:52 GMT
Try calling using these parameters

keybd_event(KeyCode, 0, KEYEVENTF_EXTENDEDKEY, 0);
keybd_event(KeyCode, 0, KEYEVENTF_KEYUP, 0);

Regards,
Rick D.
Contractor

> Hi all,
>
[quoted text clipped - 280 lines]
>
>         }
ink - 29 Nov 2007 14:58 GMT
Hi all,

I have come up with and explanation / solution to this problem. As I am
rather new to this sort of thing I have had to make some guesses as to how
things are working under the covers.

What seems to be happening when the Hard keyboard Enter key is hit it
outputs 2 KeyUp events, but looking closer they are not the same.
KeyDown: KeyValue = 134
KeyPress:
KeyUp: KeyValue = 13
KeyUp:  KeyValue = 134

So my guess is that Symbol has had this problem with the Software keyboard
not firing the Key Down event and what they have done as a work around is
send the Keyboard down event for the 134 then the keyboard up event for the
13 and then the up event for 134.

So in order to mimic this behaviour I have change my code as follows:

       public void PressKey(VirtualKeyCodes keyCode)
       {
           const int KEYEVENTF_EXTENDEDKEY = 0x1;
           const int KEYEVENTF_KEYUP = 0x2;
           const int KEYEVENTF_KEYDOWN = 0x0;

           if (keyCode == VirtualKeyCodes.VK_RETURN)
           {
               keybd_event(134, 0x45, KEYEVENTF_KEYDOWN |
KEYEVENTF_EXTENDEDKEY, 0);
               keybd_event(13, 0x45, KEYEVENTF_KEYDOWN |
KEYEVENTF_EXTENDEDKEY, 0);
               keybd_event(13, 0x45, KEYEVENTF_EXTENDEDKEY |
KEYEVENTF_KEYUP, 0);
               keybd_event(134, 0x45, KEYEVENTF_EXTENDEDKEY |
KEYEVENTF_KEYUP, 0);
           }
           else
           {
               byte KeyCode = (byte)keyCode;

               keybd_event(KeyCode, 0x45, KEYEVENTF_KEYDOWN |
KEYEVENTF_EXTENDEDKEY, 0);
               keybd_event(KeyCode, 0x45, KEYEVENTF_EXTENDEDKEY |
KEYEVENTF_KEYUP, 0);
           }

       }

This does not fix the SIP but it is a solution to my original problem.

If anyone can further shed some light on this, I would be very interested to
know why just sending a VK_Return buy itself does not do the job.

Thanks,
ink

> Hi all,
>
[quoted text clipped - 285 lines]
>
> //######## END Of Code  #####################

Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.