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.

  1. // ==UserScript==
  2. // @name ASCII Control Codes Keyboard Input Helper for Firefox
  3. // @namespace ASCIIControlCodesKeyboardInputHelperForFirefox
  4. // @version 1.0.2
  5. // @license AGPLv3
  6. // @author jcunews
  7. // @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.
  8. // @include *://*/*
  9. // @grant none
  10. // @run-at document-start
  11. // ==/UserScript==
  12.  
  13. var numpadKeyCodes = [45, 35, 40, 34, 37, 12, 39, 36, 38, 33]; //Insert, End, Down, PgDn, Left, Clear, Right, Home, Up, PgUp
  14. var code = "";
  15.  
  16. function numFromKeyCode(keyCode) {
  17. if ((keyCode >= 96) && (keyCode <= 105)) { //Numpad0 - Numpad9
  18. return keyCode - 96;
  19. } else return numpadKeyCodes.indexOf(keyCode);
  20. }
  21.  
  22. function keyUp(ev, ele, cc, i, s) {
  23. if (
  24. (ele = document.activeElement) && ((ele.tagName === "TEXTAREA") || ((ele.tagName === "INPUT") && (ele.type === "text")) || (ele.contentEditable === "true"))
  25. ) {
  26. ev = ev || window.event;
  27. if (ev.altKey) {
  28. cc = numFromKeyCode(ev.keyCode);
  29. if (cc >= 0) code += cc;
  30. } else {
  31. if ((ev.keyCode === 18 /*ALT*/) && (code.charAt(0) === "0") && (code = parseInt(code, 10)) && (code < 32)) {
  32. i = ele.selectionStart;
  33. s = ele.value;
  34. ele.value = s.substring(0, i) + String.fromCharCode(code) + s.substring(ele.selectionEnd, s.length);
  35. ele.selectionEnd = i + 1;
  36. }
  37. code = "";
  38. }
  39. } else code = "";
  40. }
  41.  
  42. if (window.InstallTrigger) document.addEventListener("keyup", keyUp);