次にWiiRemoteのボタンについて実践的なプログラミングを行っていきます。
ここでは、Wiiリモコンのボタンを押すと指定したアプリケーションが起動するプログラムを作成します。
1.WiimoteLibの宣言と制御プログラム
Form1.csに以下の部分を追加する。using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Linq; using System.Text; using System.Windows.Forms; using WiimoteLib; //WimoteLib namespace WiimoteLib_Sample { public partial class Form1 : Form { Wiimote wm = new Wiimote(); //Wiimoteの宣言 public Form1() { InitializeComponent(); wm.WiimoteChanged += wm_WiimoteChanged; //イベント関数の登録 wm.Connect(); //Wiimoteの接続 } //Wiiリモコンの値が変化したときに呼ばれる関数 void wm_WiimoteChanged(object sender, WiimoteChangedEventArgs args){ WiimoteState ws = args.WiimoteState; //WiimoteStateの値を取得 if (ws.ButtonState.A == true) { //Aボタンがおされたらメモ帳を起動 System.Diagnostics.Process.Start("notepad.exe"); } if (ws.ButtonState.B == true) { //Bボタンがおされたら電卓を起動 System.Diagnostics.Process.Start("calc.exe"); } } } }
2.実行
1.WiiRemoteを接続してください。
2.F5キーを押して実行してください。
※もしエラーが発生する場合はWiiRemoteが正しく接続されているか確認してください。
3.WiiリモコンのAボタンをクリックしてください。
メモ帳が起動します。
4.WiiリモコンのBボタンを押してください。
電卓が起動します。
5.WiiリモコンのBボタンを数回押してしてください。
電卓が押した回数分起動します。
3.解説
Wiiリモコンのボタンが押されることによって設定したアプリケーションが起動します。
if (ws.ButtonState.A == true) {
//Aボタンがおされたらメモ帳を起動
System.Diagnostics.Process.Start("notepad.exe");
}
WiiリモコンのAボタンが押されたときに、ws.ButtonState.Aの値がtrueになります。 if文でtrueの時 notepad.exe(メモ帳)を起動します。
if (ws.ButtonState.B == true) {
//Bボタンがおされたら電卓を起動
System.Diagnostics.Process.Start("calc.exe");
}
WiiリモコンのBボタンが押されたときに、ws.ButtonState.Bの値がtrueになります。 if文でtrueの時 calc.exe(電卓)を起動します。

