Greasy Fork is available in English.

Key navigation

To be used for mapping keyboard arrow press events

Tento skript by neměl být instalován přímo. Jedná se o knihovnu, kterou by měly jiné skripty využívat pomocí meta příkazu // @require https://greasyfork.org/scripts/396703-key-navigation/code/Key%20navigation.js?version=774316

(() => {
  'use strict';

  const KEYS = {
    37: 'LEFT',
    38: 'UP',
    39: 'RIGHT',
    40: 'DOWN',
  };

  const setUpKeyNavigation = ({
    onDownPressed,
    onLeftPressed,
    onRightPressed,
    onUpPressed,
    preventDefault,
    stopPropagation,
  }) => {
    document.addEventListener('keydown', event => {
      const eventKey = KEYS[event.which];

      const operation = {
        DOWN: e => executeFn(onDownPressed, e),
        LEFT: e => executeFn(onLeftPressed, e),
        RIGHT: e => executeFn(onRightPressed, e),
        UP: e => executeFn(onUpPressed, e),
      }[eventKey];
      operation && operation(event);

      preventDefault && event.preventDefault();
      stopPropagation && event.stopPropagation();
    });
  };

  const executeFn = (fn, ...parameters) => {
    return typeof fn === 'function' && fn(...parameters);
  };

  window.setUpKeyNavigation = setUpKeyNavigation;
})();