Key Press

simulate Key Press

This script should not be not be installed directly. It is a library for other scripts to include with the meta directive // @require https://update.greasyfork.org/scripts/451574/1095111/Key%20Press.js

You will need to install an extension such as Tampermonkey, Greasemonkey or Violentmonkey to install this script.

You will need to install an extension such as Tampermonkey or Violentmonkey to install this script.

You will need to install an extension such as Tampermonkey or Violentmonkey to install this script.

You will need to install an extension such as Tampermonkey or Userscripts to install this script.

You will need to install an extension such as Tampermonkey to install this script.

You will need to install a user script manager extension to install this script.

(I already have a user script manager, let me install it!)

You will need to install an extension such as Stylus to install this style.

You will need to install an extension such as Stylus to install this style.

You will need to install an extension such as Stylus to install this style.

You will need to install a user style manager extension to install this style.

You will need to install a user style manager extension to install this style.

You will need to install a user style manager extension to install this style.

(I already have a user style manager, let me install it!)

!(function (moduleName, definition) {
  // Whether to expose Keyvent as an AMD module or to the global object.
  if (typeof define === 'function' && typeof define.amd === 'object') define(definition);
  else this[moduleName] = definition();

})('keyvent', function definition () {

  function contextOn (element) {
    var exports = {};
    exports.on = contextOn;

    exports.down = function (keys) {
      dispatch(element, 'keydown', keys);
    };

    exports.up = function (keys) {
      dispatch(element, 'keyup', keys);
    };

    return exports;
  }

  function dispatch (element, type, keys) {
    var event = document.createEvent('HTMLEvents');
    event.initEvent(type, true, true);
    keys = normalizeKeys(keys);
    for (var i = 0; i < keys.length; i++) {
      var keyCode = toKeyCode(keys[i]);
      event.which = event.keyCode = keyCode;
      if (MODIFIERS[keyCode]) event[MODIFIERS[keyCode] + 'Key'] = true;
      element.dispatchEvent(event);
    }
  }

  function normalizeKeys (keys) {
    if (!keys) return [0];
    if (isString(keys)) return keys.split(' ');
    return [keys];
  }

  function isString (object) { return typeof object === 'string'; }

  // Borrowed from https://github.com/madrobby/keymaster
  var ALIASES = {
    '⇧': 16, 'shift': 16,
    '⌃': 17, 'ctrl': 17, 'control': 17,
    '⌥': 18, 'alt': 18, 'option': 18,
    '⌘': 91, 'command': 91,
    'backspace': 8, 'tab': 9,
    'clear': 12, 'enter': 13,
    'return': 13, 'esc': 27,
    'escape': 27, 'space': 32,
    'left': 37, 'up': 38,
    'right': 39, 'down': 40,
    'del': 46, 'delete': 46,
    'home': 36, 'end': 35,
    'pageup': 33, 'pagedown': 34,
    ',': 188, '.': 190,
    '/': 191, '`': 192,
    '-': 189, '=': 187,
    ';': 186, '\'': 222,
    '[': 219, ']': 221,
    '\\': 220
  };
  for (key = 1; key < 20; key++) ALIASES['f' + key] = ALIASES['F' + key] = 111 + key;

  var MODIFIERS = {
    '16': 'shift',
    '17': 'ctrl',
    '18': 'alt',
    '91': 'meta'
  };

  function toKeyCode(key) {
    if (isString(key)) return ALIASES[key] || key.toUpperCase().charCodeAt(0);
    return key;
  }

  return contextOn(document);
});