Colemak-DH Remapper for Remote Desktop

Remaps QWERTY key codes to Colemak-DH for Google Remote Desktop

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         Colemak-DH Remapper for Remote Desktop
// @namespace    http://tampermonkey.net/
// @version      0.5
// @description  Remaps QWERTY key codes to Colemak-DH for Google Remote Desktop
// @author       Michael M
// @license      LGPL-3.0-or-later
// @match        https://remotedesktop.google.com/*
// @grant        none
// @run-at       document-start
// ==/UserScript==

(function() {
    'use strict';

    const keyMap = {
        'Digit7': 'Equal',
        'Digit8': 'Digit7',
        'Digit9': 'Digit8',
        'Digit0': 'Digit9',
        'Minus': 'Digit0',
        'Equal': 'Minus',
        'KeyQ': 'KeyQ',
        'KeyW': 'KeyW',
        'KeyE': 'KeyF',
        'KeyR': 'KeyP',
        'KeyT': 'KeyB',
        'KeyY': 'BracketLeft',
        'KeyU': 'KeyJ',
        'KeyI': 'KeyL',
        'KeyO': 'KeyU',
        'KeyP': 'KeyY',
        'BracketLeft': 'Semicolon',
        'BracketRight': 'Slash',
        'KeyA': 'KeyA',
        'KeyS': 'KeyR',
        'KeyD': 'KeyS',
        'KeyF': 'KeyT',
        'KeyG': 'KeyG',
        'KeyH': 'BracketRight',
        'KeyJ': 'KeyM',
        'KeyK': 'KeyN',
        'KeyL': 'KeyE',
        'Semicolon': 'KeyI',
        'Quote': 'KeyO',
        'Backslash': 'Quote',
        'IntlBackslash': 'KeyZ',
        'KeyZ': 'KeyX',
        'KeyX': 'KeyC',
        'KeyC': 'KeyD',
        'KeyV': 'KeyV',
        'KeyB': 'Backslash',
        'KeyN': 'KeyN', // KeyN is handled separately since the #/~ key has no code
        'KeyM': 'KeyK',
        'Comma': 'KeyH',
        'Period': 'Comma',
        'Slash': 'Period',
    };

    const remapper = (e) => {
        // Only process events generated by the user, not by this script.
        if (!e.isTrusted) {
            return;
        }

        if (keyMap[e.code] && e.target.getAttribute('jsname') === 'fQxT4c') {
            e.preventDefault();
            e.stopPropagation();

            // Handle 'n' and 'N' mapping
            let newCode = (e.code === 'KeyN') ? (e.shiftKey ? 'Backquote' : 'Digit3') : keyMap[e.code]

            const newEvent = new KeyboardEvent('keydown', {
                key: e.key,
                code: newCode,
                bubbles: true,
                cancelable: true,
                composed: true,
                location: e.location,
                ctrlKey: e.ctrlKey,
                shiftKey: ((e.code === 'KeyN') || (e.shiftKey)),
                altKey: e.altKey,
                metaKey: e.metaKey,
                repeat: e.repeat,
            });
            if (e.code === 'KeyN' && !e.shiftKey) {
                e.target.dispatchEvent(new KeyboardEvent('keydown', {
                    key: 'Shift',
                    code: 'ShiftLeft',
                    bubbles: true,
                    cancelable: true,
                    composed: true,
                    location: 1, // left shift
                }));
                e.target.dispatchEvent(newEvent);
                e.target.dispatchEvent(new KeyboardEvent('keyup', {
                    key: 'Shift',
                    code: 'ShiftLeft',
                    bubbles: true,
                    cancelable: true,
                    composed: true,
                    location: 1
                }));
            } else {
                e.target.dispatchEvent(newEvent);
            }
        }
    };

    document.addEventListener('keydown', remapper, true);
})();