author | Eugene Ostroukhov <eugeneo@symbian.org> |
Wed, 14 Apr 2010 14:54:13 -0700 | |
changeset 305 | be8783adb3a8 |
parent 230 | 7848c135d915 |
permissions | -rw-r--r-- |
230
7848c135d915
Fixed 2046 - WRTKit Help. Renamed package for consistency.
tasneems@symbian.org
parents:
diff
changeset
|
1 |
<?xml version="1.0" encoding="UTF-8"?><!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> |
7848c135d915
Fixed 2046 - WRTKit Help. Renamed package for consistency.
tasneems@symbian.org
parents:
diff
changeset
|
2 |
<html lang="en" xml:lang="en"> |
7848c135d915
Fixed 2046 - WRTKit Help. Renamed package for consistency.
tasneems@symbian.org
parents:
diff
changeset
|
3 |
<head> |
7848c135d915
Fixed 2046 - WRTKit Help. Renamed package for consistency.
tasneems@symbian.org
parents:
diff
changeset
|
4 |
<meta content="text/html; charset=utf-8" http-equiv="Content-Type" /> |
7848c135d915
Fixed 2046 - WRTKit Help. Renamed package for consistency.
tasneems@symbian.org
parents:
diff
changeset
|
5 |
<meta name="copyright" content="(C) Copyright 2005" /> |
7848c135d915
Fixed 2046 - WRTKit Help. Renamed package for consistency.
tasneems@symbian.org
parents:
diff
changeset
|
6 |
<meta name="DC.rights.owner" content="(C) Copyright 2005" /> |
7848c135d915
Fixed 2046 - WRTKit Help. Renamed package for consistency.
tasneems@symbian.org
parents:
diff
changeset
|
7 |
<meta content="concept" name="DC.Type" /> |
7848c135d915
Fixed 2046 - WRTKit Help. Renamed package for consistency.
tasneems@symbian.org
parents:
diff
changeset
|
8 |
<meta name="DC.Title" content="Handling events" /> |
7848c135d915
Fixed 2046 - WRTKit Help. Renamed package for consistency.
tasneems@symbian.org
parents:
diff
changeset
|
9 |
<meta scheme="URI" name="DC.Relation" content="WRTKit_Common_WRTKit_tasks-GUID-24870895-4449-4307-9a54-7c90f7b3905e.html" /> |
7848c135d915
Fixed 2046 - WRTKit Help. Renamed package for consistency.
tasneems@symbian.org
parents:
diff
changeset
|
10 |
<meta content="XHTML" name="DC.Format" /> |
7848c135d915
Fixed 2046 - WRTKit Help. Renamed package for consistency.
tasneems@symbian.org
parents:
diff
changeset
|
11 |
<meta content="GUID-A1A86C8A-6E66-4DC8-8967-B5C9C7BC6563" name="DC.Identifier" /> |
7848c135d915
Fixed 2046 - WRTKit Help. Renamed package for consistency.
tasneems@symbian.org
parents:
diff
changeset
|
12 |
<meta content="en" name="DC.Language" /> |
7848c135d915
Fixed 2046 - WRTKit Help. Renamed package for consistency.
tasneems@symbian.org
parents:
diff
changeset
|
13 |
<link href="commonltr.css" type="text/css" rel="stylesheet" /> |
7848c135d915
Fixed 2046 - WRTKit Help. Renamed package for consistency.
tasneems@symbian.org
parents:
diff
changeset
|
14 |
<title> |
7848c135d915
Fixed 2046 - WRTKit Help. Renamed package for consistency.
tasneems@symbian.org
parents:
diff
changeset
|
15 |
Handling events</title> |
7848c135d915
Fixed 2046 - WRTKit Help. Renamed package for consistency.
tasneems@symbian.org
parents:
diff
changeset
|
16 |
</head> |
7848c135d915
Fixed 2046 - WRTKit Help. Renamed package for consistency.
tasneems@symbian.org
parents:
diff
changeset
|
17 |
<body id="GUID-A1A86C8A-6E66-4DC8-8967-B5C9C7BC6563"><a name="GUID-A1A86C8A-6E66-4DC8-8967-B5C9C7BC6563"><!-- --></a> |
7848c135d915
Fixed 2046 - WRTKit Help. Renamed package for consistency.
tasneems@symbian.org
parents:
diff
changeset
|
18 |
|
7848c135d915
Fixed 2046 - WRTKit Help. Renamed package for consistency.
tasneems@symbian.org
parents:
diff
changeset
|
19 |
|
7848c135d915
Fixed 2046 - WRTKit Help. Renamed package for consistency.
tasneems@symbian.org
parents:
diff
changeset
|
20 |
|
7848c135d915
Fixed 2046 - WRTKit Help. Renamed package for consistency.
tasneems@symbian.org
parents:
diff
changeset
|
21 |
<h1 class="topictitle1"> |
7848c135d915
Fixed 2046 - WRTKit Help. Renamed package for consistency.
tasneems@symbian.org
parents:
diff
changeset
|
22 |
Handling events</h1> |
7848c135d915
Fixed 2046 - WRTKit Help. Renamed package for consistency.
tasneems@symbian.org
parents:
diff
changeset
|
23 |
|
7848c135d915
Fixed 2046 - WRTKit Help. Renamed package for consistency.
tasneems@symbian.org
parents:
diff
changeset
|
24 |
<div> |
7848c135d915
Fixed 2046 - WRTKit Help. Renamed package for consistency.
tasneems@symbian.org
parents:
diff
changeset
|
25 |
|
7848c135d915
Fixed 2046 - WRTKit Help. Renamed package for consistency.
tasneems@symbian.org
parents:
diff
changeset
|
26 |
<p> |
7848c135d915
Fixed 2046 - WRTKit Help. Renamed package for consistency.
tasneems@symbian.org
parents:
diff
changeset
|
27 |
|
7848c135d915
Fixed 2046 - WRTKit Help. Renamed package for consistency.
tasneems@symbian.org
parents:
diff
changeset
|
28 |
Event handling in the WRTKit is based on the "observer pattern", |
7848c135d915
Fixed 2046 - WRTKit Help. Renamed package for consistency.
tasneems@symbian.org
parents:
diff
changeset
|
29 |
meaning that events are reported from the event source to observers |
7848c135d915
Fixed 2046 - WRTKit Help. Renamed package for consistency.
tasneems@symbian.org
parents:
diff
changeset
|
30 |
that are said to be "listening" to event notifications. An event |
7848c135d915
Fixed 2046 - WRTKit Help. Renamed package for consistency.
tasneems@symbian.org
parents:
diff
changeset
|
31 |
listener is simply a JavaScript function that takes a single |
7848c135d915
Fixed 2046 - WRTKit Help. Renamed package for consistency.
tasneems@symbian.org
parents:
diff
changeset
|
32 |
argument: the event message. In fact even that single argument is |
7848c135d915
Fixed 2046 - WRTKit Help. Renamed package for consistency.
tasneems@symbian.org
parents:
diff
changeset
|
33 |
optional and if the function is not interested in the event message |
7848c135d915
Fixed 2046 - WRTKit Help. Renamed package for consistency.
tasneems@symbian.org
parents:
diff
changeset
|
34 |
then it can simply ignore it. |
7848c135d915
Fixed 2046 - WRTKit Help. Renamed package for consistency.
tasneems@symbian.org
parents:
diff
changeset
|
35 |
</p> |
7848c135d915
Fixed 2046 - WRTKit Help. Renamed package for consistency.
tasneems@symbian.org
parents:
diff
changeset
|
36 |
|
7848c135d915
Fixed 2046 - WRTKit Help. Renamed package for consistency.
tasneems@symbian.org
parents:
diff
changeset
|
37 |
<p> |
7848c135d915
Fixed 2046 - WRTKit Help. Renamed package for consistency.
tasneems@symbian.org
parents:
diff
changeset
|
38 |
|
7848c135d915
Fixed 2046 - WRTKit Help. Renamed package for consistency.
tasneems@symbian.org
parents:
diff
changeset
|
39 |
All views and controls inherit from a common base class called |
7848c135d915
Fixed 2046 - WRTKit Help. Renamed package for consistency.
tasneems@symbian.org
parents:
diff
changeset
|
40 |
"UIElement" that defines the mechanics for event observation and |
7848c135d915
Fixed 2046 - WRTKit Help. Renamed package for consistency.
tasneems@symbian.org
parents:
diff
changeset
|
41 |
notification. From the point of view of event listeners, the most |
7848c135d915
Fixed 2046 - WRTKit Help. Renamed package for consistency.
tasneems@symbian.org
parents:
diff
changeset
|
42 |
important methods are addEventListener() and removeEventListener(). |
7848c135d915
Fixed 2046 - WRTKit Help. Renamed package for consistency.
tasneems@symbian.org
parents:
diff
changeset
|
43 |
These two methods are used to register and unregister listener |
7848c135d915
Fixed 2046 - WRTKit Help. Renamed package for consistency.
tasneems@symbian.org
parents:
diff
changeset
|
44 |
functions from a view or control. |
7848c135d915
Fixed 2046 - WRTKit Help. Renamed package for consistency.
tasneems@symbian.org
parents:
diff
changeset
|
45 |
</p> |
7848c135d915
Fixed 2046 - WRTKit Help. Renamed package for consistency.
tasneems@symbian.org
parents:
diff
changeset
|
46 |
|
7848c135d915
Fixed 2046 - WRTKit Help. Renamed package for consistency.
tasneems@symbian.org
parents:
diff
changeset
|
47 |
<p> |
7848c135d915
Fixed 2046 - WRTKit Help. Renamed package for consistency.
tasneems@symbian.org
parents:
diff
changeset
|
48 |
|
7848c135d915
Fixed 2046 - WRTKit Help. Renamed package for consistency.
tasneems@symbian.org
parents:
diff
changeset
|
49 |
There are different types of events though, and event listeners are |
7848c135d915
Fixed 2046 - WRTKit Help. Renamed package for consistency.
tasneems@symbian.org
parents:
diff
changeset
|
50 |
typically not interested in receiving notifications of all events. |
7848c135d915
Fixed 2046 - WRTKit Help. Renamed package for consistency.
tasneems@symbian.org
parents:
diff
changeset
|
51 |
For example an event listener that wants to know when a button has |
7848c135d915
Fixed 2046 - WRTKit Help. Renamed package for consistency.
tasneems@symbian.org
parents:
diff
changeset
|
52 |
been pressed doesn't usually care if the pointer is currently |
7848c135d915
Fixed 2046 - WRTKit Help. Renamed package for consistency.
tasneems@symbian.org
parents:
diff
changeset
|
53 |
hovering above the button or not. Filtering of event notifications |
7848c135d915
Fixed 2046 - WRTKit Help. Renamed package for consistency.
tasneems@symbian.org
parents:
diff
changeset
|
54 |
works based on event type names. E.g. in this case the event |
7848c135d915
Fixed 2046 - WRTKit Help. Renamed package for consistency.
tasneems@symbian.org
parents:
diff
changeset
|
55 |
listener would have been added so that it should be called only for |
7848c135d915
Fixed 2046 - WRTKit Help. Renamed package for consistency.
tasneems@symbian.org
parents:
diff
changeset
|
56 |
events of the "ActionPerformed" type. The event type is given to the |
7848c135d915
Fixed 2046 - WRTKit Help. Renamed package for consistency.
tasneems@symbian.org
parents:
diff
changeset
|
57 |
addEventListener() function when a listener is registered. If a |
7848c135d915
Fixed 2046 - WRTKit Help. Renamed package for consistency.
tasneems@symbian.org
parents:
diff
changeset
|
58 |
listener function really wants to be notified of all event types |
7848c135d915
Fixed 2046 - WRTKit Help. Renamed package for consistency.
tasneems@symbian.org
parents:
diff
changeset
|
59 |
then null can be specified as the event type. Note that the event |
7848c135d915
Fixed 2046 - WRTKit Help. Renamed package for consistency.
tasneems@symbian.org
parents:
diff
changeset
|
60 |
type must also be specified when an event listener is unregistered. |
7848c135d915
Fixed 2046 - WRTKit Help. Renamed package for consistency.
tasneems@symbian.org
parents:
diff
changeset
|
61 |
</p> |
7848c135d915
Fixed 2046 - WRTKit Help. Renamed package for consistency.
tasneems@symbian.org
parents:
diff
changeset
|
62 |
|
7848c135d915
Fixed 2046 - WRTKit Help. Renamed package for consistency.
tasneems@symbian.org
parents:
diff
changeset
|
63 |
<p> |
7848c135d915
Fixed 2046 - WRTKit Help. Renamed package for consistency.
tasneems@symbian.org
parents:
diff
changeset
|
64 |
|
7848c135d915
Fixed 2046 - WRTKit Help. Renamed package for consistency.
tasneems@symbian.org
parents:
diff
changeset
|
65 |
The code below shows a typical event listener function: |
7848c135d915
Fixed 2046 - WRTKit Help. Renamed package for consistency.
tasneems@symbian.org
parents:
diff
changeset
|
66 |
</p> |
7848c135d915
Fixed 2046 - WRTKit Help. Renamed package for consistency.
tasneems@symbian.org
parents:
diff
changeset
|
67 |
|
7848c135d915
Fixed 2046 - WRTKit Help. Renamed package for consistency.
tasneems@symbian.org
parents:
diff
changeset
|
68 |
<pre> |
7848c135d915
Fixed 2046 - WRTKit Help. Renamed package for consistency.
tasneems@symbian.org
parents:
diff
changeset
|
69 |
|
7848c135d915
Fixed 2046 - WRTKit Help. Renamed package for consistency.
tasneems@symbian.org
parents:
diff
changeset
|
70 |
// Callback for event notifications. |
7848c135d915
Fixed 2046 - WRTKit Help. Renamed package for consistency.
tasneems@symbian.org
parents:
diff
changeset
|
71 |
function handleEvent(event) { |
7848c135d915
Fixed 2046 - WRTKit Help. Renamed package for consistency.
tasneems@symbian.org
parents:
diff
changeset
|
72 |
// handle event here |
7848c135d915
Fixed 2046 - WRTKit Help. Renamed package for consistency.
tasneems@symbian.org
parents:
diff
changeset
|
73 |
} |
7848c135d915
Fixed 2046 - WRTKit Help. Renamed package for consistency.
tasneems@symbian.org
parents:
diff
changeset
|
74 |
</pre> |
7848c135d915
Fixed 2046 - WRTKit Help. Renamed package for consistency.
tasneems@symbian.org
parents:
diff
changeset
|
75 |
|
7848c135d915
Fixed 2046 - WRTKit Help. Renamed package for consistency.
tasneems@symbian.org
parents:
diff
changeset
|
76 |
<p> |
7848c135d915
Fixed 2046 - WRTKit Help. Renamed package for consistency.
tasneems@symbian.org
parents:
diff
changeset
|
77 |
|
7848c135d915
Fixed 2046 - WRTKit Help. Renamed package for consistency.
tasneems@symbian.org
parents:
diff
changeset
|
78 |
The event message is passed to the first argument (called event in |
7848c135d915
Fixed 2046 - WRTKit Help. Renamed package for consistency.
tasneems@symbian.org
parents:
diff
changeset
|
79 |
this case) of the event handler function. The event message is a |
7848c135d915
Fixed 2046 - WRTKit Help. Renamed package for consistency.
tasneems@symbian.org
parents:
diff
changeset
|
80 |
JavaScript object with three properties: type, source and value. The |
7848c135d915
Fixed 2046 - WRTKit Help. Renamed package for consistency.
tasneems@symbian.org
parents:
diff
changeset
|
81 |
type property specifies the event type name and is useful if a |
7848c135d915
Fixed 2046 - WRTKit Help. Renamed package for consistency.
tasneems@symbian.org
parents:
diff
changeset
|
82 |
listener function is listening to several types of events. The |
7848c135d915
Fixed 2046 - WRTKit Help. Renamed package for consistency.
tasneems@symbian.org
parents:
diff
changeset
|
83 |
source argument is a reference to the source view or control that |
7848c135d915
Fixed 2046 - WRTKit Help. Renamed package for consistency.
tasneems@symbian.org
parents:
diff
changeset
|
84 |
sent out the event notification. If a listener function is listening |
7848c135d915
Fixed 2046 - WRTKit Help. Renamed package for consistency.
tasneems@symbian.org
parents:
diff
changeset
|
85 |
to events from many different controls then this is useful to figure |
7848c135d915
Fixed 2046 - WRTKit Help. Renamed package for consistency.
tasneems@symbian.org
parents:
diff
changeset
|
86 |
out in which of the controls the event occurred. Here the unique |
7848c135d915
Fixed 2046 - WRTKit Help. Renamed package for consistency.
tasneems@symbian.org
parents:
diff
changeset
|
87 |
identifier of views and controls can come in handy to identify the |
7848c135d915
Fixed 2046 - WRTKit Help. Renamed package for consistency.
tasneems@symbian.org
parents:
diff
changeset
|
88 |
source without needing to retain references to all the source |
7848c135d915
Fixed 2046 - WRTKit Help. Renamed package for consistency.
tasneems@symbian.org
parents:
diff
changeset
|
89 |
controls. Finally the value property is a event-type specific |
7848c135d915
Fixed 2046 - WRTKit Help. Renamed package for consistency.
tasneems@symbian.org
parents:
diff
changeset
|
90 |
property that contains some information about the event. For example |
7848c135d915
Fixed 2046 - WRTKit Help. Renamed package for consistency.
tasneems@symbian.org
parents:
diff
changeset
|
91 |
if the event type is "TextChanged" from a text entry control then |
7848c135d915
Fixed 2046 - WRTKit Help. Renamed package for consistency.
tasneems@symbian.org
parents:
diff
changeset
|
92 |
the value would be the new text value that the user has typed into |
7848c135d915
Fixed 2046 - WRTKit Help. Renamed package for consistency.
tasneems@symbian.org
parents:
diff
changeset
|
93 |
the control. |
7848c135d915
Fixed 2046 - WRTKit Help. Renamed package for consistency.
tasneems@symbian.org
parents:
diff
changeset
|
94 |
</p> |
7848c135d915
Fixed 2046 - WRTKit Help. Renamed package for consistency.
tasneems@symbian.org
parents:
diff
changeset
|
95 |
|
7848c135d915
Fixed 2046 - WRTKit Help. Renamed package for consistency.
tasneems@symbian.org
parents:
diff
changeset
|
96 |
<p> |
7848c135d915
Fixed 2046 - WRTKit Help. Renamed package for consistency.
tasneems@symbian.org
parents:
diff
changeset
|
97 |
|
7848c135d915
Fixed 2046 - WRTKit Help. Renamed package for consistency.
tasneems@symbian.org
parents:
diff
changeset
|
98 |
The code below demonstrates how to add and remove an event listener |
7848c135d915
Fixed 2046 - WRTKit Help. Renamed package for consistency.
tasneems@symbian.org
parents:
diff
changeset
|
99 |
to/from a control. The example assumes that the control has already |
7848c135d915
Fixed 2046 - WRTKit Help. Renamed package for consistency.
tasneems@symbian.org
parents:
diff
changeset
|
100 |
been created and that the ctrl variable refers to it. |
7848c135d915
Fixed 2046 - WRTKit Help. Renamed package for consistency.
tasneems@symbian.org
parents:
diff
changeset
|
101 |
</p> |
7848c135d915
Fixed 2046 - WRTKit Help. Renamed package for consistency.
tasneems@symbian.org
parents:
diff
changeset
|
102 |
|
7848c135d915
Fixed 2046 - WRTKit Help. Renamed package for consistency.
tasneems@symbian.org
parents:
diff
changeset
|
103 |
<pre> |
7848c135d915
Fixed 2046 - WRTKit Help. Renamed package for consistency.
tasneems@symbian.org
parents:
diff
changeset
|
104 |
|
7848c135d915
Fixed 2046 - WRTKit Help. Renamed package for consistency.
tasneems@symbian.org
parents:
diff
changeset
|
105 |
// add listener to ctrl |
7848c135d915
Fixed 2046 - WRTKit Help. Renamed package for consistency.
tasneems@symbian.org
parents:
diff
changeset
|
106 |
// function to add is handleEvent() for event type "ActionPerformed" |
7848c135d915
Fixed 2046 - WRTKit Help. Renamed package for consistency.
tasneems@symbian.org
parents:
diff
changeset
|
107 |
ctrl.addEventListener("ActionPerformed", handleEvent); |
7848c135d915
Fixed 2046 - WRTKit Help. Renamed package for consistency.
tasneems@symbian.org
parents:
diff
changeset
|
108 |
|
7848c135d915
Fixed 2046 - WRTKit Help. Renamed package for consistency.
tasneems@symbian.org
parents:
diff
changeset
|
109 |
// remove listener from ctrl |
7848c135d915
Fixed 2046 - WRTKit Help. Renamed package for consistency.
tasneems@symbian.org
parents:
diff
changeset
|
110 |
// function to remove is handleEvent() for event type "ActionPerformed" |
7848c135d915
Fixed 2046 - WRTKit Help. Renamed package for consistency.
tasneems@symbian.org
parents:
diff
changeset
|
111 |
ctrl.removeEventListener("ActionPerformed", handleEvent); |
7848c135d915
Fixed 2046 - WRTKit Help. Renamed package for consistency.
tasneems@symbian.org
parents:
diff
changeset
|
112 |
</pre> |
7848c135d915
Fixed 2046 - WRTKit Help. Renamed package for consistency.
tasneems@symbian.org
parents:
diff
changeset
|
113 |
|
7848c135d915
Fixed 2046 - WRTKit Help. Renamed package for consistency.
tasneems@symbian.org
parents:
diff
changeset
|
114 |
</div> |
7848c135d915
Fixed 2046 - WRTKit Help. Renamed package for consistency.
tasneems@symbian.org
parents:
diff
changeset
|
115 |
|
7848c135d915
Fixed 2046 - WRTKit Help. Renamed package for consistency.
tasneems@symbian.org
parents:
diff
changeset
|
116 |
<div> |
7848c135d915
Fixed 2046 - WRTKit Help. Renamed package for consistency.
tasneems@symbian.org
parents:
diff
changeset
|
117 |
<div class="familylinks"> |
7848c135d915
Fixed 2046 - WRTKit Help. Renamed package for consistency.
tasneems@symbian.org
parents:
diff
changeset
|
118 |
<div class="parentlink"><strong>Parent topic:</strong> <a href="WRTKit_Common_WRTKit_tasks-GUID-24870895-4449-4307-9a54-7c90f7b3905e.html">Common WRTKit tasks</a></div> |
7848c135d915
Fixed 2046 - WRTKit Help. Renamed package for consistency.
tasneems@symbian.org
parents:
diff
changeset
|
119 |
</div> |
7848c135d915
Fixed 2046 - WRTKit Help. Renamed package for consistency.
tasneems@symbian.org
parents:
diff
changeset
|
120 |
</div> |
7848c135d915
Fixed 2046 - WRTKit Help. Renamed package for consistency.
tasneems@symbian.org
parents:
diff
changeset
|
121 |
|
7848c135d915
Fixed 2046 - WRTKit Help. Renamed package for consistency.
tasneems@symbian.org
parents:
diff
changeset
|
122 |
</body> |
7848c135d915
Fixed 2046 - WRTKit Help. Renamed package for consistency.
tasneems@symbian.org
parents:
diff
changeset
|
123 |
</html> |