Dart API Referencedart:htmlEvent

Event class

This chapter describes the DOM Event Model. The Event interface itself is described, as well as the interfaces for event registration on nodes in the DOM, and event listeners, and several longer examples that show how the various event interfaces relate to one another.

There is an excellent diagram that clearly explains the three phases of event flow through the DOM in the DOM Level 3 Events draft.

@DomName('Event')
class Event native "Event" {
 // In JS, canBubble and cancelable are technically required parameters to
 // init*Event. In practice, though, if they aren't provided they simply
 // default to false (since that's Boolean(undefined)).
 //
 // Contrary to JS, we default canBubble and cancelable to true, since that's
 // what people want most of the time anyway.
 factory Event(String type,
     {bool canBubble: true, bool cancelable: true}) {
   return new Event.eventType('Event', type, canBubble: canBubble,
       cancelable: cancelable);
 }

 /**
  * Creates a new Event object of the specified type.
  *
  * This is analogous to document.createEvent.
  * Normally events should be created via their constructors, if available.
  *
  *     var e = new Event.type('MouseEvent', 'mousedown', true, true);
  */
 factory Event.eventType(String type, String name, {bool canBubble: true,
     bool cancelable: true}) {
   final Event e = document.$dom_createEvent(type);
   e.$dom_initEvent(name, canBubble, cancelable);
   return e;
 }

 @DomName('Event.AT_TARGET')
 @DocsEditable
 static const int AT_TARGET = 2;

 @DomName('Event.BLUR')
 @DocsEditable
 static const int BLUR = 8192;

 @DomName('Event.BUBBLING_PHASE')
 @DocsEditable
 static const int BUBBLING_PHASE = 3;

 @DomName('Event.CAPTURING_PHASE')
 @DocsEditable
 static const int CAPTURING_PHASE = 1;

 @DomName('Event.CHANGE')
 @DocsEditable
 static const int CHANGE = 32768;

 @DomName('Event.CLICK')
 @DocsEditable
 static const int CLICK = 64;

 @DomName('Event.DBLCLICK')
 @DocsEditable
 static const int DBLCLICK = 128;

 @DomName('Event.DRAGDROP')
 @DocsEditable
 static const int DRAGDROP = 2048;

 @DomName('Event.FOCUS')
 @DocsEditable
 static const int FOCUS = 4096;

 @DomName('Event.KEYDOWN')
 @DocsEditable
 static const int KEYDOWN = 256;

 @DomName('Event.KEYPRESS')
 @DocsEditable
 static const int KEYPRESS = 1024;

 @DomName('Event.KEYUP')
 @DocsEditable
 static const int KEYUP = 512;

 @DomName('Event.MOUSEDOWN')
 @DocsEditable
 static const int MOUSEDOWN = 1;

 @DomName('Event.MOUSEDRAG')
 @DocsEditable
 static const int MOUSEDRAG = 32;

 @DomName('Event.MOUSEMOVE')
 @DocsEditable
 static const int MOUSEMOVE = 16;

 @DomName('Event.MOUSEOUT')
 @DocsEditable
 static const int MOUSEOUT = 8;

 @DomName('Event.MOUSEOVER')
 @DocsEditable
 static const int MOUSEOVER = 4;

 @DomName('Event.MOUSEUP')
 @DocsEditable
 static const int MOUSEUP = 2;

 @DomName('Event.NONE')
 @DocsEditable
 static const int NONE = 0;

 @DomName('Event.SELECT')
 @DocsEditable
 static const int SELECT = 16384;

 @DomName('Event.bubbles')
 @DocsEditable
 final bool bubbles;

 @DomName('Event.cancelBubble')
 @DocsEditable
 bool cancelBubble;

 @DomName('Event.cancelable')
 @DocsEditable
 final bool cancelable;

 @DomName('Event.clipboardData')
 @DocsEditable
 @SupportedBrowser(SupportedBrowser.CHROME)
 @SupportedBrowser(SupportedBrowser.SAFARI)
 @Experimental
 final DataTransfer clipboardData;

 EventTarget get currentTarget => _convertNativeToDart_EventTarget(this._get_currentTarget);
 @JSName('currentTarget')
 @DomName('Event.currentTarget')
 @DocsEditable
 @Creates('Null')
 @Returns('EventTarget|=Object')
 final dynamic _get_currentTarget;

 @DomName('Event.defaultPrevented')
 @DocsEditable
 final bool defaultPrevented;

 @DomName('Event.eventPhase')
 @DocsEditable
 final int eventPhase;

 EventTarget get target => _convertNativeToDart_EventTarget(this._get_target);
 @JSName('target')
 @DomName('Event.target')
 @DocsEditable
 @Creates('Node')
 @Returns('EventTarget|=Object')
 final dynamic _get_target;

 @DomName('Event.timeStamp')
 @DocsEditable
 final int timeStamp;

 @DomName('Event.type')
 @DocsEditable
 final String type;

 @JSName('initEvent')
 @DomName('Event.initEvent')
 @DocsEditable
 void $dom_initEvent(String eventTypeArg, bool canBubbleArg, bool cancelableArg) native;

 @DomName('Event.preventDefault')
 @DocsEditable
 void preventDefault() native;

 @DomName('Event.stopImmediatePropagation')
 @DocsEditable
 void stopImmediatePropagation() native;

 @DomName('Event.stopPropagation')
 @DocsEditable
 void stopPropagation() native;

}

Extends

Interceptor > Event

Subclasses

AnimationEvent, AudioProcessingEvent, AutocompleteErrorEvent, BeforeLoadEvent, BeforeUnloadEvent, CloseEvent, ContextEvent, CssFontFaceLoadEvent, CustomEvent, DeviceMotionEvent, DeviceOrientationEvent, ErrorEvent, HashChangeEvent, MediaKeyEvent, MediaStreamEvent, MediaStreamTrackEvent, MessageEvent, MidiConnectionEvent, MidiMessageEvent, MutationEvent, OfflineAudioCompletionEvent, OverflowEvent, PageTransitionEvent, PopStateEvent, ProgressEvent, RtcDataChannelEvent, RtcDtmfToneChangeEvent, RtcIceCandidateEvent, SecurityPolicyViolationEvent, SpeechInputEvent, SpeechRecognitionError, SpeechRecognitionEvent, SpeechSynthesisEvent, StorageEvent, TrackEvent, TransitionEvent, UIEvent, VersionChangeEvent

Static Properties

const int AT_TARGET #

static const int AT_TARGET = 2

const int BLUR #

static const int BLUR = 8192

const int BUBBLING_PHASE #

static const int BUBBLING_PHASE = 3

const int CAPTURING_PHASE #

static const int CAPTURING_PHASE = 1

const int CHANGE #

static const int CHANGE = 32768

const int CLICK #

static const int CLICK = 64

const int DBLCLICK #

static const int DBLCLICK = 128

const int DRAGDROP #

static const int DRAGDROP = 2048

const int FOCUS #

static const int FOCUS = 4096

const int KEYDOWN #

static const int KEYDOWN = 256

const int KEYPRESS #

static const int KEYPRESS = 1024

const int KEYUP #

static const int KEYUP = 512

const int MOUSEDOWN #

static const int MOUSEDOWN = 1

const int MOUSEDRAG #

static const int MOUSEDRAG = 32

const int MOUSEMOVE #

static const int MOUSEMOVE = 16

const int MOUSEOUT #

static const int MOUSEOUT = 8

const int MOUSEOVER #

static const int MOUSEOVER = 4

const int MOUSEUP #

static const int MOUSEUP = 2

const int NONE #

static const int NONE = 0

const int SELECT #

static const int SELECT = 16384

Constructors

factory Event(String type, {bool canBubble: true, bool cancelable: true}) #

factory Event(String type,
   {bool canBubble: true, bool cancelable: true}) {
 return new Event.eventType('Event', type, canBubble: canBubble,
     cancelable: cancelable);
}

factory Event.eventType(String type, String name, {bool canBubble: true, bool cancelable: true}) #

Creates a new Event object of the specified type.

This is analogous to document.createEvent. Normally events should be created via their constructors, if available.

var e = new Event.type('MouseEvent', 'mousedown', true, true);
factory Event.eventType(String type, String name, {bool canBubble: true,
   bool cancelable: true}) {
 final Event e = document.$dom_createEvent(type);
 e.$dom_initEvent(name, canBubble, cancelable);
 return e;
}

Properties

final bool bubbles #

A boolean indicating whether the event bubbles up through the DOM or not.
final bool bubbles

final bool cancelable #

A boolean indicating whether the event is cancelable.
final bool cancelable

bool cancelBubble #

A boolean indicating whether the bubbling of the event has been canceled or not.
bool cancelBubble

final DataTransfer clipboardData #

final DataTransfer clipboardData

final EventTarget currentTarget #

EventTarget get currentTarget => _convertNativeToDart_EventTarget(this._get_currentTarget);

final bool defaultPrevented #

Indicates whether or not event.preventDefault() has been called on the event.
final bool defaultPrevented

final int eventPhase #

Indicates which phase of the event flow is being processed.
final int eventPhase

final int hashCode #

inherited from Interceptor

Get a hash code for this object.

All objects have hash codes. Hash codes are guaranteed to be the same for objects that are equal when compared using the equality operator ==. Other than that there are no guarantees about the hash codes. They will not be consistent between runs and there are no distribution guarantees.

If a subclass overrides hashCode it should override the equality operator as well to maintain consistency.

docs inherited from Object
int get hashCode => Primitives.objectHashCode(this);

final Type runtimeType #

inherited from Interceptor

A representation of the runtime type of the object.

docs inherited from Object
Type get runtimeType => getRuntimeType(this);

final EventTarget target #

EventTarget get target => _convertNativeToDart_EventTarget(this._get_target);

final int timeStamp #

The time that the event was created.
final int timeStamp

final String type #

The name of the event (case-insensitive).
final String type

Operators

bool operator ==(other) #

inherited from Interceptor

The equality operator.

The default behavior for all Objects is to return true if and only if this and other are the same object.

If a subclass overrides the equality operator it should override the hashCode method as well to maintain consistency.

docs inherited from Object
bool operator ==(other) => identical(this, other);

Methods

dynamic noSuchMethod(Invocation invocation) #

inherited from Interceptor

noSuchMethod is invoked when users invoke a non-existant method on an object. The name of the method and the arguments of the invocation are passed to noSuchMethod in an Invocation. If noSuchMethod returns a value, that value becomes the result of the original invocation.

The default behavior of noSuchMethod is to throw a noSuchMethodError.

docs inherited from Object
dynamic noSuchMethod(Invocation invocation) {
 throw new NoSuchMethodError(
     this,
     _symbolToString(invocation.memberName),
     invocation.positionalArguments,
     _symbolMapToStringMap(invocation.namedArguments));
}

void preventDefault() #

Cancels the event (if it is cancelable).
@DomName('Event.preventDefault')
@DocsEditable
void preventDefault() native;

void stopImmediatePropagation() #

For this particular event, no other listener will be called. Neither those attached on the same element, nor those attached on elements which will be traversed later (in capture phase, for instance)
@DomName('Event.stopImmediatePropagation')
@DocsEditable
void stopImmediatePropagation() native;

void stopPropagation() #

Stops the propagation of events further along in the DOM.
@DomName('Event.stopPropagation')
@DocsEditable
void stopPropagation() native;

String toString() #

inherited from Interceptor

Returns a string representation of this object.

docs inherited from Object
String toString() => Primitives.objectToString(this);

void $dom_initEvent(String eventTypeArg, bool canBubbleArg, bool cancelableArg) #

Initializes the value of an Event created through the DocumentEvent interface.
@JSName('initEvent')
@DomName('Event.initEvent')
@DocsEditable
void $dom_initEvent(String eventTypeArg, bool canBubbleArg, bool cancelableArg) native;

This page includes content from the Mozilla Foundation that is graciously licensed under a Creative Commons: Attribution-Sharealike license. Mozilla has no other association with Dart or dartlang.org. We encourage you to improve the web by contributing to The Mozilla Developer Network.