Google Sheets Enable Scandinavian Keyboard On Mac

Fix shift+option+number keys in Google Sheets on Mac while using a Scandinavian keyboard layout, by interception of shift+option+number keys, and replace them with the correct char pasted in. Flaws: Does not enter edit mode when inserting into inactivated sheet cells.

  1. // ==UserScript==
  2. // @name Google Sheets Enable Scandinavian Keyboard On Mac
  3. // @namespace https://gist.github.com/f-steff
  4. // @version 1.3
  5. // @description Fix shift+option+number keys in Google Sheets on Mac while using a Scandinavian keyboard layout, by interception of shift+option+number keys, and replace them with the correct char pasted in. Flaws: Does not enter edit mode when inserting into inactivated sheet cells.
  6. // @author Flemming Steffensen
  7. // @match http://docs.google.com/spreadsheets/*
  8. // @match https://docs.google.com/spreadsheets/*
  9. // @include http://docs.google.com/spreadsheets/*
  10. // @include https://docs.google.com/spreadsheets/*
  11. // @grant none
  12. // @license MIT
  13. // @homepageURL https://gist.github.com/f-steff/ace84434e1ee4e1107bcf0ba8d72ed2b
  14. // @run-at document-start
  15. // ==/UserScript==
  16.  
  17.  
  18. function simulatePaste(code, character) {
  19. navigator.clipboard.writeText(character).then(() => {
  20. document.execCommand('paste');
  21. });
  22. }
  23.  
  24. (function() {
  25. 'use strict';
  26.  
  27. var isMac = navigator.platform.indexOf("Mac") === 0
  28. if (!isMac) return; // Only a fix for Mac computer
  29.  
  30. // Store the original addEventListener function
  31. const originalAddEventListener = EventTarget.prototype.addEventListener;
  32.  
  33. // To my supprise, browser keyboard events are unfiltered hardware events, so they need debounching.
  34. const debounceTime = 50; // milliseconds
  35. let lastInvocationTime = 0;
  36.  
  37. // Hack: Override addEventListener on the EventTarget prototype
  38. EventTarget.prototype.addEventListener = function(type, listener, options) {
  39. const isPassive = options && (typeof options === 'object') && options.passive;
  40. const customListener = (event) => {
  41. if (type === "keydown" && event.shiftKey && event.altKey && event.code.startsWith("Digit")) {
  42. if (!isPassive) {
  43. event.preventDefault(); // Prevent the default key action
  44. }
  45. const currentTime = new Date().getTime();
  46. if (currentTime - lastInvocationTime > debounceTime) {
  47.  
  48. console.log("Current: " + currentTime + " Last: " + lastInvocationTime + " = Shift and Option are pressed together with " + event.code);
  49. lastInvocationTime = currentTime;
  50.  
  51. // Handle keys
  52. if (event.code === "Digit1") { simulatePaste(event.code, "¯"); } // Sheets mapping is: upper border set
  53. else if (event.code === "Digit2") { simulatePaste(event.code, "”"); } // Sheets mapping is: right border set
  54. else if (event.code === "Digit3") { simulatePaste(event.code, "$"); } // Sheets mapping is: lower border set
  55. else if (event.code === "Digit4") { simulatePaste(event.code, "¢"); } // Sheets mapping is: left border set
  56. else if (event.code === "Digit5") { simulatePaste(event.code, "‰"); }
  57. else if (event.code === "Digit6") { simulatePaste(event.code, "˜"); } // Sheets mapping is: all border clear
  58. else if (event.code === "Digit7") { simulatePaste(event.code, "\\"); } // Sheets mapping is: all border set
  59. else if (event.code === "Digit8") { simulatePaste(event.code, "{"); }
  60. else if (event.code === "Digit9") { simulatePaste(event.code, "}"); }
  61. else if (event.code === "Digit0") { simulatePaste(event.code, "≈"); }
  62. } else return;
  63. } else {
  64. listener.call(this, event);
  65. }
  66. };
  67. // Call the original addEventListener function
  68. originalAddEventListener.call(this, type, customListener, options);
  69. };
  70. })();

QingJ © 2025

镜像随时可能失效,请加Q群300939539或关注我们的公众号极客氢云获取最新地址