|
| 1 | +const initKeyboardEventType = (function (event) { |
| 2 | + try { |
| 3 | + event.initKeyboardEvent( |
| 4 | + "keyup" // in DOMString typeArg |
| 5 | + , false // in boolean canBubbleArg |
| 6 | + , false // in boolean cancelableArg |
| 7 | + , window // in views::AbstractView viewArg |
| 8 | + , "+" // [test]in DOMString keyIdentifierArg | webkit event.keyIdentifier | IE9 event.key |
| 9 | + , 3 // [test]in unsigned long keyLocationArg | webkit event.keyIdentifier | IE9 event.location |
| 10 | + , true // [test]in boolean ctrlKeyArg | webkit event.shiftKey | old webkit event.ctrlKey | IE9 event.modifiersList |
| 11 | + , false // [test]shift | alt |
| 12 | + , true // [test]shift | alt |
| 13 | + , false // meta |
| 14 | + , false // altGraphKey |
| 15 | + ); |
| 16 | + |
| 17 | + return ((event["keyIdentifier"] || event["key"]) == "+" && (event["location"]) || event["keyLocation"] == 3) && ( |
| 18 | + event.ctrlKey ? |
| 19 | + event.altKey ? // webkit |
| 20 | + 1 : |
| 21 | + 3 : |
| 22 | + event.shiftKey ? |
| 23 | + 2 : // webkit |
| 24 | + 4 // IE9 |
| 25 | + ) || 9; // FireFox|w3c |
| 26 | + } catch (error) { |
| 27 | + initKeyboardEventType = 0 |
| 28 | + } |
| 29 | +})(document.createEvent("KeyboardEvent")); |
| 30 | + |
| 31 | +const keyboardEventPropertiesDictionary = { |
| 32 | + "char": "", |
| 33 | + "key": "", |
| 34 | + "location": 0, |
| 35 | + "ctrlKey": false, |
| 36 | + "shiftKey": false, |
| 37 | + "altKey": false, |
| 38 | + "metaKey": false, |
| 39 | + "repeat": false, |
| 40 | + "locale": "", |
| 41 | + |
| 42 | + "detail": 0, |
| 43 | + "bubbles": false, |
| 44 | + "cancelable": false, |
| 45 | + |
| 46 | + //legacy properties |
| 47 | + "keyCode": 0, |
| 48 | + "charCode": 0, |
| 49 | + "which": 0 |
| 50 | +}; |
| 51 | + |
| 52 | +const own = Function.prototype.call.bind(Object.prototype.hasOwnProperty); |
| 53 | + |
| 54 | +const ObjectDefineProperty = Object.defineProperty || function (obj, prop, val) { |
| 55 | + if ("value" in val) { |
| 56 | + obj[prop] = val["value"]; |
| 57 | + } |
| 58 | +}; |
| 59 | + |
| 60 | +export default function KeyboardEvent(type, dict) { |
| 61 | + let event; |
| 62 | + if (initKeyboardEventType) { |
| 63 | + event = document.createEvent("KeyboardEvent"); |
| 64 | + } else { |
| 65 | + event = document.createEvent("Event"); |
| 66 | + } |
| 67 | + |
| 68 | + let propName; |
| 69 | + let localDict = {}; |
| 70 | + |
| 71 | + for (propName in keyboardEventPropertiesDictionary) |
| 72 | + if (own(keyboardEventPropertiesDictionary, propName)) { |
| 73 | + localDict[propName] = (own(dict, propName) && dict || keyboardEventPropertiesDictionary)[propName]; |
| 74 | + } |
| 75 | + |
| 76 | + const ctrlKey = localDict["ctrlKey"]; |
| 77 | + const shiftKey = localDict["shiftKey"]; |
| 78 | + const altKey = localDict["altKey"]; |
| 79 | + const metaKey = localDict["metaKey"]; |
| 80 | + const altGraphKey = localDict["altGraphKey"]; |
| 81 | + |
| 82 | + const modifiersListArg = initKeyboardEventType > 3 ? ( |
| 83 | + (ctrlKey ? "Control" : "") + |
| 84 | + (shiftKey ? " Shift" : "") + |
| 85 | + (altKey ? " Alt" : "") + |
| 86 | + (metaKey ? " Meta" : "") + |
| 87 | + (altGraphKey ? " AltGraph" : "") |
| 88 | + ).trim() : null |
| 89 | + |
| 90 | + const key = localDict["key"] + ""; |
| 91 | + const char = localDict["char"] + ""; |
| 92 | + const location = localDict["location"]; |
| 93 | + const keyCode = localDict["keyCode"] || (localDict["keyCode"] = key && key.charCodeAt(0) || 0); |
| 94 | + const charCode = localDict["charCode"] || (localDict["charCode"] = char && char.charCodeAt(0) || 0); |
| 95 | + const bubbles = localDict["bubbles"]; |
| 96 | + const cancelable = localDict["cancelable"]; |
| 97 | + const repeat = localDict["repeat"]; |
| 98 | + const locale = localDict["locale"]; |
| 99 | + const view = window; |
| 100 | + |
| 101 | + localDict["which"] || (localDict["which"] = localDict["keyCode"]); |
| 102 | + |
| 103 | + if ("initKeyEvent" in event) { //FF |
| 104 | + //https://developer.mozilla.org/en/DOM/event.initKeyEvent |
| 105 | + event.initKeyEvent(type, bubbles, cancelable, view, ctrlKey, altKey, shiftKey, metaKey, keyCode, charCode); |
| 106 | + } else if (initKeyboardEventType && "initKeyboardEvent" in event) { //https://developer.mozilla.org/en/DOM/KeyboardEvent#initKeyboardEvent() |
| 107 | + if (initKeyboardEventType == 1) { // webkit |
| 108 | + //http://stackoverflow.com/a/8490774/1437207 |
| 109 | + //https://bugs.webkit.org/show_bug.cgi?id=13368 |
| 110 | + event.initKeyboardEvent(type, bubbles, cancelable, view, key, location, ctrlKey, shiftKey, altKey, metaKey, altGraphKey); |
| 111 | + } else if (initKeyboardEventType == 2) { // old webkit |
| 112 | + //http://code.google.com/p/chromium/issues/detail?id=52408 |
| 113 | + event.initKeyboardEvent(type, bubbles, cancelable, view, ctrlKey, altKey, shiftKey, metaKey, keyCode, charCode); |
| 114 | + } else if (initKeyboardEventType == 3) { // webkit |
| 115 | + event.initKeyboardEvent(type, bubbles, cancelable, view, key, location, ctrlKey, altKey, shiftKey, metaKey, altGraphKey); |
| 116 | + } else if (initKeyboardEventType == 4) { // IE9 |
| 117 | + //http://msdn.microsoft.com/en-us/library/ie/ff975297(v=vs.85).aspx |
| 118 | + event.initKeyboardEvent(type, bubbles, cancelable, view, key, location, modifiersListArg, repeat, locale); |
| 119 | + } else { // FireFox|w3c |
| 120 | + //http://www.w3.org/TR/DOM-Level-3-Events/#events-KeyboardEvent-initKeyboardEvent |
| 121 | + //https://developer.mozilla.org/en/DOM/KeyboardEvent#initKeyboardEvent() |
| 122 | + event.initKeyboardEvent(type, bubbles, cancelable, view, char, key, location, modifiersListArg, repeat, locale); |
| 123 | + } |
| 124 | + } else { |
| 125 | + event.initEvent(type, bubbles, cancelable) |
| 126 | + } |
| 127 | + |
| 128 | + for (propName in keyboardEventPropertiesDictionary) |
| 129 | + if (own(keyboardEventPropertiesDictionary, propName)) { |
| 130 | + if (event[propName] != localDict[propName]) { |
| 131 | + try { |
| 132 | + delete event[propName]; |
| 133 | + ObjectDefineProperty(event, propName, { |
| 134 | + writable: true, |
| 135 | + "value": localDict[propName] |
| 136 | + }); |
| 137 | + } catch (error) { |
| 138 | + //Some properties is read-only |
| 139 | + } |
| 140 | + |
| 141 | + } |
| 142 | + } |
| 143 | + |
| 144 | + return event; |
| 145 | +} |
0 commit comments