1.3.2 • Published 10 months ago

node-native-win-utils v1.3.2

Weekly downloads
-
License
MIT
Repository
github
Last release
10 months ago

License

Node Native Win Utils

I did it for myself because I didn't feel like dealing with libraries like 'node-ffi' to implement this functionality. Maybe someone will find it useful. It's WINDOWS OS ONLY

This package is a native addon for Node.js that allows you to perform various utility operations on Windows systems. It includes key event listeners, window data retrieval, window screenshot capture functionality, mouse movement, mouse click, mouse drag, and typing functionality, also I included precompiled libs of OpenCV(core, imgcodecs, imgproc)

Installation

You can install the package using npm:

npm  install  node-native-win-utils

Usage

Importing the Package

To use the package, import the necessary functions, types, and classes:

import {
  keyDownHandler,
  keyUpHandler,
  getWindowData,
  captureWindow,
  captureWindowN,
  mouseMove,
  mouseClick,
  mouseDrag,
  typeString,
  OpenCV,
} from "node-native-win-utils";

Key Event Listeners

The package provides keyDownHandler and keyUpHandler functions, which allow you to register callbacks for key down and key up events, respectively. The callbacks receive the keyCode as a parameter:

keyDownHandler((keyCode) => {
  console.log("Key down:", keyCode);
});

// Key down: 8

keyUpHandler((keyCode) => {
  console.log("Key up:", keyCode);
});

// Key up: 8

Window Data

The getWindowData function retrieves information about a specific window identified by its name. It returns an object with properties width, height, x, and y, representing the window dimensions and position:

const windowData = getWindowData("Window Name");

console.log("Window data:", windowData);

// Window data: { width: 800, height: 600, x: 50, y: 50 }

Window Capture

The captureWindow function allows you to capture a screenshot of a specific window identified by its name. Provide the window name and the output path as parameters:

captureWindow("Window Name", "output.png");

// Output: output.png with a screenshot of the window

Mouse Movement

The mouseMove function allows you to move the mouse to a specific position on the screen. Provide the posX and posY coordinates as parameters:

mouseMove(100, 200);

Mouse Click

The mouseClick function allows you to perform a mouse click event. Optionally, you can specify the mouse button as a parameter ("left", "middle", or "right"). If no button is specified, a left mouse click is performed by default:

mouseClick(); // Left mouse click

mouseClick("right"); // Right mouse click

Mouse Drag

The mouseDrag function allows you to simulate dragging the mouse from one position to another. Provide the starting and ending coordinates (startX, startY, endX, endY) as parameters. Optionally, you can specify the speed at which the mouse should be dragged:

mouseDrag(100, 200, 300, 400);

mouseDrag(100, 200, 300, 400, 100); // Drag with speed 100

Typing

The typeString function allows you to simulate typing a string of characters. Provide the string to type as the stringToType parameter. Optionally, you can specify

a delay between each character (in milliseconds) using the delay parameter:

typeString("Hello, world!");

typeString("Hello, world!", 100); // Type with a delay of 100ms between characters

Key Listener Class

The KeyListener class extends the EventEmitter class and simplifies working with the keyDownHandler and keyUpHandler functions. You can register event listeners for the "keyDown" and "keyUp" events using the on method:

const listener = new KeyListener();

listener.on("keyDown", (data) => {
  console.log("Key down:", data.keyCode, data.keyName);
});

// Key down: 8 Backspace

listener.on("keyUp", (data) => {
  console.log("Key up:", data.keyCode, data.keyName);
});

// Key up: 8 Backspace

OpenCV

The OpenCV class extends the capabilities of the native addon package by providing various image processing functionalities. It allows users to perform operations such as matching templates, blurring images, converting color formats, drawing rectangles, getting image regions, and writing images to files.

Constructor

const image = new  OpenCV(image: string | ImageData)

Creates a new instance of the OpenCV class with the specified image data. The image parameter can be either a file path (string) or an existing ImageData object.

Properties

imageData: ImageData

Holds the underlying image data that will be used for image processing operations.

width: number

Read-only property that returns the width of the image in pixels.

height: number

Read-only property that returns the height of the image in pixels.

Methods

matchTemplate(template: ImageData, method?: number | null, mask?: ImageData): OpenCV

Matches a template image with the current image and returns a new OpenCV instance containing the result.

  • template: ImageData: The template image data to be matched.
  • method?: number | null: (Optional) The matching method to be used. If not provided, the default method will be used.(currently no implemented)
  • mask?: ImageData: (Optional) An optional mask image data to be used during the matching process.
blur(sizeX: number, sizeY: number): OpenCV

Applies a blur filter to the current image and returns a new OpenCV instance containing the blurred result.

  • sizeX: number: The size of the blur kernel in the X direction.
  • sizeY: number: The size of the blur kernel in the Y direction.
bgrToGray(): OpenCV

Converts the current image from the BGR color format to grayscale and returns a new OpenCV instance containing the grayscale result.

drawRectangle(start: Point, end: Point, rgb: Color, thickness: number): OpenCV

Draws a rectangle on the current image and returns a new OpenCV instance containing the modified result.

  • start: Point: The starting point (top-left) of the rectangle.
  • end: Point: The ending point (bottom-right) of the rectangle.
  • rgb: Color: The color of the rectangle in the RGB format (e.g., { r: 255, g: 0, b: 0 } for red).
  • thickness: number: The thickness of the rectangle's border.
getRegion(region: ROI): OpenCV

Extracts a region of interest (ROI) from the current image and returns a new OpenCV instance containing the extracted region.

  • region: ROI: An object specifying the region of interest with properties x, y, width, height.
imwrite(path: string): void

Writes the current image to a file specified by the path.

  • path: string: The file path where the image will be saved.

Functions

FunctionParametersReturn Type
keyDownHandlercallback: (keyCode: number) => voidvoid
keyUpHandlercallback: (keyCode: number) => voidvoid
getWindowDatawindowName: stringWindowData
captureWindowwindowName: string, outputPath: stringvoid
mouseMoveposX: number, posY: numberboolean
mouseClickbutton?: "left" \| "middle" \| "right"boolean
mouseDragstartX: number, startY: number, endX: number, endY: number, speed?: numberboolean
typeStringstringToType: string, delay?: numberboolean
captureWindowNwindowName: stringBuffer

Examples

Here are some examples of using the package:

// Example usage of the OpenCV class
import { OpenCV } from "node-native-win-utils";

const image = new OpenCV("path/to/image.png");

const template = new OpenCV("path/to/template.png");
const matchedImage = image.matchTemplate(template.imageData);

const blurredImage = image.blur(5, 5);

const grayscaleImage = image.bgrToGray();

const regionOfInterest = { x: 100, y: 100, width: 200, height: 150 };
const regionImage = image.getRegion(regionOfInterest);

const redColor = { r: 255, g: 0, b: 0 };
const thickRectangle = image.drawRectangle(
  { x: 50, y: 50 },
  { x: 150, y: 150 },
  redColor,
  3
);

matchedImage.imwrite("output/matched.png");
blurredImage.imwrite("output/blurred.png");
grayscaleImage.imwrite("output/grayscale.png");
regionImage.imwrite("output/region.png");
thickRectangle.imwrite("output/thick_rectangle.png");
// If you want to aply blur and convert to gray then do it that order:
image.blur(5, 5).bgrToGray();
// Otherwise you will get an error.

Please note that the above example demonstrates the usage of different methods available in the OpenCV class. Make sure to replace "path/to/image.png" and "path/to/template.png" with actual image file paths.

import {
  keyDownHandler,
  keyUpHandler,
  getWindowData,
  captureWindow,
  mouseMove,
  mouseClick,
  mouseDrag,
  typeString,
  KeyListener,
} from "node-native-win-utils";

// Register key event handlers

keyDownHandler((keyCode) => {
  console.log("Key down:", keyCode);
});

// Key down: 123

keyUpHandler((keyCode) => {
  console.log("Key up:", keyCode);
});

// Key up: 123

// Retrieve window data

const windowData = getWindowData("My Window");

console.log("Window data:", windowData);

// Window data: { width: 1024, height: 768, x: 100, y: 100 }

// Capture window screenshot

captureWindow("My Window", "output.png");

// Output: output.png with a screenshot of the window

// Move the mouse

mouseMove(100, 200);

// Perform mouse click

mouseClick(); // Left mouse click

mouseClick("right"); // Right mouse click

// Simulate mouse drag

mouseDrag(100, 200, 300, 400);

mouseDrag(100, 200, 300, 400, 100); // Drag with speed 100

// Simulate typing

typeString("Hello, world!");

typeString("Hello, world!", 100); // Type with a delay of 100ms between characters

// Use KeyListener class

const listener = new KeyListener();

listener.on("keyDown", (data) => {
  console.log("Key down:", data.keyCode, data.keyName);
});

// Key down: 8 Backspace

listener.on("keyUp", (data) => {
  console.log("Key up:", data.keyCode, data.keyName);
});

// Key up: 8 Backspace

P.S.: As my knowledge of C++ is just brief, most of the C++ code is written with help of GPT-3.5 and GPT-4

OpenCV License