Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save f-steff/ace84434e1ee4e1107bcf0ba8d72ed2b to your computer and use it in GitHub Desktop.
Save f-steff/ace84434e1ee4e1107bcf0ba8d72ed2b to your computer and use it in GitHub Desktop.
Tampermonkey userscript to enable missing keys in Scandinavian Keyboard mapping on Mac

Tampermonkey userscript to type missing characters into Google Sheets on mac

This script was started when I wanted to type $ and other shift+option characters into Google Sheets on MacOS with a Danish keyboard.

The problem

It turns out that 7 out of the ten shift+option+digit keys available on the keyboard have been hijacked by Google Sheets for internal shortcuts to add and remove borders to cells. On many keyboard layouts thats not a problem, but for scandinavian keyboard layouts it means that these 7 characters: ¯ ” $ ¢ \ { , simply can not be type into Sheet, and instead must be copy-pasted to be inserted.

This is especially problematic if you type in formulas in Google Sheets, since several of these signs are vital in formulas.

The solusion

I've created a userscript that can be installed in Chrome and other browsers, which will intercept keyboard events on the Google Sheets page. If any of the 10 shift+option+digit keys are pressed, the event is blocked and the proper character is inserted via clipboard.

In Google Sheets, if a cell is in edit mode, a character is insertes exactly as if it was any ordinary key. If the cell is not in edit mode, the behaviour is slightly different to if it was any ordinary key:

  • An ordinary key event would insert the character into a cell, overwriting contents, and leave the cell in edit model, so subsequent key events would be added to the cell.
  • This solusion will insert the character into the cell, overwriting contents, but NOT leave the cell in edit mode, so subsequent key events will overwrite contents.

Usage

Simply install the script in Tampermonkey, and reload your Google Sheets page.

Install Tampermonkey from here: https://www.tampermonkey.net/

// ==UserScript==
// @name Google Sheets Enable Scandinavian Keyboard On Mac
// @namespace http://tampermonkey.net/
// @version 1.0
// @description Fix shift+option+number keys in Google Sheets on Mac while using a Scandinavian keyboard layout, by nterception 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.
// @author Flemming Steffensen
// @match https://docs.google.com/spreadsheets/*
// @grant none
// @homepageURL https://gist.github.com/f-steff/ace84434e1ee4e1107bcf0ba8d72ed2b
// @updateURL https://gist.githubusercontent.com/f-steff/ace84434e1ee4e1107bcf0ba8d72ed2b/raw/GoogleSheetsEnableScandinavianKeyboardOnMac.js
// @downloadURL https://gist.githubusercontent.com/f-steff/ace84434e1ee4e1107bcf0ba8d72ed2b/raw/GoogleSheetsEnableScandinavianKeyboardOnMac.js
// @run-at document-start
// ==/UserScript==
function simulatePaste(code, character) {
navigator.clipboard.writeText(character).then(() => {
document.execCommand('paste');
});
}
(function() {
'use strict';
var isMac = navigator.platform.toUpperCase().indexOf('MAC') >= 0;
if (!isMac) return; // Only a fix for Mac computer
// Store the original addEventListener function
const originalAddEventListener = EventTarget.prototype.addEventListener;
// To my supprise, browser keyboard events are unfiltered hardware events, so they need debounching.
const debounceTime = 50; // milliseconds
let lastInvocationTime = 0;
// Hack: Override addEventListener on the EventTarget prototype
EventTarget.prototype.addEventListener = function(type, listener, options) {
const isPassive = options && (typeof options === 'object') && options.passive;
const customListener = (event) => {
if (type === "keydown" && event.shiftKey && event.altKey && event.code.startsWith("Digit")) {
if (!isPassive) {
event.preventDefault(); // Prevent the default key action
}
const currentTime = new Date().getTime();
if (currentTime - lastInvocationTime > debounceTime) {
console.log("Current: " + currentTime + " Last: " + lastInvocationTime + " = Shift and Option are pressed together with " + event.code);
lastInvocationTime = currentTime;
// Handle keys
if (event.code === "Digit1") { simulatePaste(event.code, "¯"); } // Sheets mapping is: upper border set
else if (event.code === "Digit2") { simulatePaste(event.code, "”"); } // Sheets mapping is: right border set
else if (event.code === "Digit3") { simulatePaste(event.code, "$"); } // Sheets mapping is: lower border set
else if (event.code === "Digit4") { simulatePaste(event.code, "¢"); } // Sheets mapping is: left border set
else if (event.code === "Digit5") { simulatePaste(event.code, "‰"); }
else if (event.code === "Digit6") { simulatePaste(event.code, "˜"); } // Sheets mapping is: all border clear
else if (event.code === "Digit7") { simulatePaste(event.code, "\\"); } // Sheets mapping is: all border set
else if (event.code === "Digit8") { simulatePaste(event.code, "{"); }
else if (event.code === "Digit9") { simulatePaste(event.code, "}"); }
else if (event.code === "Digit0") { simulatePaste(event.code, "≈"); }
} else return;
} else {
listener.call(this, event);
}
};
// Call the original addEventListener function
originalAddEventListener.call(this, type, customListener, options);
};
})();
0.1 First version uploaded to Github
1.0 Added Userscript integration links to this gist. Hopefully this means automatic update detection is working.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment