This tracker can be used to listen to user inputs (mouse clicks, keystrokes, mouse movement, and mouse scrolls) on any platform (MacOS, Windows, and Linux)
Add this git repository to your project as a git submodule
git submodule add https://github.com/HASEL-UZH/PA.UserInputTracker
Install the package by adding to package.json and running npm install
npm i ./PA.UserInputTracker/typescript
The simplest example looks as follows
import { UserInputTracker } from "user-input-tracker";
const tracker = new UserInputTracker(function (aggregate) {
console.log(aggregate);
});
tracker.start();
Not tested on Linux
- There is a bug on macOS that removes event listeners from time to time. uiohook-napi crashes when this happens (when used with Electron). This issue was reported, but it doesn't look like it will be fixed in the near future. To circumvent this issue, we created a fork. With the fork, the application no longer crashes but a restart of the tracker is required to reestablish the event listeners. This functionality has to be provided by the client application! As a reference, you could have a look at the fallback created in the FlowTeams project. There, the following code was added:
// check if tracker is still running (issue of io-library that only occurs on Mac)
// restart the tracker when no input is detected but active window changed
const msPerMinute = 60000;
const startTime = new Date(Date.now() - 3 * msPerMinute);
if (
isMac &&
(calculatedFlowStatus.flowStatus === FlowStatus.AVAILABLE ||
calculatedFlowStatus.flowStatus === FlowStatus.AWAY)
) {
const activeIOCount: number =
await UserInputAggregate.countActiveIntervals(startTime);
const activeWindowCount: number = await ActiveWindow.countActivity(
startTime
);
if (activeIOCount === 0 && activeWindowCount !== 0) {
console.error(
"[FlowTeams] No input detected for 3 minutes but detected window activity. Restart input tracker to resolve possible issue."
);
await this.pauseTrackers();
await this.resumeTrackers();
}
}
In words, if in the last three minutes no input was detected (activeIOCount
), but window activity was still detected (activeWindowCount
- active-win is used for this) then restart the tracker.
- Read more about it in this issue.
Huge thanks to the maintainers of uiohook-napi: https://github.com/SnosMe/uiohook-napi