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

keyboard-simulator

v0.0.2

Published

Simulate key presess.

Downloads

77

Readme

Build Status License: MIT

Keyboard Simulator

Simulate key presess.

Currently only supports EN-US keyboard layout.

The key point of this library is mimicking a real keyboard so you can't press a key that is already pressed down and you cannot release a key that is not pressed down. Holding the Shift key makes the following key presses generate the alternative values for the relevant keys (pressing 2 while shift is down generates @ instead).

CapsLock and NumLock that when are ON generate alternative values are not supported yet in a similar way.

Also, holding a modifier key down (Control, Alt, Shift, Meta) updates following events accordingly (e.g. ev.ctrlKey = true).

TODO: add supported key list

Install

$ npm install keyboard-simulator

Basic Usage

import {KeyboardSimulator} from 'keyboard-simulator`;

const kbSim = new KeyboardSimulator();

kbSim.keyDown('A');
kbSim.keyUp('A');

kbSim.keyPress('B');

API

Constructor

const kbSim = new KeyboardSimulator(contextElement = document);

The context element is the element that dispatches the following keyboard events. The default is document. Returns a KeyboardSimulator instance that has the following methods:

  • .keyDown()
  • .keyUp()
  • .keyPress()
  • .holdKey()
  • .releaseAll()
  • .setContextElement()
  • .reset()

.keyDown(...keys)

Dispatches one or more keydown events of given keys. Returns a boolean (or an array of booleans if passed in multiple keys), which is the result of .dispatchEvent(). MDN dispatchEvent docs.

kbSim.keyDown('A');
kbSim.keyDown('X', 'Y', 'Z');

The instance tries to simulate a real physical keyboard so when a key is already pressed down, trying to press it again throws an error:

kbSim.keyDown('A');
kbSim.keyDown('A'); // ERROR

.keyUp(...keys)

Dispatches one or more keyup events of given keys. Returns a boolean (or an array of booleans if passed in multiple keys), which is the result of .dispatchEvent(). MDN dispatchEvent docs.

kbSim.keyUp('A');
kbSim.keyUp('X', 'Y', 'Z');

The instance tries to simulate a real physical keyboard so when a key is not pressed down, trying to release it with .keyUp() throws an error:

kbSim.keyUp('A');
kbSim.keyUp('A'); // ERROR

.keyPress(...keys)

Dispatches keydown followed by keyup events for each given key. Returns an array of two booleans (per key, one for the keydown event and one for keyup) or an array of array of two booleans if passed in multiple keys.

kbSim.keyPress('A');
kbSim.keyPress('A', 'B', 'C');

.holdKey(key, repeatCount)

Simulates holding a key down by dispatching multiple keydown events for the same key with the repeat property set to true. Returns a boolean, which is the result of .dspatchEvent docs). MDN dispatchEvent.

kbSim.keyDown('A', 'B', 'C');
kbSim.holdKey('C', 3);

.releaseAll()

Dispatches keyup events for all the keys that are pressed down in a reversed order of which they were pressed down (first down = last to be released)

kbSim.keyDown('A', 'B', 'C');
kbSim.releaseAll(); // keyup C, B, A

.setContextElm(HTMLElement)

Sets a new context element to dispatch following events.

const kbSim = new KeyboardSimulator(); // `document` is the default

kbSim.keyPress('A'); // document.dispatchEvent()
kbSim.setContextElm(myDiv);
kbSim.keyPress('A'); // myDiv.dispatchEvent()

.reset()

The instance keeps track of pressed keys. Calling .reset() clears the records but does not change the context element.

kbSim.keyDown('A');
kbSim.keyDown('A'); // ERROR - key 'A' is already pressed down
kbSim.reset();
kbSim.keyDown('A'); // OK