KeyEvent 构造函数
程序化创建一个新的 KeyEvent(和 KeyboardEvent)。
实现
factory KeyEvent(String type,
{Window? view,
bool canBubble = true,
bool cancelable = true,
int keyCode = 0,
int charCode = 0,
int location = 1,
bool ctrlKey = false,
bool altKey = false,
bool shiftKey = false,
bool metaKey = false,
EventTarget? currentTarget}) {
if (view == null) {
view = window;
}
KeyboardEvent eventObj;
// Currently this works on everything but Safari. Safari throws an
// "Attempting to change access mechanism for an unconfigurable property"
// TypeError when trying to do the Object.defineProperty hack, so we avoid
// this branch if possible.
// Also, if we want this branch to work in FF, we also need to modify
// _initKeyboardEvent to also take charCode and keyCode values to
// initialize initKeyEvent.
eventObj = new Event.eventType('KeyboardEvent', type,
canBubble: canBubble, cancelable: cancelable) as KeyboardEvent;
// Chromium Hack
JS(
'void',
"Object.defineProperty(#, 'keyCode', {"
" get : function() { return this.keyCodeVal; } })",
eventObj);
JS(
'void',
"Object.defineProperty(#, 'which', {"
" get : function() { return this.keyCodeVal; } })",
eventObj);
JS(
'void',
"Object.defineProperty(#, 'charCode', {"
" get : function() { return this.charCodeVal; } })",
eventObj);
var keyIdentifier = _convertToHexString(charCode, keyCode);
eventObj._initKeyboardEvent(type, canBubble, cancelable, view,
keyIdentifier, location, ctrlKey, altKey, shiftKey, metaKey);
JS('void', '#.keyCodeVal = #', eventObj, keyCode);
JS('void', '#.charCodeVal = #', eventObj, charCode);
// Tell dart2js that it smells like a KeyboardEvent!
setDispatchProperty(eventObj, _keyboardEventDispatchRecord);
var keyEvent = new KeyEvent.wrap(eventObj);
if (keyEvent._currentTarget == null) {
keyEvent._currentTarget = currentTarget == null ? window : currentTarget;
}
return keyEvent;
}