방법: HardwareButton 구성 요소 사용

업데이트: 2007년 11월

Form이 활성화되도록 Pocket PC의 하드웨어 단추를 구성할 수 있습니다. 이 예제에서는 첫 번째와 네 번째 하드웨어 단추로 응용 프로그램을 활성화하고 사용자가 누른 단추를 상태 표시줄에 표시합니다.

폼을 활성화하도록 하드웨어 단추를 설정하려면

  1. Pocket PC Windows 응용 프로그램을 만듭니다.

  2. HardwareButton의 인스턴스를 만듭니다.

  3. AssociatedControl 속성을 폼에 설정합니다.

  4. HardwareKeys 열거형으로 정의된 응용 프로그램 키에 HardwareKey 속성을 설정합니다.

  5. 사용하고자 하는 추가 하드웨어 단추에 대해 2단계에서 6단계까지를 반복합니다.

  6. 하드웨어 단추를 눌렀다 놓으면 폼은 KeyDownKeyUp 이벤트를 받습니다. 이 이벤트 중 하나를 사용하여 하드웨어 단추를 눌렀는지 여부를 확인할 수 있습니다.

예제

이 예제에서는 폼을 활성화하도록 첫 번째와 네 번째 하드웨어 단추를 설정합니다. 테스트하려면 다음 단계를 수행합니다.

  1. 응용 프로그램을 실행합니다.

  2. 장치에서 다른 응용 프로그램을 엽니다.

  3. 하드웨어 단추 1 또는 4를 눌러 이 응용 프로그램의 폼을 활성화합니다. 현재 눌려 있는 상태가 표시됩니다.

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

코드 컴파일

이 예제에는 다음과 같은 네임스페이스에 대한 참조가 필요합니다.

참고 항목

참조

HardwareButton

기타 리소스

Pocket PC 개발 및 .NET Compact Framework