production-taskbar-client / src / main / ipc / inputEventChannel.js
inputEventChannel.js
Raw
import { spawn } from "child_process";
import { app, ipcMain } from "electron";
import path from "path";

const kInputEventChannel = "input-event";

export default function initInputEventChannel() {
  const logdir = path.join(app.getPath("userData"), "logs");
  const keyServer = spawn("./bin/win-key-server.exe", [
    "/force",
    "taskbarInit",
    `logdir=${logdir}`,
  ]);

  keyServer.stdout.on("data", (data) => {
    // In some cases multiple input data come together in stream. Split prevent parse error.
    const keys = data
      .toString()
      .split("\n")
      .filter((i) => i);

    keys.forEach((keyData) => {
      ipcMain.emit(kInputEventChannel, {
        event: "key-down",
        data: JSON.parse(keyData),
      });
    });
  });

  keyServer.on("close", () => initInputEventChannel());
}