Cómo: Utilizar el componente HardwareButton

Actualización: noviembre 2007

Puede configurar los botones de hardware en un Pocket PC para activar un objeto Form. Este ejemplo activa una aplicación con los botones de hardware primero y cuarto, e indica en la barra de estado cuál de ellos se ha presionado.

Para establecer un botón de hardware que active un formulario

  1. Cree una aplicación para Windows de Pocket PC.

  2. Cree una instancia de HardwareButton.

  3. Establezca la propiedad AssociatedControl en el formulario.

  4. Establezca la propiedad HardwareKey en una clave de aplicación definida por la enumeración HardwareKeys.

  5. Repita los pasos del 2 al 4 para los botones de hardware adicionales que desee utilizar.

  6. Cuando se presiona y se suelta un botón de hardware, el formulario recibe los dos eventos KeyDown y KeyUp. Puede utilizar cualquiera de ellos para determinar si un botón de hardware se ha presionado.

Ejemplo

Este ejemplo establece que los botones de hardware primero y cuarto activan el formulario. Para mostrarlo, siga estos pasos:

  1. Ejecute la aplicación.

  2. Abra otra aplicación en el dispositivo.

  3. Presione los botones de hardware 1 ó 4 para activar el formulario de esta aplicación. La barra de estado indica cuál de ellos se ha presionado.

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.
    Try
        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
// 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;
    }
}

Compilar el código

Para este ejemplo se requieren referencias a los siguientes espacios de nombres:

Vea también

Referencia

HardwareButton

Otros recursos

Desarrollo de Pocket PC y .NET Compact Framework