Compartilhar via


Como: Use o componente HardwareButton

Você pode configurar os botões de hardware em um Pocket PC para ativar um Form. Este exemplo ativa um aplicativo com os botões de hardware primeiro e quarto e indica o barra de status qual botão foi pressionado.

Para definir um botão físico para ativar um formulário

  1. Crie um aplicativo do Windows Pocket PC.

  2. Criar uma instância de um HardwareButton.

  3. conjunto o AssociatedControl propriedade para o formulário.

  4. conjunto o HardwareKey propriedade como uma tecla de aplicativo definida pela HardwareKeys enumeração.

  5. Repita as etapas 2 a 4 para botões físicos adicionais que deseja usar.

  6. Quando um botão de hardware é pressionado e liberado, o formulário recebe o KeyDown e KeyUp eventos. Você pode usar qualquer evento para determinar se um botão físico foi pressionado.

Exemplo

Este exemplo define que o primeiro e quarto botões físicos ativem o formulário.Para demonstrar, execute as seguintes etapas:

  1. Execute o aplicativo.

  2. Abra outro aplicativo no dispositivo.

  3. Pressione o botão 1 ou 4 para ativar o formulário deste aplicativo.A barra de status indica qual botão físico foi pressionado.

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

Compilando o código

Este exemplo requer referências aos seguintes namespaces:

Consulte também

Referência

HardwareButton

Outros recursos

Desenvolvimento para Pocket PC e o .NET Compact Framework