File: cs_75.html

package info (click to toggle)
crystalspace 0.94-20020412-3
  • links: PTS
  • area: main
  • in suites: woody
  • size: 62,276 kB
  • ctags: 52,843
  • sloc: cpp: 274,783; ansic: 6,608; perl: 6,276; objc: 3,952; asm: 2,942; python: 2,354; php: 542; pascal: 530; sh: 430; makefile: 370; awk: 193
file content (118 lines) | stat: -rw-r--r-- 4,467 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<!-- Created by texi2html 1.64 -->
<!-- 
Written by: Lionel Cons <Lionel.Cons@cern.ch> (original author)
            Karl Berry  <karl@freefriends.org>
            Olaf Bachmann <obachman@mathematik.uni-kl.de>
            and many others.
Maintained by: Olaf Bachmann <obachman@mathematik.uni-kl.de>
Send bugs and suggestions to <texi2html@mathematik.uni-kl.de>
 
-->
<HTML>
<HEAD>
<TITLE>Crystal Space: Simple Event Handling</TITLE>

<META NAME="description" CONTENT="Crystal Space: Simple Event Handling">
<META NAME="keywords" CONTENT="Crystal Space: Simple Event Handling">
<META NAME="resource-type" CONTENT="document">
<META NAME="distribution" CONTENT="global">
<META NAME="Generator" CONTENT="texi2html 1.64">

</HEAD>

<BODY LANG="" BGCOLOR="#FFFFFF" TEXT="#000000" LINK="#0000FF" VLINK="#800080" ALINK="#FF0000">

<A NAME="SEC173"></A>
<TABLE CELLPADDING=1 CELLSPACING=1 BORDER=0>
<TR><TD VALIGN="MIDDLE" ALIGN="LEFT">[<A HREF="cs_74.html#SEC172"> &lt; </A>]</TD>
<TD VALIGN="MIDDLE" ALIGN="LEFT">[<A HREF="cs_76.html#SEC174"> &gt; </A>]</TD>
<TD VALIGN="MIDDLE" ALIGN="LEFT"> &nbsp; <TD VALIGN="MIDDLE" ALIGN="LEFT">[<A HREF="cs_70.html#SEC158"> &lt;&lt; </A>]</TD>
<TD VALIGN="MIDDLE" ALIGN="LEFT">[<A HREF="cs_73.html#SEC171"> Up </A>]</TD>
<TD VALIGN="MIDDLE" ALIGN="LEFT">[<A HREF="cs_79.html#SEC177"> &gt;&gt; </A>]</TD>
<TD VALIGN="MIDDLE" ALIGN="LEFT"> &nbsp; <TD VALIGN="MIDDLE" ALIGN="LEFT"> &nbsp; <TD VALIGN="MIDDLE" ALIGN="LEFT"> &nbsp; <TD VALIGN="MIDDLE" ALIGN="LEFT"> &nbsp; <TD VALIGN="MIDDLE" ALIGN="LEFT">[<A HREF="index.html#SEC_Top">Top</A>]</TD>
<TD VALIGN="MIDDLE" ALIGN="LEFT">[<A HREF="cs_toc.html#SEC_Contents">Contents</A>]</TD>
<TD VALIGN="MIDDLE" ALIGN="LEFT">[<A HREF="cs_285.html#SEC711">Index</A>]</TD>
<TD VALIGN="MIDDLE" ALIGN="LEFT">[<A HREF="cs_abt.html#SEC_About"> ? </A>]</TD>
</TR></TABLE>
<HR SIZE=1>
<H3> 5.2.2 Event Handling </H3>
<!--docid::SEC173::-->
<P>

To make the testing somewhat easier we will add a way to terminate the
application by responding to the <KBD>ESC</KBD> key.  Add the following private
methods to our class in `<TT>simple.h</TT>':
</P><P>

<TABLE><tr><td>&nbsp;</td><td class=example><pre>  static bool SimpleEventHandler (iEvent&#38; ev);
  bool HandleEvent (iEvent&#38; ev);
</pre></td></tr></table></P><P>

The static function <CODE>SimpleEventHandler()</CODE> will be registered to
the event queue. It has to be a static function because the event queue
expects to call a normal C-style function (alternatively you could also
implement this by making a subclass of <CODE>iEventHandler</CODE> but this is
more complicated and not really required).
</P><P>

<CODE>HandleEvent()</CODE> is the real event handler. <CODE>SimpleEventHandler()</CODE>
only serves as a stub function to call <CODE>HandleEvent()</CODE>.
</P><P>

Then add the following code before <CODE>Simple::Initialize()</CODE>:
</P><P>

<TABLE><tr><td>&nbsp;</td><td class=example><pre>bool Simple::HandleEvent (iEvent&#38; ev)
{
  if (ev.Type == csevKeyDown &#38;&#38; ev.Key.Code == CSKEY_ESC)
  {
    iEventQueue* q = CS_QUERY_REGISTRY (object_reg, iEventQueue);
    if (q)
    {
      q-&#62;GetEventOutlet()-&#62;Broadcast (cscmdQuit);
      q-&#62;DecRef ();
    }
    return true;
  }
  return false;
}

bool Simple::SimpleEventHandler (iEvent&#38; ev)
{
  return simple-&#62;HandleEvent (ev);
}
</pre></td></tr></table></P><P>

<CODE>HandleEvent()</CODE> checks if the <KBD>ESC</KBD> key has been pressed. If so it
will use the object registry to find the global event queue object. Using
<CODE>Broadcast</CODE> it will then broadcast the <CODE>cscmdQuit</CODE> message
to all interested parties. This will cause the application to quit by ending
the default run loop.
</P><P>

We also need to initialize this event handler function. To do that add
the following after the call to <CODE>csInitializer::RequestPlugins()</CODE>:
</P><P>

<TABLE><tr><td>&nbsp;</td><td class=example><pre>  if (!csInitializer::SetupEventHandler(
    object_reg, SimpleEventHandler))
  {
    csReport (object_reg, CS_REPORTER_SEVERITY_ERROR,
    	"crystalspace.application.simple",
	"Can't initialize event handler!");
    return false;
  }
</pre></td></tr></table></P><P>

<A NAME="Simple World"></A>
<HR SIZE=1>
<BR>  
<FONT SIZE="-1">
This document was generated

using <A HREF="http://www.mathematik.uni-kl.de/~obachman/Texi2html
"><I>texi2html</I></A>

</BODY>
</HTML>