HardwareButton Class

Allows overriding the functionality of Pocket PC hardware buttons.

Namespace:  Microsoft.WindowsCE.Forms
Assembly:  Microsoft.WindowsCE.Forms (in Microsoft.WindowsCE.Forms.dll)


Public Class HardwareButton _
    Inherits Component
Dim instance As HardwareButton
public class HardwareButton : Component
public ref class HardwareButton : public Component
public class HardwareButton extends Component


You can configure a button on a Pocket PC to activate a Form, Panel, or custom control in your application by doing the following:

  • Create an instance of HardwareButton.

  • Set the AssociatedControl property to the form or control you want to activate.

  • Set the HardwareKey property to one of the HardwareKeys enumeration values. You can configure up to six hardware buttons.

When a hardware button is associated with a control, the control receives a KeyDown event when the button is pressed and a KeyUp event when the button is released.

To return a hardware button to its original state, set the AssociatedControl property to nulla null reference (Nothing in Visual Basic).

Note that some Pocket PCs have a different number of hardware buttons besides six. Also, not all buttons are supported by the operating system. Windows Mobile 2003 for Pocket PC supports four buttons and Windows Mobile version 5.0 software for Pocket PC supports five buttons.


The hardware buttons correspond to the application keys on a Pocket PC. The application keys are not included in the core key set, so their hardware key mapping may vary from one keyboard layout to the next. Only the default hardware key mapping on the device is supported.

This class is not supported on the Smartphone and other Windows CE devices that are not Pocket PCs.


The following code example shows how to activate a form using the first and fourth hardware buttons on a Pocket PC.

Imports System
Imports System.Windows.Forms
Imports Microsoft.WindowsCE.Forms

Public Class Form1
   Inherits System.Windows.Forms.Form
   Private statusBar1 As StatusBar
   Private hwb1 As HardwareButton
   Private hwb4 As HardwareButton

   Public Sub New()


      ' Display OK button to close the application. 
      Me.ControlBox = True 
      Me.MinimizeBox = False


   End Sub 

   Protected Overrides Sub Dispose(disposing As Boolean)
   End Sub 

   Private Sub InitializeComponent()
      Me.statusBar1 = New System.Windows.Forms.StatusBar()
      Me.hwb1 = New Microsoft.WindowsCE.Forms.HardwareButton
      Me.hwb4 = New Microsoft.WindowsCE.Forms.HardwareButton
      ' statusBar1 
      Me.statusBar1.Location = New System.Drawing.Point(0, 246)
      Me.statusBar1.Name = "statusBar1" 
      Me.statusBar1.Size = New System.Drawing.Size(240, 22)
      ' Form1 
      Me.ClientSize = New System.Drawing.Size(240, 268)
      Me.Name = "Form1" 
      Me.Text = "HW Button Test" 
      statusBar1.Text = "From another app, press button 1 or 4." 
   End Sub 

   Shared Sub Main()
      Application.Run(New Form1())
   End Sub 
   Private Sub ConfigHWButton()
      'Set KeyPreview to true so that the form  
      'will receive key events before they  
      'are passed to the control that has focus.  

       Me.KeyPreview = True

         hwb1 = New HardwareButton()
         hwb4 = New HardwareButton()

      'Set the AssociatedControl property 
      'to the current form and configure the 
      'first and fourth buttons to activate the form. 
         hwb1.AssociatedControl = Me
         hwb4.AssociatedControl = Me
         hwb1.HardwareKey = HardwareKeys.ApplicationKey1
         hwb4.HardwareKey = HardwareKeys.ApplicationKey4
      Catch exc As Exception
         MessageBox.Show(exc.Message + " Check if the hardware button is physically available on this device.")
      End Try 
    End Sub 

   Private Overloads Sub OnKeyUp(sender As Object, e As KeyEventArgs) Handles MyBase.KeyUp
       ' 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. 
       Select Case CType(e.KeyCode, HardwareKeys)
         Case HardwareKeys.ApplicationKey1
            statusBar1.Text = "Button 1 pressed." 

         Case HardwareKeys.ApplicationKey4
            statusBar1.Text = "Button 4 pressed." 

         Case Else 
      End Select 
   End Sub 
End Class
using System;
using System.Drawing;
using System.Collections;
using System.Windows.Forms;
using Microsoft.WindowsCE.Forms;

namespace HardwareButtonTest
    public class Form1 : System.Windows.Forms.Form
        private StatusBar statusBar1;
        private HardwareButton hwb1, hwb4;

        public Form1()


            // Display OK button to close the application. 
            this.ControlBox = true;
            this.MinimizeBox = false;

            // 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. 
            // Note that you could also use the KeyDown event 
            // instead of the KeyUp event. 
            this.KeyPreview = true;
            this.KeyUp += new KeyEventHandler(this.OnKeyUp);

            // Call the method to configure 
            // the hardware button.

        protected override void Dispose(bool disposing)
        private void InitializeComponent()
            this.statusBar1 = new System.Windows.Forms.StatusBar();
// statusBar1 
            this.statusBar1.Location = new System.Drawing.Point(0, 246);
            this.statusBar1.Name = "statusBar1";
            this.statusBar1.Size = new System.Drawing.Size(240, 22);
// Form1 
            this.ClientSize = new System.Drawing.Size(240, 268);
            this.Name = "Form1";
            this.Text = "HW Button Test";
            statusBar1.Text = "Press hardware button 1 or 4.";


        static void Main()
            Application.Run(new Form1());

        // Configure hardware buttons 
        // 1 and 4 to activate the current form. 
        private void HBConfig()
                    hwb1 = new HardwareButton();
                    hwb4 = new HardwareButton();
                    hwb1.AssociatedControl = this;
                    hwb4.AssociatedControl = this;
                    hwb1.HardwareKey = HardwareKeys.ApplicationKey1;
                    hwb4.HardwareKey = HardwareKeys.ApplicationKey4;
                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:
                    statusBar1.Text = "Button 1 pressed.";

                case HardwareKeys.ApplicationKey4:
                    statusBar1.Text = "Button 4 pressed.";


Inheritance Hierarchy


Thread Safety

Any public static (Shared in Visual Basic) members of this type are thread safe. Any instance members are not guaranteed to be thread safe.


Windows Mobile for Pocket PC

The .NET Framework and .NET Compact Framework do not support all versions of every platform. For a list of the supported versions, see .NET Framework System Requirements.

Version Information

.NET Compact Framework

Supported in: 3.5, 2.0

See Also


HardwareButton Members

Microsoft.WindowsCE.Forms Namespace

Other Resources

How to: Use the HardwareButton Component