• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 375
  • Last Modified:

Hardware Button multiple clicks

I want to know how to avoid multiple clicks for a Hardware Button.  Should I add an UnConfigure option when the event is being handled?  I looked on the lists first for the answer.

      public ControlForm()
        {
            // Create event-handler delegate for the KeyUp
            // event for this form. This form is associated
            // with each of the hardware buttons, and the
            // event occurs when a hardware button is pressed.
            this.KeyPreview = true;
            this.KeyUp += new KeyEventHandler(this.OnKeyUp);
            this.ControlBox = false;

            lastControlForm = this;

            f1 = new HardwareButton();
            f2 = new HardwareButton();
            f3 = new HardwareButton();
            f4 = new HardwareButton();
            f5 = new HardwareButton();

            // Call the method to configure
            // the hardware buttons.
            HBConfig();
        }
        #endregion

        #region public methods
        /// <summary>
        /// Default show method - overridden
        /// </summary>
        public new void Show()
        {
            // show the form first than do the configuration
            base.Show();
            HBConfig();
        }

        /// <summary>
        /// Show method to store this as the last form
        /// </summary>
        /// <param name="lastForm"></param>
        public void Show(ControlForm lastForm)
        {
            this.lastControlForm = lastForm;
            this.Show();
        }

        /// <summary>
        /// Show the last Control Form
        /// </summary>
        public void DoBack()
        {
            this.lastControlForm.Show();
        }
        #endregion

        #region private methods
        /// <summary>
        /// Configure Hardware Buttons
        /// </summary>
        private void HBConfig()
        {
            try
            {
                f1.AssociatedControl = this;
                f2.AssociatedControl = this;
                f3.AssociatedControl = this;
                f4.AssociatedControl = this;
                f5.AssociatedControl = this;

                f1.HardwareKey = HardwareKeys.ApplicationKey1;
                f2.HardwareKey = HardwareKeys.ApplicationKey2;
                f3.HardwareKey = HardwareKeys.ApplicationKey3;
                f4.HardwareKey = HardwareKeys.ApplicationKey4;
                f2.HardwareKey = HardwareKeys.ApplicationKey5;
            }
            catch (Exception exc)
            {
                MessageBox.Show(exc.Message + " Check if the hardware button is physically available on this device.");
            }
        }

        // When a hardware button is pressed and released,
        // this form receives the KeyUp event. The OnKeyUp
        // method is used to determine which hardware
        // button was pressed, because the event data
        // specifies a member of the HardwareKeys enumeration.
        private void OnKeyUp(object sender, KeyEventArgs e)
        {
            switch ((HardwareKeys)e.KeyCode)
            {
                case HardwareKeys.ApplicationKey1:
                    DoF1Pressed();
                    break;

                case HardwareKeys.ApplicationKey2:
                    DoF2Pressed();
                    break;

                case HardwareKeys.ApplicationKey3:
                    DoF3Pressed();
                    break;

                case HardwareKeys.ApplicationKey4:
                    DoF4Pressed();
                    break;

                case HardwareKeys.ApplicationKey5:
                    DoF5Pressed();
                    break;

                default:
                    break;
            }
        }
0
MJohn10296
Asked:
MJohn10296
  • 2
  • 2
1 Solution
 
JoeisanerdCommented:
are you using a pda? and is this for a  game?
0
 
MJohn10296Author Commented:
This is not for a game. Yes, this is for a handheld running WindowsCE
0
 
MJohn10296Author Commented:
I'll move it to PocketPC
0
 
JoeisanerdCommented:
you don't have to move it to pocketPC this is a C# question

When programming games even with C# there is a library that you can use to get full control of the buttons and then you can just ask to see if a button is currently be pressed down when the time is right.

In your scenario I would keep some flags for each button and when the user clicks set the flag equal to the current tick count.

//initialize at top of class
long hButton1 =0;

// in the key_up event, check for a difference of maybe 300 ms
if( (System.DateTime.Now.Ticks - hButton1) > 300 )
{
     hButton1 = System.DateTime.Now.Ticks;
     // proced with the event.

}


0

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

  • 2
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now