4.2.1.2 イベントハンドリング


イベントハンドリング

To make the testing somewhat easier we will add a way to terminate the application by responding to the ESC key. Add the following private method to our class in `simple.h':

少し簡単なテストをするには、ESCキーを押すことで、アプリケーションを終了する方法を追加することでしょう。`simple.h`のSimpleクラスに、次のプライベイトメソッドを追加して下さい。

bool OnKeyboard (iEvent&);

The function OnKeyboard?() will be called when an event arrives Add the following code to `simple.cpp' just before Simple::OnInitialize?():

イベントが到着した時に、OnKeyboard?()機能が呼ばれます。`simple.cpp`のSimple::OnInitialize?()の前に、次のコードを追加して下さい。

bool Simple::OnKeyboard(iEvent& ev)
{
 csKeyEventType eventtype = csKeyEventHelper::GetEventType(&ev);
 if (eventtype == csKeyEventTypeDown)
 {
   utf32_char code = csKeyEventHelper::GetCookedCode(&ev);
   if (code == CSKEY_ESC)
   {
     csRef<iEventQueue> q =
       csQueryRegistry<iEventQueue> (GetObjectRegistry());
     if (q.IsValid()) q->GetEventOutlet()->Broadcast(csevQuit (
     	GetObjectRegistry ()));
   }
 }
 return false;
}

OnKeyboard?() checks if the ESC key has been pressed. If so it uses the object registry to find the global event queue object. Using Broadcast() it then broadcasts the `csevQuit' message to all interested parties. This will cause the application to quit by terminating the run-loop.

OnKeyboard?()は、ESCキーが押されたかをチェックします。もし押されていた場合、グローバルイベントキューオブジェクトを見つけるために、オブジェクトレジストリを使用します。Broadcast()を使用し、すべての関係のあるオブジェクトに`csevQuit`メッセージを送信します。`csevQuit`メッセージが送られると、run-loopが終了し、アプリケーションは終了します。