Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
60 changes: 33 additions & 27 deletions companion-bridge/index.js
Original file line number Diff line number Diff line change
@@ -1,27 +1,22 @@
/* eslint-disable no-console */

const { GlobalKeyboardListener } = require("node-global-key-listener");
const { uIOhook, UiohookKey } = require("uiohook-napi");
const { WebSocketServer } = require("ws");

const PORT = 6969;
const pressedKeys = new Set();
const ToggleBindings = new Map([
["INSERT", "TOGGLE_MUTE"],
["NUMPAD0", "TOGGLE_MUTE"],
["HOME", "TOGGLE_DEAFEN"],
["NUMPAD7", "TOGGLE_DEAFEN"]
[UiohookKey.Insert, "TOGGLE_MUTE"],
[UiohookKey.Numpad0, "TOGGLE_MUTE"],
[UiohookKey.Home, "TOGGLE_DEAFEN"],
[UiohookKey.Numpad7, "TOGGLE_DEAFEN"]
]);

const wss = new WebSocketServer({ host: "127.0.0.1", port: PORT });
const keyboard = new GlobalKeyboardListener({
windows: {
onError: errorCode => console.error("Companion bridge keyboard error:", errorCode),
onInfo: info => console.info("Companion bridge keyboard info:", info)
}
});

function broadcast(action) {
const payload = JSON.stringify({ action });
console.log(`Companion bridge broadcasting ${action} to ${wss.clients.size} client(s).`);

for (const client of wss.clients) {
if (client.readyState === client.OPEN) {
Expand All @@ -30,31 +25,37 @@ function broadcast(action) {
}
}

function handleKeyDown(name) {
if (pressedKeys.has(name)) return;
function resolveAction(event) {
return ToggleBindings.get(event.keycode) ?? null;
}

function handleKeyDown(event) {
console.log(`Companion bridge keydown: keycode=${event.keycode}`);

if (pressedKeys.has(event.keycode)) return;

pressedKeys.add(name);
pressedKeys.add(event.keycode);

const action = ToggleBindings.get(name);
const action = resolveAction(event);
if (action) {
broadcast(action);
}
}

function handleKeyUp(name) {
pressedKeys.delete(name);
function handleKeyUp(event) {
console.log(`Companion bridge keyup: keycode=${event.keycode}`);
pressedKeys.delete(event.keycode);
}

keyboard.addListener(event => {
if (event.state === "DOWN") {
handleKeyDown(event.name);
return;
}
uIOhook.on("keydown", handleKeyDown);
uIOhook.on("keyup", handleKeyUp);

if (event.state === "UP") {
handleKeyUp(event.name);
}
});
try {
uIOhook.start();
console.log("Companion bridge keyboard listener started.");
} catch (error) {
console.error("Companion bridge failed to start keyboard listener:", error);
}

wss.on("listening", () => {
console.log(`Companion bridge listening on ws://localhost:${PORT}`);
Expand All @@ -77,7 +78,12 @@ wss.on("error", error => {
});

function shutdown() {
keyboard.kill();
try {
uIOhook.stop();
} catch (error) {
console.error("Companion bridge failed to stop keyboard listener:", error);
}

wss.close(() => {
process.exit(0);
});
Expand Down
2 changes: 1 addition & 1 deletion companion-bridge/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
"start": "node index.js"
},
"dependencies": {
"node-global-key-listener": "^0.3.0",
"uiohook-napi": "^1.5.4",
"ws": "^8.18.3"
}
}
Loading