locked
Button KeyDown and KeyUp not firing. RRS feed

  • Question

  • Hi
    I have created a normal windows mobile smart device project and added some buttons. I can create a normal click event which works fine but when I tried to create a Key Down or Key Up event, they are never triggered. I have no idea whats gone wrong as I'm not getting any errors. The events simply don't work.

    Code Sample:

    Designer Code -

     

    this.cmdForward.Location = new System.Drawing.Point(84, 77);
    this.cmdForward.Name = "cmdForward";
    this.cmdForward.Size = new System.Drawing.Size(72, 20);
    this.cmdForward.TabIndex = 0;
    this.cmdForward.Text = "Forward";
    this.cmdForward.KeyUp += new System.Windows.Forms.KeyEventHandler(this.cmdForward_KeyUp);
    this.cmdForward.KeyDown += new System.Windows.Forms.KeyEventHandler(this.forwardDown);


    CS Code-

    private

     

    void forwardDown(object sender, KeyEventArgs e)
    {
         //Some Code 

    }


    Friday, December 4, 2009 10:43 PM

Answers

  • Key = PDA HARDWARE KEY. Not a button.

    You probably want to have MouseDown and MouseUp events fired on this button when You press it.

    If You'll find my answer satisfactory or helpful - mark it as answered or vote for it! Thank You.
    If You think You know better then me, why is Your code not working, then don't waste my time at this forum. Otherwise - do as I'm suggesting.

    I'm on MSDN just like MD House in the clinic. But I'm also a human which sometimes needs to see another doctor :)
    • Marked as answer by Egone Sunday, December 6, 2009 11:21 PM
    Saturday, December 5, 2009 10:50 AM

All replies

  • These events occur when a key is pressed/released while the control has focus.
    Friday, December 4, 2009 11:22 PM
  • Sorry I'm  bit confused and not used to writing applications for mobile devices. What do I have to do to make the button events work? Which control needs focus? :S

    Thanks.

    Saturday, December 5, 2009 10:11 AM
  • Setting Your form's KeyPreview = true should do the trick. Which key are You trying to chatch? D-pad, Left/Right Soft Key, Enter, Send, End, Back?
    If You'll find my answer satisfactory or helpful - mark it as answered or vote for it! Thank You.
    If You think You know better then me, why is Your code not working, then don't waste my time at this forum. Otherwise - do as I'm suggesting.

    I'm on MSDN just like MD House in the clinic. But I'm also a human which sometimes needs to see another doctor :)
    Saturday, December 5, 2009 10:31 AM
  • I've created a button on the form and I want to get the KeyDown and KeyUp events for it to fire. How do I set the KeyPreview? Do I need to set it per button?

    Some background info:
    The button's on the form will be used to control a Lego NXT robot via bluetooth. So I need the keydown event (tyre's go forward) keyup event (tyres stop). Just like a remote control car. :)

    Saturday, December 5, 2009 10:37 AM
  • Sorry I also should have stated its running on a touch screen. A HTC touch pro to be specific.
    Saturday, December 5, 2009 10:39 AM
  • Key = PDA HARDWARE KEY. Not a button.

    You probably want to have MouseDown and MouseUp events fired on this button when You press it.

    If You'll find my answer satisfactory or helpful - mark it as answered or vote for it! Thank You.
    If You think You know better then me, why is Your code not working, then don't waste my time at this forum. Otherwise - do as I'm suggesting.

    I'm on MSDN just like MD House in the clinic. But I'm also a human which sometimes needs to see another doctor :)
    • Marked as answer by Egone Sunday, December 6, 2009 11:21 PM
    Saturday, December 5, 2009 10:50 AM
  • I see where I was going wrong now. There is no mouse down/up button events for mobile devices. I assumed the keydown/up events would serve the same function.  Im not sure what to do now. It looks like my only option is to try and map the keyboard buttons from my phone. Unless you know of another control in the toolbox I could use to accomplish what I'm trying to do? Now just to learn how to map keyboard buttons from my phone :S

    Thanks for your help. 
    Saturday, December 5, 2009 11:01 AM
  • Mouse Event = touch screen event on PDA.

    If You press button it makes those events (in that sequence): Mouse Down, Click, Mouse Up.
    Just add Mouse Events and check Yourself.

    If You'll find my answer satisfactory or helpful - mark it as answered or vote for it! Thank You.
    If You think You know better then me, why is Your code not working, then don't waste my time at this forum. Otherwise - do as I'm suggesting.

    I'm on MSDN just like MD House in the clinic. But I'm also a human which sometimes needs to see another doctor :)
    Saturday, December 5, 2009 2:07 PM
  • I added the code you suggest but the events still won't fire.

    designer:
    this.cmdForward.MouseDown += new MouseEventHandler(cmdForward_MouseDown);
    this.cmdForward.MouseUp += new MouseEventHandler(cmdForward_MouseUp);

    form code:
    private void cmdForward_MouseDown(object sender, System.Windows.Forms.MouseEventArgs e)
    {
          // Some code
    }

     

    private void cmdForward_MouseUp(object sender, System.Windows.Forms.MouseEventArgs e)
    {
           //Some Code
    }


    I was able to map the arrow button's on my phones QWERTY keyboard and use the KeyDown and KeyUp events on them which works great but it would be nice to have the buttons on the touch screen working.

    Saturday, December 5, 2009 7:17 PM
  • cmdForward is a Button control or something different?
    Show me Your full form's designer code.

    You must do something wrong, because it works every time on every WM touch device.

    If You'll find my answer satisfactory or helpful - mark it as answered or vote for it! Thank You.
    If You think You know better then me, why is Your code not working, then don't waste my time at this forum. Otherwise - do as I'm suggesting.

    I'm on MSDN just like MD House in the clinic. But I'm also a human which sometimes needs to see another doctor :)
    Saturday, December 5, 2009 9:31 PM
  • Heres the designer code. Thanks for taking the time to help me.

    using

     

    System.Windows.Forms;

    namespace

     

    NXTButtonControl

    {

     

    partial class NXTButtonControl

    {

     

    /// <summary>

     

    /// Required designer variable.

     

    /// </summary>

     

    private System.ComponentModel.IContainer components = null;

     

    private System.Windows.Forms.MainMenu mainMenu1;

     

    /// <summary>

     

    /// Clean up any resources being used.

     

    /// </summary>

     

    /// <param name="disposing">true if managed resources should be disposed; otherwise, false.</param>

     

    protected override void Dispose(bool disposing)

    {

     

    if (disposing && (components != null))

    {

    components.Dispose();

    }

     

    base.Dispose(disposing);

    }

    #region

     

    Windows Form Designer generated code

     

    /// <summary>

     

    /// Required method for Designer support - do not modify

     

    /// the contents of this method with the code editor.

     

    /// </summary>

     

    private void InitializeComponent()

    {

     

    this.mainMenu1 = new System.Windows.Forms.MainMenu();

     

    this.cmdForward = new System.Windows.Forms.Button();

     

    this.cmdRight = new System.Windows.Forms.Button();

     

    this.cmdBackward = new System.Windows.Forms.Button();

     

    this.cmdLeft = new System.Windows.Forms.Button();

     

    this.blueSync = new System.Windows.Forms.Button();

     

    this.cmbDevices = new System.Windows.Forms.ComboBox();

     

    this.turnOff = new System.Windows.Forms.Button();

     

    this.SuspendLayout();

     

    //

     

    // cmdForward

     

    //

     

    this.cmdForward.Location = new System.Drawing.Point(152, 74);

     

    this.cmdForward.Name = "cmdForward";

     

    this.cmdForward.Size = new System.Drawing.Size(72, 20);

     

    this.cmdForward.TabIndex = 0;

     

    this.cmdForward.Text = "Forward";

     

    this.cmdForward.MouseDown += new MouseEventHandler(cmdForward_MouseDown);

     

    this.cmdForward.MouseUp += new MouseEventHandler(cmdForward_MouseUp);

     

    //

     

    // cmdRight

     

    //

     

    this.cmdRight.Location = new System.Drawing.Point(84, 117);

     

    this.cmdRight.Name = "cmdRight";

     

    this.cmdRight.Size = new System.Drawing.Size(72, 20);

     

    this.cmdRight.TabIndex = 1;

     

    this.cmdRight.Text = "Right";

     

    //

     

    // cmdBackward

     

    //

     

    this.cmdBackward.Location = new System.Drawing.Point(152, 163);

     

    this.cmdBackward.Name = "cmdBackward";

     

    this.cmdBackward.Size = new System.Drawing.Size(72, 20);

     

    this.cmdBackward.TabIndex = 2;

     

    this.cmdBackward.Text = "Backward";

     

    //

     

    // cmdLeft

     

    //

     

    this.cmdLeft.Location = new System.Drawing.Point(3, 117);

     

    this.cmdLeft.Name = "cmdLeft";

     

    this.cmdLeft.Size = new System.Drawing.Size(72, 20);

     

    this.cmdLeft.TabIndex = 3;

     

    this.cmdLeft.Text = "Left";

     

    //

     

    // blueSync

     

    //

     

    this.blueSync.Location = new System.Drawing.Point(84, 13);

     

    this.blueSync.Name = "blueSync";

     

    this.blueSync.Size = new System.Drawing.Size(72, 20);

     

    this.blueSync.TabIndex = 5;

     

    this.blueSync.Text = "Sync";

     

    this.blueSync.Click += new System.EventHandler(this.blueSync_Click);

     

    //

     

    // cmbDevices

     

    //

     

    this.cmbDevices.Location = new System.Drawing.Point(18, 223);

     

    this.cmbDevices.Name = "cmbDevices";

     

    this.cmbDevices.Size = new System.Drawing.Size(100, 22);

     

    this.cmbDevices.TabIndex = 6;

     

    //

     

    // turnOff

     

    //

     

    this.turnOff.Location = new System.Drawing.Point(138, 223);

     

    this.turnOff.Name = "turnOff";

     

    this.turnOff.Size = new System.Drawing.Size(72, 20);

     

    this.turnOff.TabIndex = 7;

     

    this.turnOff.Text = "Off";

     

    this.turnOff.Click += new System.EventHandler(this.turnOff_Click);

     

    //this.turnOff.KeyDown += new System.Windows.Forms.KeyEventHandler(this.button1_KeyDown);

     

    //

     

    // NXTButtonControl

     

    //

     

    this.AutoScaleDimensions = new System.Drawing.SizeF(96F, 96F);

     

    this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Dpi;

     

    this.AutoScroll = true;

     

    this.ClientSize = new System.Drawing.Size(240, 268);

     

    this.Controls.Add(this.turnOff);

     

    this.Controls.Add(this.cmbDevices);

     

    this.Controls.Add(this.blueSync);

     

    this.Controls.Add(this.cmdLeft);

     

    this.Controls.Add(this.cmdBackward);

     

    this.Controls.Add(this.cmdRight);

     

    this.Controls.Add(this.cmdForward);

     

    this.KeyPreview = true;

     

    this.Menu = this.mainMenu1;

     

    this.Name = "NXTButtonControl";

     

    this.Text = "NXT Button Control";

     

    this.KeyUp += new System.Windows.Forms.KeyEventHandler(this.NXTButtonControl_KeyUp);

     

    this.KeyDown += new System.Windows.Forms.KeyEventHandler(this.NXTButtonControl_KeyDown);

     

    this.ResumeLayout(false);

    }

    #endregion

     

    private System.Windows.Forms.Button cmdForward;

     

    private System.Windows.Forms.Button cmdRight;

     

    private System.Windows.Forms.Button cmdBackward;

     

    private System.Windows.Forms.Button cmdLeft;

     

    private System.Windows.Forms.Button blueSync;

     

    private System.Windows.Forms.ComboBox cmbDevices;

     

    private Button turnOff;

    }

    }

    Sunday, December 6, 2009 11:00 AM
  • Hmm, seems ok. It looks like it's the code You're putting into Mouse Down and Up events. 
    Try to just add a simple MessageBox.Show("Forward"); in cmdForward_MouseDown (nothing more). 

    If it will show You a messagebox it means that something is wrong with Your code inside button event.

    If You'll find my answer satisfactory or helpful - mark it as answered or vote for it! Thank You.
    If You think You know better then me, why is Your code not working, then don't waste my time at this forum. Otherwise - do as I'm suggesting.

    I'm on MSDN just like MD House in the clinic. But I'm also a human which sometimes needs to see another doctor :)
    Sunday, December 6, 2009 11:35 AM
  • I tried message boxes and putting break points in, neither show the events firing. At least I can now use the keyboard buttons on my phone, although its alot more awkward.

    Thanks again.
    Sunday, December 6, 2009 11:19 PM