SPB Git forge

spb/admin-ka

Public
41commits 1branches 0releases
172.9 MBsize
maindefault branch
19 days agolast push
JavaScript 65.5% Python 17.8% CSS 13% HTML 3.7%
12.8 KB · 293 lines javascript
Raw Blame History
1"use strict";23Object.defineProperty(exports, "__esModule", {4  value: true5});6exports["default"] = void 0;7var Log = _interopRequireWildcard(require("../util/logging.js"));8var _events = require("../util/events.js");9var KeyboardUtil = _interopRequireWildcard(require("./util.js"));10var _keysym = _interopRequireDefault(require("./keysym.js"));11var browser = _interopRequireWildcard(require("../util/browser.js"));12function _interopRequireDefault(e) { return e && e.__esModule ? e : { "default": e }; }13function _getRequireWildcardCache(e) { if ("function" != typeof WeakMap) return null; var r = new WeakMap(), t = new WeakMap(); return (_getRequireWildcardCache = function _getRequireWildcardCache(e) { return e ? t : r; })(e); }14function _interopRequireWildcard(e, r) { if (!r && e && e.__esModule) return e; if (null === e || "object" != _typeof(e) && "function" != typeof e) return { "default": e }; var t = _getRequireWildcardCache(r); if (t && t.has(e)) return t.get(e); var n = { __proto__: null }, a = Object.defineProperty && Object.getOwnPropertyDescriptor; for (var u in e) if ("default" !== u && {}.hasOwnProperty.call(e, u)) { var i = a ? Object.getOwnPropertyDescriptor(e, u) : null; i && (i.get || i.set) ? Object.defineProperty(n, u, i) : n[u] = e[u]; } return n["default"] = e, t && t.set(e, n), n; }15function _typeof(o) { "@babel/helpers - typeof"; return _typeof = "function" == typeof Symbol && "symbol" == typeof Symbol.iterator ? function (o) { return typeof o; } : function (o) { return o && "function" == typeof Symbol && o.constructor === Symbol && o !== Symbol.prototype ? "symbol" : typeof o; }, _typeof(o); }16function _classCallCheck(a, n) { if (!(a instanceof n)) throw new TypeError("Cannot call a class as a function"); }17function _defineProperties(e, r) { for (var t = 0; t < r.length; t++) { var o = r[t]; o.enumerable = o.enumerable || !1, o.configurable = !0, "value" in o && (o.writable = !0), Object.defineProperty(e, _toPropertyKey(o.key), o); } }18function _createClass(e, r, t) { return r && _defineProperties(e.prototype, r), t && _defineProperties(e, t), Object.defineProperty(e, "prototype", { writable: !1 }), e; }19function _toPropertyKey(t) { var i = _toPrimitive(t, "string"); return "symbol" == _typeof(i) ? i : i + ""; }20function _toPrimitive(t, r) { if ("object" != _typeof(t) || !t) return t; var e = t[Symbol.toPrimitive]; if (void 0 !== e) { var i = e.call(t, r || "default"); if ("object" != _typeof(i)) return i; throw new TypeError("@@toPrimitive must return a primitive value."); } return ("string" === r ? String : Number)(t); } /*21 * noVNC: HTML5 VNC client22 * Copyright (C) 2019 The noVNC authors23 * Licensed under MPL 2.0 or any later version (see LICENSE.txt)24 */25//26// Keyboard event handler27//28var Keyboard = exports["default"] = /*#__PURE__*/function () {29  function Keyboard(target) {30    _classCallCheck(this, Keyboard);31    this._target = target || null;32    this._keyDownList = {}; // List of depressed keys33    // (even if they are happy)34    this._altGrArmed = false; // Windows AltGr detection3536    // keep these here so we can refer to them later37    this._eventHandlers = {38      'keyup': this._handleKeyUp.bind(this),39      'keydown': this._handleKeyDown.bind(this),40      'blur': this._allKeysUp.bind(this)41    };4243    // ===== EVENT HANDLERS =====4445    this.onkeyevent = function () {}; // Handler for key press/release46  }4748  // ===== PRIVATE METHODS =====49  return _createClass(Keyboard, [{50    key: "_sendKeyEvent",51    value: function _sendKeyEvent(keysym, code, down) {52      var numlock = arguments.length > 3 && arguments[3] !== undefined ? arguments[3] : null;53      var capslock = arguments.length > 4 && arguments[4] !== undefined ? arguments[4] : null;54      if (down) {55        this._keyDownList[code] = keysym;56      } else {57        // Do we really think this key is down?58        if (!(code in this._keyDownList)) {59          return;60        }61        delete this._keyDownList[code];62      }63      Log.Debug("onkeyevent " + (down ? "down" : "up") + ", keysym: " + keysym, ", code: " + code + ", numlock: " + numlock + ", capslock: " + capslock);64      this.onkeyevent(keysym, code, down, numlock, capslock);65    }66  }, {67    key: "_getKeyCode",68    value: function _getKeyCode(e) {69      var code = KeyboardUtil.getKeycode(e);70      if (code !== 'Unidentified') {71        return code;72      }7374      // Unstable, but we don't have anything else to go on75      if (e.keyCode) {76        // 229 is used for composition events77        if (e.keyCode !== 229) {78          return 'Platform' + e.keyCode;79        }80      }8182      // A precursor to the final DOM3 standard. Unfortunately it83      // is not layout independent, so it is as bad as using keyCode84      if (e.keyIdentifier) {85        // Non-character key?86        if (e.keyIdentifier.substr(0, 2) !== 'U+') {87          return e.keyIdentifier;88        }89        var codepoint = parseInt(e.keyIdentifier.substr(2), 16);90        var _char = String.fromCharCode(codepoint).toUpperCase();91        return 'Platform' + _char.charCodeAt();92      }93      return 'Unidentified';94    }95  }, {96    key: "_handleKeyDown",97    value: function _handleKeyDown(e) {98      var code = this._getKeyCode(e);99      var keysym = KeyboardUtil.getKeysym(e);100      var numlock = e.getModifierState('NumLock');101      var capslock = e.getModifierState('CapsLock');102103      // getModifierState for NumLock is not supported on mac and ios and always returns false.104      // Set to null to indicate unknown/unsupported instead.105      if (browser.isMac() || browser.isIOS()) {106        numlock = null;107      }108109      // Windows doesn't have a proper AltGr, but handles it using110      // fake Ctrl+Alt. However the remote end might not be Windows,111      // so we need to merge those in to a single AltGr event. We112      // detect this case by seeing the two key events directly after113      // each other with a very short time between them (<50ms).114      if (this._altGrArmed) {115        this._altGrArmed = false;116        clearTimeout(this._altGrTimeout);117        if (code === "AltRight" && e.timeStamp - this._altGrCtrlTime < 50) {118          // FIXME: We fail to detect this if either Ctrl key is119          //        first manually pressed as Windows then no120          //        longer sends the fake Ctrl down event. It121          //        does however happily send real Ctrl events122          //        even when AltGr is already down. Some123          //        browsers detect this for us though and set the124          //        key to "AltGraph".125          keysym = _keysym["default"].XK_ISO_Level3_Shift;126        } else {127          this._sendKeyEvent(_keysym["default"].XK_Control_L, "ControlLeft", true, numlock, capslock);128        }129      }130131      // We cannot handle keys we cannot track, but we also need132      // to deal with virtual keyboards which omit key info133      if (code === 'Unidentified') {134        if (keysym) {135          // If it's a virtual keyboard then it should be136          // sufficient to just send press and release right137          // after each other138          this._sendKeyEvent(keysym, code, true, numlock, capslock);139          this._sendKeyEvent(keysym, code, false, numlock, capslock);140        }141        (0, _events.stopEvent)(e);142        return;143      }144145      // Alt behaves more like AltGraph on macOS, so shuffle the146      // keys around a bit to make things more sane for the remote147      // server. This method is used by RealVNC and TigerVNC (and148      // possibly others).149      if (browser.isMac() || browser.isIOS()) {150        switch (keysym) {151          case _keysym["default"].XK_Super_L:152            keysym = _keysym["default"].XK_Alt_L;153            break;154          case _keysym["default"].XK_Super_R:155            keysym = _keysym["default"].XK_Super_L;156            break;157          case _keysym["default"].XK_Alt_L:158            keysym = _keysym["default"].XK_Mode_switch;159            break;160          case _keysym["default"].XK_Alt_R:161            keysym = _keysym["default"].XK_ISO_Level3_Shift;162            break;163        }164      }165166      // Is this key already pressed? If so, then we must use the167      // same keysym or we'll confuse the server168      if (code in this._keyDownList) {169        keysym = this._keyDownList[code];170      }171172      // macOS doesn't send proper key releases if a key is pressed173      // while meta is held down174      if ((browser.isMac() || browser.isIOS()) && e.metaKey && code !== 'MetaLeft' && code !== 'MetaRight') {175        this._sendKeyEvent(keysym, code, true, numlock, capslock);176        this._sendKeyEvent(keysym, code, false, numlock, capslock);177        (0, _events.stopEvent)(e);178        return;179      }180181      // macOS doesn't send proper key events for modifiers, only182      // state change events. That gets extra confusing for CapsLock183      // which toggles on each press, but not on release. So pretend184      // it was a quick press and release of the button.185      if ((browser.isMac() || browser.isIOS()) && code === 'CapsLock') {186        this._sendKeyEvent(_keysym["default"].XK_Caps_Lock, 'CapsLock', true, numlock, capslock);187        this._sendKeyEvent(_keysym["default"].XK_Caps_Lock, 'CapsLock', false, numlock, capslock);188        (0, _events.stopEvent)(e);189        return;190      }191192      // Windows doesn't send proper key releases for a bunch of193      // Japanese IM keys so we have to fake the release right away194      var jpBadKeys = [_keysym["default"].XK_Zenkaku_Hankaku, _keysym["default"].XK_Eisu_toggle, _keysym["default"].XK_Katakana, _keysym["default"].XK_Hiragana, _keysym["default"].XK_Romaji];195      if (browser.isWindows() && jpBadKeys.includes(keysym)) {196        this._sendKeyEvent(keysym, code, true, numlock, capslock);197        this._sendKeyEvent(keysym, code, false, numlock, capslock);198        (0, _events.stopEvent)(e);199        return;200      }201      (0, _events.stopEvent)(e);202203      // Possible start of AltGr sequence? (see above)204      if (code === "ControlLeft" && browser.isWindows() && !("ControlLeft" in this._keyDownList)) {205        this._altGrArmed = true;206        this._altGrTimeout = setTimeout(this._interruptAltGrSequence.bind(this), 100);207        this._altGrCtrlTime = e.timeStamp;208        return;209      }210      this._sendKeyEvent(keysym, code, true, numlock, capslock);211    }212  }, {213    key: "_handleKeyUp",214    value: function _handleKeyUp(e) {215      (0, _events.stopEvent)(e);216      var code = this._getKeyCode(e);217218      // We can't get a release in the middle of an AltGr sequence, so219      // abort that detection220      this._interruptAltGrSequence();221222      // See comment in _handleKeyDown()223      if ((browser.isMac() || browser.isIOS()) && code === 'CapsLock') {224        this._sendKeyEvent(_keysym["default"].XK_Caps_Lock, 'CapsLock', true);225        this._sendKeyEvent(_keysym["default"].XK_Caps_Lock, 'CapsLock', false);226        return;227      }228      this._sendKeyEvent(this._keyDownList[code], code, false);229230      // Windows has a rather nasty bug where it won't send key231      // release events for a Shift button if the other Shift is still232      // pressed233      if (browser.isWindows() && (code === 'ShiftLeft' || code === 'ShiftRight')) {234        if ('ShiftRight' in this._keyDownList) {235          this._sendKeyEvent(this._keyDownList['ShiftRight'], 'ShiftRight', false);236        }237        if ('ShiftLeft' in this._keyDownList) {238          this._sendKeyEvent(this._keyDownList['ShiftLeft'], 'ShiftLeft', false);239        }240      }241    }242  }, {243    key: "_interruptAltGrSequence",244    value: function _interruptAltGrSequence() {245      if (this._altGrArmed) {246        this._altGrArmed = false;247        clearTimeout(this._altGrTimeout);248        this._sendKeyEvent(_keysym["default"].XK_Control_L, "ControlLeft", true);249      }250    }251  }, {252    key: "_allKeysUp",253    value: function _allKeysUp() {254      Log.Debug(">> Keyboard.allKeysUp");255256      // Prevent control key being processed after losing focus.257      this._interruptAltGrSequence();258      for (var code in this._keyDownList) {259        this._sendKeyEvent(this._keyDownList[code], code, false);260      }261      Log.Debug("<< Keyboard.allKeysUp");262    }263264    // ===== PUBLIC METHODS =====265  }, {266    key: "grab",267    value: function grab() {268      //Log.Debug(">> Keyboard.grab");269270      this._target.addEventListener('keydown', this._eventHandlers.keydown);271      this._target.addEventListener('keyup', this._eventHandlers.keyup);272273      // Release (key up) if window loses focus274      window.addEventListener('blur', this._eventHandlers.blur);275276      //Log.Debug("<< Keyboard.grab");277    }278  }, {279    key: "ungrab",280    value: function ungrab() {281      //Log.Debug(">> Keyboard.ungrab");282283      this._target.removeEventListener('keydown', this._eventHandlers.keydown);284      this._target.removeEventListener('keyup', this._eventHandlers.keyup);285      window.removeEventListener('blur', this._eventHandlers.blur);286287      // Release (key up) all keys that are in a down state288      this._allKeysUp();289290      //Log.Debug(">> Keyboard.ungrab");291    }292  }]);293}();