
CHAPTER 4: User-Interface Tools Defining behavior with event callbacks and listeners 82
Simulating user events
You can simulate user actions by sending an event notification directly to a window or control with the
notify method. A script can use this method to generate events in the controls of a window, as if a user
was clicking buttons, entering text, or moving the window. If you have defined an event-handler callback
for the element, the
notify method invokes it.
The
notify method takes an optional argument that specifies which event it should simulate. If a control
can generate only one kind of event, notification generates that event by default.
The following controls generate the
onClick event:
Button
Checkbox
IconButton
RadioButton
The following controls generate the onChange event:
DropDownList
EditText
ListBox
Scrollbar
Slider
TreeView
The following controls generate the onChanging event:
EditText
Scrollbar
Slider
In the ListBox, double-clicking an item generates the onDoubleClick event.
In
RadioButton and Checkbox controls, the boolean value property automatically changes when the
user clicks the control. If you use
notify() to simulate a click, the value changes just as if the user had
clicked. For example, if the
value of a checkbox hasBtnsCb is true, this code changes the value to false:
if (dlg.hasBtnsCb.value == true) dlg.hasBtnsCb.notify();
// dlg.hasBtnsCb.value is now false
Registering event listeners for windows or controls
Another way to define the behavior of your windows and controls is register a handler function that
responds to a specific type of event in that window or control. This technique allows you to respond to the
cascading of an event through a hierarchy of containers and controls.
Use
windowObj.addEventListener() or controlObj.addEventListener() to register a handler. The function
you register receives an event object (from the UIEvent base class
) that encapsulates the event
information. As an event cascades down through a hierarchy and back up through the hierarchy, your
handler can respond at any level, or use the
UIEvent object’s stopPropagation() method to stop the event
propagation at some level.
You can register:
X The name of a handler function defined in the extension that takes one argument, the event object.
For example:
Comentários a estes Manuais