ASCII Control Codes Keyboard Input Helper for Firefox

This is a script to help inputting ASCII Control Codes such as TAB, CR, LF, etc. on Firefox web browsers using ALT+Numpad keys. Inputted control codes must begin with 0 (e.g. 09, 013, etc.), and only control code 1 to 31 are accepted. The control character will only be generated when the current focus is on a TEXTAREA element, or a text typed INPUT element.

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!)

// ==UserScript==
// @name         ASCII Control Codes Keyboard Input Helper for Firefox
// @namespace    ASCIIControlCodesKeyboardInputHelperForFirefox
// @version      1.0.2
// @license      AGPLv3
// @author       jcunews
// @description  This is a script to help inputting ASCII Control Codes such as TAB, CR, LF, etc. on Firefox web browsers using ALT+Numpad keys. Inputted control codes must begin with 0 (e.g. 09, 013, etc.), and only control code 1 to 31 are accepted. The control character will only be generated when the current focus is on a TEXTAREA element, or a text typed INPUT element.
// @include      *://*/*
// @grant        none
// @run-at       document-start
// ==/UserScript==

var numpadKeyCodes = [45, 35, 40, 34, 37, 12, 39, 36, 38, 33]; //Insert, End, Down, PgDn, Left, Clear, Right, Home, Up, PgUp
var code = "";

function numFromKeyCode(keyCode) {
  if ((keyCode >= 96) && (keyCode <= 105)) { //Numpad0 - Numpad9
    return keyCode - 96;
  } else return numpadKeyCodes.indexOf(keyCode);
}

function keyUp(ev, ele, cc, i, s) {
  if (
    (ele = document.activeElement) && ((ele.tagName === "TEXTAREA") || ((ele.tagName === "INPUT") && (ele.type === "text")) || (ele.contentEditable === "true"))
  ) {
    ev = ev || window.event;
    if (ev.altKey) {
      cc = numFromKeyCode(ev.keyCode);
      if (cc >= 0) code += cc;
    } else {
      if ((ev.keyCode === 18 /*ALT*/) && (code.charAt(0) === "0") && (code = parseInt(code, 10)) && (code < 32)) {
        i = ele.selectionStart;
        s = ele.value;
        ele.value = s.substring(0, i) + String.fromCharCode(code) + s.substring(ele.selectionEnd, s.length);
        ele.selectionEnd = i + 1;
      }
      code = "";
    }
  } else code = "";
}

if (window.InstallTrigger) document.addEventListener("keyup", keyUp);