npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

@xitanggg/node-insert-text

v1.5.1

Published

Insert text on desktop in Node.js via Node-API native addon

Downloads

16

Readme

Node Insert Text

This package provides a simple Node.js util that allows you to programmatically insert text on desktop.

  • Support Windows and Mac
    • Mac: Need to grant accessibility permission to the calling app (Settings -> Privacy & Security -> Accessibility) to call Apple's Core Graphics framework to post CGEvent to insert text
  • Require Node.js >= 10

📦Installation

npm i @xitanggg/node-insert-text

ℹ️Usage

import { insertText } from '@xitanggg/node-insert-text';

insertText('👋Hello World! This line is inserted programmatically🤖');

insertText accepts 3 arguments

  1. text - Text to be inserted
  2. insertWithPaste - An optional boolean that sets whether to insert text with the paste method. Default to false. (Setting true to use the paste method is useful to bypass some limitations in the default insert method. For example, the default insert method may not work for some apps, and in Mac, it doesn't work when certain key, such as Cmd, is pressed during insert.)
  3. arrowKeyToClickBeforeInsert - An optional string that sets which arrow key to click before inserting text. Can be either "left" or "right". Default to None.

💡Implementation

Core Logic

The implementation is written in Rust and is ~200 lines of code in a single file /src/lib.rs.

The happy path is a wrapper of the text function of enigo, which is a cross platform input simulation library in Rust. The default behavior simply calls enigo.text to perform text insertion, which works for most use cases.

However, enigo.text has its limitations: 1. it may not work for some apps, and 2. In Mac, it doesn't work when certain key, such as Cmd, is pressed during insert (issue link). Therefore, an alternative option insertWithPaste is provided to insert text via clipboard paste in a 5 steps process:

  1. Save clipboard existing text or image
  2. Clear clipboard
  3. Set text to be inserted to clipboard
  4. Simulate Ctrl + V (Cmd + V in Mac) keyboard input to paste text from clipboard
  5. Restore clipboard previous text or image to minimize side effects to users

Dependencies

It uses Arboard (Arthur's Clipboard) to perform clipboard operation and enigo to perform keyboard input simulation.

Arboard supports getting and setting clipboard text and image, which should satisfy most use cases. But it is worth noting that it doesn't support other clipboard contents, e.g. html, rtf, file. A paste method is provided for those who would like to implement custom logics to save and restore clipboard state or just want to call Ctrl + V (Cmd + V in Mac) to paste.

import { paste } from '@xitanggg/node-insert-text';

// Skip custom logics to save clipboard state and write text to clipboard
paste();
// Skip custom logics to restore clipboard state

Build & Distribution

It uses NAPI-RS to compile the library into binaries via GitHub actions, package it as a Node-API native addon, and then publish it to npm for distribution and easy use.

One very nice thing about the NAPI-RS tooling is that the binary has been built, so this package just works after installation, i.e. no need to build it yourself. Also, the binary is selectively installed, meaning installation only installs the binary that your system needs, e.g. windows or Mac, to keep the size small instead of including all binaries at once.