Export (0) Print
Expand All

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.

NoteNote:

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 values may vary from one keyboard layout to the next. Only the default key mapping is supported by HardwareButton. For an example of handling key input from other hardware keys, see How to: Detect Navigation Keys.

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 whether a hardware button was pressed.

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

  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;
    }
}

This example requires references to the following namespaces:

Community Additions

ADD
Show:
© 2014 Microsoft