Skip to content

All your computers and your phone/iPhone, working as one desk. Screens, apps, files, clipboard, calls, texts, notifications, mouse and keyboard, shared between Mac, Windows, Linux , Android, Web and ios / ipados . Free and open source. No account, no cloud, and no shared network needed.

2.5 KB
Raw Blame
// TwinDesk — Copyright (C) 2026 inukaasith. GPL-3.0, see LICENSE.
// shared/modmap.js: Keyboard › Modifier keys, applied where keys are received.
//   - off, or a key not in the table: as before (Swap Ctrl and Command included)
//   - a mapped key isn't swapped as well (Mac left Option -> left Ctrl, right Option stays Alt)
//   - a key comes up as it went down, even when the table changes while it's held (no stuck keys)
//   - clean() keeps only modifiers, drops a key mapped to itself
// Run with: node shared/test/modmap.js
'use strict';
const assert = require('assert');
const { clean, target, Mapper } = require('../modmap.js');
const P = require('../protocol.js');
const swap = (c) => P.mapKeyFromOtherPlatform(c);

// clean
assert.deepStrictEqual(clean(undefined), { on: false, keys: {} });
assert.deepStrictEqual(clean({ on: true, keys: { AltLeft: 'ControlLeft', ShiftLeft: 'ShiftLeft', KeyA: 'ControlLeft', AltRight: 'Escape' } }), { on: true, keys: { AltLeft: 'ControlLeft' } });
assert.strictEqual(clean({ on: 'yes' }).on, false);

// off: exactly as before
const off = { on: false, keys: { AltLeft: 'ControlLeft' } };
assert.strictEqual(target(off, 'AltLeft', swap), 'AltLeft');
assert.strictEqual(target(off, 'MetaLeft', swap), 'ControlLeft');
assert.strictEqual(target(off, 'KeyA', swap), 'KeyA');

// on: mapped keys go where they're told, unmapped ones keep the swap
const on = { on: true, keys: { AltLeft: 'ControlLeft', MetaLeft: 'MetaLeft' } };
assert.strictEqual(target(on, 'AltLeft', swap), 'ControlLeft');
assert.strictEqual(target(on, 'AltRight', swap), 'AltRight');
assert.strictEqual(target(on, 'MetaRight', swap), 'ControlRight');

// no stuck keys: the table changes while left Option is held
let map = { on: true, keys: { AltLeft: 'ControlLeft' } };
const m = new Mapper(() => map);
assert.strictEqual(m.map(true, 'AltLeft', swap), 'ControlLeft');
map = { on: false, keys: {} };
assert.strictEqual(m.map(true, 'AltLeft', swap), 'ControlLeft', 'a repeat while held, as it was pressed');
assert.strictEqual(m.map(false, 'AltLeft', swap), 'ControlLeft', 'released as it was pressed');
assert.strictEqual(m.map(true, 'AltLeft', swap), 'AltLeft');
assert.strictEqual(m.map(false, 'AltLeft', swap), 'AltLeft');
// a release never seen pressed (pressed before the session): mapped as the table says now
assert.strictEqual(m.map(false, 'MetaLeft', swap), 'ControlLeft');
// reset forgets what was held
m.map(true, 'ShiftLeft', swap);
m.reset();
assert.strictEqual(m.down.size, 0);
console.log('modmap: ok');