方法 : HardwareButton コンポーネントを使用します。
[このドキュメントはプレビュー版であり、後のリリースで変更されることがあります。 空白のトピックは、プレースホルダーとして挿入されています。]
ハードウェア ボタンを Form をアクティブに Pocket PC で構成できます。 この例は、1 番目と 4 番目のハードウェア ボタンを持つアプリケーションをアクティブにし、バーのボタンが押された状態で示します。
注意
ハードウェア ボタンは、Pocket PC でアプリケーション キーに対応します。その値へ 1 つのキーボード レイアウトによって異なりますが、アプリケーション キーは、コア キー セットに含まれません。既定のキー マッピングだけは HardwareButton でサポートされます。他のハードウェア キーからのキー入力の処理の例、「方法 : 移動キーを検出します。」を参照してください。
フォームをアクティブにするためのハードウェア ボタンを設定するには
Pocket PC の Windows アプリケーションを作成します。
HardwareButton のインスタンスを作成します。
AssociatedControl プロパティは、フォームに設定します。
HardwareKey プロパティは、 HardwareKeys 列挙で定義されたアプリケーション キーに設定します。
手順 2 ~ 4 を使用する追加のハードウェア ボタンを繰り返します。
ハードウェア ボタンが押されてリリースされたとき、フォームは、KeyDown と KeyUp イベントの両方を受け取ります。 いずれかイベントを使用するハードウェア ボタンが押されたかどうかを判断できます。
使用例
次の使用例、1 番目と 4 番目のハードウェア ボタンのフォームをアクティブに設定します。 示すには、次の手順に従います。
アプリケーションを実行します。
デバイス上の別のアプリケーションを開きます。
ハードウェア ボタン 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.")
EndTryEndSubPrivateOverloadsSub OnKeyUp(sender AsObject, e As KeyEventArgs) _
HandlesMyBase.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.SelectCaseCType(e.KeyCode, HardwareKeys)
Case HardwareKeys.ApplicationKey1
statusBar1.Text = "Button 1 pressed."Case HardwareKeys.ApplicationKey4
statusBar1.Text = "Button 4 pressed."CaseElseEndSelectEndSub
// 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.privatevoid 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;
}
}
コードのコンパイル方法
この例では、次の名前空間への参照が必要です。