ScreenTap.js is a Node.js addon library that allows you to detect mouse/tap events on the screen/display. It currently supports both macOS and Windows. The library triggers events that may occur on different screens and provides the displayId of the currently active screen (screen where the event occured), such as mouse up/down events and foreground window changes/moves (currently only supported on Windows).
You can install ScreenTap.js using npm:
npm install screentap
ScreenTap.js exports a ScreenTap
class that extends EventEmitter
. You can use it to listen for screen change events.
Here's a basic example:
import ScreenTap from "screentap";
const screenTap = new ScreenTap();
screenTap.on("foregroundWindowUpdated", ({ eventName, displayId }) => {
console.log("Foreground window updated ", displayId);
});
screenTap.on("leftMouseDown", ({ eventName, displayId }) => {
console.log("Left mouse button down", displayId);
});
screenTap.on("leftMouseUp", ({ eventName, displayId }) => {
console.log("Left mouse button up", displayId);
});
screenTap.on("rightMouseDown", ({ eventName, displayId }) => {
console.log("Right mouse button down", displayId);
});
screenTap.on("rightMouseUp", ({ eventName, displayId }) => {
console.log("Right mouse button up", displayId);
});
screenTap.startScreenTapListener();
setTimeout(() => {
screenTap.stopScreenTapListener();
}, 10_000);
Starts the screen tap listener. This should be called to start receiving the events in your subscribed listeners.
Subscribes to a screen change event. The listener
callback is called with an IScreenTapEventResult
object whenever the event occurs.
Unsubscribes from a screen change event. The listener
callback will no longer be called for the event.
Stops the screen tap listener. This should be called when you no longer want to receive events.
This interface represents the object that is passed to event listeners when a screen change event occurs.
This property is a string that indicates the type of the event that occurred. It can have one of the following values:
-
leftMouseDown
: Triggered when the left mouse button is pressed down. -
leftMouseUp
: Triggered when the left mouse button is released. -
rightMouseDown
: Triggered when the right mouse button is pressed down. -
rightMouseUp
: Triggered when the right mouse button is released. -
foregroundWindowUpdated
(Windows only): Triggered when the foreground window is updated. This event is currently only supported on Windows.
The display where the event occurred. If the display cannot be determined, the displayId
will be -1.
Since ScreenTap
extends EventEmitter
, all the APIs of Node.js's EventEmitter
are available. This includes methods like addListener
, removeListener
, removeAllListeners
, once
, and so on.
Here's an example of using the once
method to listen for a single leftMouseDown
event:
import ScreenTap from "screentap";
const screenTap = new ScreenTap();
screenTap.once("leftMouseDown", (event) => {
console.log("Left mouse button pressed", event);
});
screenTap.startScreenTapListener();
In this example, the listener is automatically removed after it is called once, so it will only log the first leftMouseDown
event.
For more information about the EventEmitter
API, see the Node.js documentation.
Contributions are welcome! Please submit a pull request or create an issue to propose changes or report bugs.
ScreenTap.js is licensed under the MIT License. See the LICENSE file for more details.