1 line
5.9 KiB
JavaScript
1 line
5.9 KiB
JavaScript
function KeyEventDecoder(e,r){"use strict";function t(e){for(var t=0;t<e.length;++t)r(e[t])}function n(t,n){var y={type:n},s=kbdUtil.getKey(t);s&&(y.keyId=s);var a=kbdUtil.getKeysym(t),u=e.hasShortcutModifier()||!!e.activeCharModifier();a&&("keydown"!==n||kbdUtil.nonCharacterKey(t)||u)&&(y.keysym=a);var i=16===t.keyCode||"Shift"===t.key,o=!i&&("keydown"!==n||e.hasShortcutModifier()||!!kbdUtil.nonCharacterKey(t)),c="keydown"===n&&e.activeCharModifier()&&!kbdUtil.nonCharacterKey(t),_=e.activeCharModifier();if(_&&a){for(var k=!1,l=0;l<_.length;++l)_[l]===a.keysym&&(k=!0);"keypress"!==n||k||(y.escape=e.activeCharModifier())}return c&&r({type:"stall"}),r(y),o}return{keydown:function(r){return t(e.keydown(r)),n(r,"keydown")},keypress:function(e){return n(e,"keypress")},keyup:function(r){return t(e.keyup(r)),n(r,"keyup")},syncModifiers:function(r){t(e.syncAny(r))},releaseAll:function(){r({type:"releaseall"})}}}function VerifyCharModifier(e){"use strict";function r(){if(!n)for(var y=function(){clearTimeout(n),n=null,r()};0!==t.length;){var s=t[0];switch(t=t.splice(1),s.type){case"stall":return void(n=setTimeout(y,5));case"keydown":0!==t.length&&"keypress"===t[0].type&&(!t[0].escape||s.keysym&&s.keysym.keysym===t[0].keysym.keysym||(s.escape=t[0].escape),s.keysym=t[0].keysym,t=t.splice(1))}"stall"!==s.type&&e(s)}}var t=[],n=null;return function(e){t.push(e),r()}}function TrackKeyState(e){"use strict";var r=[];return function(t){var n=0!==r.length?r[r.length-1]:null;switch(t.type){case"keydown":n&&t.keyId&&n.keyId===t.keyId||(n={keyId:t.keyId,keysyms:{}},r.push(n)),t.keysym&&(n.keysyms[t.keysym.keysym]=t.keysym,n.ignoreKeyPress=!0,e(t));break;case"keypress":n||(n={keyId:t.keyId,keysyms:{}},r.push(n)),t.keysym||console.log("keypress with no keysym:",t),t.keysym&&!n.ignoreKeyPress&&(n.keysyms[t.keysym.keysym]=t.keysym,t.type="keydown",e(t));break;case"keyup":if(0===r.length)return;for(var y=null,s=0;s!==r.length;++s)if(r[s].keyId===t.keyId){y=s;break}null===y&&(y=r.length-1);var a=r.splice(y,1)[0],u=function(){function e(){}return function(r){return e.prototype=r,new e}}();for(var i in a.keysyms){var o=u(t);o.keysym=a.keysyms[i],e(o)}break;case"releaseall":for(var s=0;s<r.length;++s)for(var i in r[s].keysyms){var c=r[s].keysyms[i];e({keyId:0,keysym:c,type:"keyup"})}r=[]}}}function EscapeModifiers(e){"use strict";return function(r){if("keydown"!==r.type||void 0===r.escape)return void e(r);for(var t=0;t<r.escape.length;++t)e({type:"keyup",keyId:0,keysym:keysyms.lookup(r.escape[t])});e(r);for(var t=0;t<r.escape.length;++t)e({type:"keydown",keyId:0,keysym:keysyms.lookup(r.escape[t])})}}var kbdUtil=function(){"use strict";function e(e){var r={536:350,537:351,538:354,539:355},t=r[e];return t||e}function r(){return navigator&&!!/mac/i.exec(navigator.platform)}function t(){return navigator&&!!/win/i.exec(navigator.platform)}function n(){return navigator&&!!/linux/i.exec(navigator.platform)}function y(e,r){var t={};for(var n in r)parseInt(n)!==XK_Shift_L&&(t[n]=r[n]);var y=0;for(var a in r)t[a]&&++y;return s(e,t)?y>e.length:y>0}function s(e,r){if(0===e.length)return!1;for(var t=0;t<e.length;++t)if(!r[e[t]])return!1;return!0}function a(e){function a(e,r){function t(e){return{keysym:keysyms.lookup(e),type:o[e]?"keydown":"keyup"}}var n=[];return void 0!==e.ctrlKey&&e.ctrlKey!==o[XK_Control_L]&&r!==XK_Control_L&&(o[XK_Control_L]=e.ctrlKey,n.push(t(XK_Control_L))),void 0!==e.altKey&&e.altKey!==o[XK_Alt_L]&&r!==XK_Alt_L&&(o[XK_Alt_L]=e.altKey,n.push(t(XK_Alt_L))),void 0!==e.altGraphKey&&e.altGraphKey!==o[XK_ISO_Level3_Shift]&&r!==XK_ISO_Level3_Shift&&(o[XK_ISO_Level3_Shift]=e.altGraphKey,n.push(t(XK_ISO_Level3_Shift))),void 0!==e.shiftKey&&e.shiftKey!==o[XK_Shift_L]&&r!==XK_Shift_L&&(o[XK_Shift_L]=e.shiftKey,n.push(t(XK_Shift_L))),void 0!==e.metaKey&&e.metaKey!==o[XK_Meta_L]&&r!==XK_Meta_L&&(o[XK_Meta_L]=e.metaKey,n.push(t(XK_Meta_L))),n}function u(e,r){var t=i(e),n=t?t.keysym:null;return null!==n&&void 0!==o[n]&&(o[n]=r),a(e,n)}e||(e=r()?[XK_Alt_L]:t()?[XK_Alt_L,XK_Control_L]:n()?[XK_ISO_Level3_Shift]:[]);var o={};return o[XK_Control_L]=!1,o[XK_Alt_L]=!1,o[XK_ISO_Level3_Shift]=!1,o[XK_Shift_L]=!1,o[XK_Meta_L]=!1,{keydown:function(e){return u(e,!0)},keyup:function(e){return u(e,!1)},syncAny:function(e){return a(e)},hasShortcutModifier:function(){return y(e,o)},activeCharModifier:function(){return s(e,o)?e:null}}}function u(e){return"keyCode"in e&&"key"in e?e.key+":"+e.keyCode:"keyCode"in e?e.keyCode:e.key}function i(r){var t;if(r.char&&1===r.char.length?t=r.char.charCodeAt():r.charCode?t=r.charCode:r.keyCode&&"keypress"===r.type&&(t=r.keyCode),t){var n=keysyms.fromUnicode(e(t));if(n)return n}return r.keyCode?keysyms.lookup(o(r.keyCode,r.shiftKey)):r.which?keysyms.lookup(o(r.which,r.shiftKey)):null}function o(e,r){if("number"!=typeof e)return null;if(e>=48&&e<=57)return e;if(e>=65&&e<=90)return r?e:e+32;if(e>=96&&e<=105)return XK_KP_0+(e-96);switch(e){case 32:return XK_space;case 106:return XK_KP_Multiply;case 107:return XK_KP_Add;case 108:return XK_KP_Separator;case 109:return XK_KP_Subtract;case 110:return XK_KP_Decimal;case 111:return XK_KP_Divide;case 187:return XK_plus;case 188:return XK_comma;case 189:return XK_minus;case 190:return XK_period}return c({keyCode:e})}function c(e){if(!e.keyCode)return null;var r=e.keyCode;if(r>=112&&r<=135)return XK_F1+r-112;switch(r){case 8:return XK_BackSpace;case 13:return XK_Return;case 9:return XK_Tab;case 27:return XK_Escape;case 46:return XK_Delete;case 36:return XK_Home;case 35:return XK_End;case 33:return XK_Page_Up;case 34:return XK_Page_Down;case 45:return XK_Insert;case 37:return XK_Left;case 38:return XK_Up;case 39:return XK_Right;case 40:return XK_Down;case 16:return XK_Shift_L;case 17:return XK_Control_L;case 18:return XK_Alt_L;case 224:return XK_Meta_L;case 225:return XK_ISO_Level3_Shift;case 91:return XK_Super_L;case 92:return XK_Super_R;case 93:return XK_Menu;default:return null}}return{hasShortcutModifier:y,hasCharModifier:s,ModifierSync:a,getKey:u,getKeysym:i,keysymFromKeyCode:o,nonCharacterKey:c,substituteCodepoint:e}}(); |