How to: Use the HardwareButton Component
Important This document may not represent best practices for current development, links to downloads and other resources may no longer be valid. Current recommended version can be found here. ArchiveDisclaimer

How to: Use the HardwareButton Component

You can configure the hardware buttons on a Pocket PC to activate a Form. This example activates an application with the first and fourth hardware buttons, and indicates in the status bar which button was pressed.

To set a hardware button to activate a form

  1. Create a Pocket PC Windows application.

  2. Create an instance of a HardwareButton.

  3. Set the AssociatedControl property to the form.

  4. Set the HardwareKey property to an application key defined by the HardwareKeys enumeration.

  5. Repeat steps 2 through 4 for additional hardware buttons you want to use.

  6. When a hardware button is pressed and released, the form receives both the KeyDown and KeyUp events. You can use either event to determine if a hardware button was pressed.

Example

This example sets the first and fourth hardware buttons to activate the form. To demonstrate:

  1. Run the application.

  2. Open another application on the device.

  3. Press hardware button 1 or 4 to activate the form of this application. The status bar indicates which hardware button was pressed.

// Configure hardware buttons
// 1 and 4 to activate the current form.
private void HBConfig()
    {
        try 
        {
            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.";
            break;

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

        default:
            break;
    }
}

Compiling the Code

This example requires references to the following namespaces:

See Also

Show:
© 2016 Microsoft