event-handling
Event handling
Section titled “Event handling”Several helper classes provide low-level event-handling capabilities.
Event objects are normally created by ScriptUI and passed to your event handler. However, you can simulate a user action by constructing an event object using ScriptUI.events.createEvent(), and sending it to a target object’s controlobj-dispatchEvent function.
A helper object, Keyboard state object, provides global access to the keyboard state during function execution, outside the event-handling framework.
UIEvent Base Class
Section titled “UIEvent Base Class”Encapsulates input event information for an event that propagates through a container and control hierarchy. This is a base class for the more specialized KeyboardEvent object and MouseEvent object.
UIEvent Object Attributes
Section titled “UIEvent Object Attributes”Both keyboard and mouse events have these properties.
bubbles
Section titled “bubbles”eventObj.bubbles
Description
Section titled “Description”When true
, the event supports the bubbling phase.
Boolean
cancelable
Section titled “cancelable”eventObj.cancelable
Description
Section titled “Description”When true
, the handler can call this object’s preventDefault() method to cancel the default action of the event.
Boolean
currentTarget
Section titled “currentTarget”eventObj.currentTarget
Description
Section titled “Description”The element object where the currently executing handler was registered.
This could be an ancestor of the target object, if the handler is invoked during the capture or bubbling phase.
Object
eventPhase
Section titled “eventPhase”eventObj.eventPhase
Description
Section titled “Description”Current event propagation phase. One of these constants:
Event.NOT_DISPATCHING
Event.CAPTURING_PHASE
Event.AT_TARGET
Event.BUBBLING_PHASE
Number
target
Section titled “target”eventObj.target
Description
Section titled “Description”The element object where the event occurred.
Object
timeStamp
Section titled “timeStamp”eventObj.timeStamp
Description
Section titled “Description”Time the event was initiated. A JavaScript Date object.
Object
eventObj.type
Description
Section titled “Description”The name of the event that occurred. Predefined events types are:
"blur"
"change"
"changing"
"enterKey"
"focus"
"move"
"moving"
"resize"
"resizing"
"show"
Additional type names apply specifically to keyboard and mouse events.
String
eventObj.view
Description
Section titled “Description”The container or control object that dispatched the event.
UIEvent Object Methods
Section titled “UIEvent Object Methods”initUIEvent()
Section titled “initUIEvent()”eventObj.initUIEvent(eventName, bubble, isCancelable, view, detail)
Description
Section titled “Description”Modifies an event before it is dispatched to its targets. Takes effect only if UIEvent.eventPhase is Event.NOT_DISPATCHING
.
Ignored at all other phases.
Parameters
Section titled “Parameters”Parameter | Type | Description |
---|---|---|
eventName | String | The event name string. |
bubble | Boolean | When true , the event should be triggered in ancestors of the target object during the bubbling phase. |
isCancelable | Boolean | When true , the event can be cancelled. |
view | Container or Control object | The container or control object that dispatched the event. |
detail | Any | Details of the event, which vary according to the event type. The value is 1 or 2 for the click event, indicating a single or double click. |
Returns
Section titled “Returns”Nothing
preventDefault()
Section titled “preventDefault()”eventObj.preventDefault()
Description
Section titled “Description”Cancels the default action of this event, if this event is cancelable (that is, cancelable is true).
For example, the default click action of an OK button is to close the containing dialog; this call prevents that behavior.
Returns
Section titled “Returns”Nothing
stopPropagation()
Section titled “stopPropagation()”eventObj.stopPropagation()
Description
Section titled “Description”Stops event propagation (bubbling and capturing) after executing the handler or handlers at the current target.
Returns
Section titled “Returns”Nothing
KeyboardEvent Object
Section titled “KeyboardEvent Object”This type of object is passed to your registered event handler when a keyboard-input event occurs. The properties reflect the keypress and key modifier state at the time the keyboard event was generated.
All properties are read only.
KeyboardEvent Object Methods
Section titled “KeyboardEvent Object Methods”In addition to the properties defined for UIEvent base class, a keyboard event has these properties.
altKey
Section titled “altKey”eventObj.altKey
Description
Section titled “Description”When true
, the ALT
key was active.
Value is undefined
if the keyIdentifier
is for a modifier key.
Boolean
ctrlKey
Section titled “ctrlKey”eventObj.ctrlKey
Description
Section titled “Description”When true
, the CTRL
key was active.
Value is undefined
if the keyIdentifier
is for a modifier key.
Boolean
metaKey
Section titled “metaKey”eventObj.metaKey
Description
Section titled “Description”When true
, the META
or COMMAND
key was active.
Value is undefined
if the keyIdentifier
is for a modifier key.
Boolean
shiftKey
Section titled “shiftKey”eventObj.shiftKey
Description
Section titled “Description”When true
, the SHIFT
key was active.
Value is undefined
if the keyIdentifier
is for a modifier key.
Boolean
keyIdentifier
Section titled “keyIdentifier”eventObj.keyIdentifier
Description
Section titled “Description”The key whose keypress generated the event, as a W3C identifier contained in a string; for example, "U+0044"
. See W3 Keyset Article.
String
keyLocation
Section titled “keyLocation”eventObj.keyLocation
Description
Section titled “Description”A constant that identifies where on the keyboard the keypress occurred.
One of:
DOM_KEY_LOCATION_STANDARD
DOM_KEY_LOCATION_LEFT
DOM_KEY_LOCATION_RIGHT
DOM_KEY_LOCATION_NUMPAD
Number
keyName
Section titled “keyName”eventObj.keyName
Description
Section titled “Description”The key whose keypress generated the event, as a simple key name; for example "A"
.
String
eventObj.type
Description
Section titled “Description”The name of the event that occurred. Key events types are:
keyup
keydown
String
KeyboardEvent Object Methods
Section titled “KeyboardEvent Object Methods”In addition to the functions defined for UIEvent base class, a keyboard event has these functions.
getModifierState()
Section titled “getModifierState()”eventObj.getModifierState(keyIdentifier)
Description
Section titled “Description”Get the current modifier keys being used in this event.
Parameters
Section titled “Parameters”Parameter | Type | Description |
---|---|---|
keyIdentifier | String | A string containing a modifier key identifier, one of: |
- Alt | ||
- CapsLock | ||
- Control | ||
- Meta | ||
- NumLock | ||
- Scroll | ||
- Shift |
Returns
Section titled “Returns”Boolean. true
if the given modifier was active when the event occurred, false
otherwise.
initKeyboardEvent()
Section titled “initKeyboardEvent()”eventObj.initKeyboardEvent(eventName, bubble, isCancelable, view, keyID, keyLocation, modifiersList)
Description
Section titled “Description”Reinitializes the object, allowing you to change the event properties after construction. Arguments set the corresponding properties.
Parameters
Section titled “Parameters”Parameter | Type | Description |
---|---|---|
eventName | String | The event name string. |
bubble | Boolean | When true , the event should be triggered in ancestors of the target object during the bubbling phase. |
isCancelable | Boolean | When true , the event can be cancelled. |
view | Container or Control object | The container or control object that dispatched the event. |
keyID | String | Sets the keyIdentifier value. |
keyLocation | String | Sets the keyLocation . value. |
modifiersList | String | A whitespace-separated string of modifier key names, such as “Control Alt”. |
Returns
Section titled “Returns”Nothing
MouseEvent Object
Section titled “MouseEvent Object”This type of object is passed to your registered event handler when a mouse-input event occurs. The properties reflect the button and modifier-key state and pointer position at the time the event was generated.
In the case of nested elements, mouse event types are always targeted at the most deeply nested element. Ancestors of the targeted element can use bubbling to obtain notification of mouse events which occur within its descendent elements.
MouseEvent Object Attributes
Section titled “MouseEvent Object Attributes”In addition to the properties defined for UIEvent base class, a mouse event has these properties.
All properties are read only.
altKey
Section titled “altKey”eventObj.altKey
Description
Section titled “Description”When true
, the ALT
key was active.
Value is undefined
if the keyIdentifier
is for a modifier key.
Boolean
button
Section titled “button”eventObj.button
Description
Section titled “Description”Which mouse button changed state.
One of:
0
: The left button of a two- or three-button mouse or the one button on a one-button mouse, used to activate a UI button or select text.1
: The middle button of a three-button mouse, or the mouse wheel.2
: The right button, used to display a context menu, if present.
Some mice may provide or simulate more buttons, and values higher than 2
represent such buttons.
Number
clientX and clientY
Section titled “clientX and clientY”eventObj.clientX
eventObj.clientY
Description
Section titled “Description”The horizontal and vertical coordinates at which the event occurred relative to the target object. The origin is the top left of the control or window, inside any border decorations.
Number
ctrlKey
Section titled “ctrlKey”eventObj.ctrlKey
Description
Section titled “Description”When true
, the CTRL
key was active.
Value is undefined
if the keyIdentifier
is for a modifier key.
Boolean
detail
Section titled “detail”eventObj.detail
Description
Section titled “Description”Details of the event, which vary according to the event type.
For the click
, mousedown
, and mouseup
events, the value is 1
for a single click, or 2
for a double click.
Number
metaKey
Section titled “metaKey”eventObj.metaKey
Description
Section titled “Description”When true
, the META
or COMMAND
key was active.
Value is undefined
if the keyIdentifier
is for a modifier key.
Boolean
relatedTarget
Section titled “relatedTarget”eventObj.relatedTarget
Description
Section titled “Description”- For a
mouseover
event, the UI element the pointer is leaving, if any. - For a
mouseout
event, the UI element the pointer is entering, if any.
Otherwise undefined
.
Object
screenX and screenY
Section titled “screenX and screenY”eventObj.screenX
eventObj.screenY
Description
Section titled “Description”The horizontal and vertical coordinates at which the event occurred relative to the screen.
Number
shiftKey
Section titled “shiftKey”eventObj.shiftKey
Description
Section titled “Description”When true
, the SHIFT
key was active.
Value is undefined
if the keyIdentifier
is for a modifier key.
Boolean
eventObj.type
Description
Section titled “Description”The name of the event that occurred. Mouse events types are:
"mousedown"
"mouseup"
"mousemove"
"mouseover"
"mouseout"
"click"
(detail = 1 for single, 2 for double)
The sequence of click events is: mousedown
, mouseup
, click
.
String
MouseEvent Object Methods
Section titled “MouseEvent Object Methods”In addition to the functions defined for UIEvent base class, a mouse event has these functions.
getModifierState()
Section titled “getModifierState()”eventObj.getModifierState(keyIdentifier)
Description
Section titled “Description”Get the current modifier keys being used in this event.
Parameters
Section titled “Parameters”Parameter | Type | Description |
---|---|---|
keyIdentifier | String | A string containing a modifier key identifier, one of: |
- "Alt" | ||
- "CapsLock" | ||
- "Control" | ||
- "Meta" | ||
- "NumLock" | ||
- "Scroll" | ||
- "Shift" |
Returns
Section titled “Returns”Boolean. true
if the given modifier was active when the event occurred, false
otherwise.
initMouseEvent()
Section titled “initMouseEvent()”eventObj.initMouseEvent( eventName, bubble, isCancelable, view, detail, screenX, screenY, clientX, clientY, ctrlKey, altKey, shiftKey, metaKey, button, relatedTarge)
Description
Section titled “Description”Reinitializes the object, allowing you to change the event properties after construction. Arguments set the corresponding properties.
Parameters
Section titled “Parameters”Parameter | Type | Description |
---|---|---|
eventName | String | The event name string. |
bubble | Boolean | When true , the event should be triggered in ancestors of the target object during the bubbling phase. |
isCancelable | Boolean | When true , the event can be cancelled. |
view | Container or Control object | The container or control object that dispatched the event. |
detail | Number | Sets the single-double click value for the click event. |
screenX, screenY | Number | Sets the event coordinates relative to the screen. |
clientX, clientY | Number | Sets the event coordinates relative to the target object. The origin is the top left of the control or window, inside any border decorations. |
ctrlKey, altKey, metaKey | Boolean | Sets the modifier key states. |
button | Number | Sets the mouse button. |
relatedTarget | Object | Optional. Sets the related target, if any, for a mouseover or mouseout event. |
Returns
Section titled “Returns”Nothing