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

electron-webcontents-shortcut

v1.0.8

Published

register/unregister a keyboard shortcut locally to a webContents instance, using `before-input-event`

Downloads

4

Readme

electron-webcontents-shortcut

Overview

This project is inspired by another project, electron-localshortcut, with a similar implementation principle but enhanced functionality. In comparison to electron-localshortcut, this project offers greater flexibility, allowing users to freely bind shortcuts in the multi-process environment of Electron applications, including BrowserWindow, BrowserView, and Webview.

Background

In electron-localshortcut, shortcuts can only be bound to a BrowserWindow, limiting its use in Electron applications with multiple processes that may involve the use of BrowserView or Webview. To address this limitation, this project relaxes the constraints imposed by electron-localshortcut, enabling users to bind shortcuts to any source of WebContents, thus better accommodating the needs of multi-process applications.

Features

  • Flexibility: Shortcuts can be bound to sources from any WebContents, including BrowserWindow, BrowserView, Webview, and more.

Installation

Install via npm:

npm install electron-webcontents-shortcut

Usage

Import this module into your Electron project:

const shortcutManager = require('electron-webcontents-shortcut');

Then, use the following methods to bind shortcuts:

const { register } = shortcutManager;

app.whenReady().then(() => {
  const win = new BrowserWindow({
    width: 600,
    height: 400,
  });

  win.loadURL('https://www.electronjs.org//');

  register(win.webContents, 'Ctrl+Shift+O', () => {
    console.log('Ctrl+Shift+O');
    win.webContents.openDevTools({ mode: 'detach' });
  });
});

API

1. isAccelerator(input: string): boolean

Checks whether a given string is a valid representation of a keyboard accelerator. A valid accelerator should consist of any number of modifier keys followed by a regular key. For example, Shift+Alt+P.

2. unregisterAll(webContents: WebContents): void

Unregisters all keyboard shortcuts associated with the specified webContents.

3. register(webContents: WebContents, accelerator: string | string[], callback: () => void): void

Registers a keyboard shortcut on the specified webContents. The accelerator parameter can be a string or an array of strings representing the keyboard shortcut(s), and the callback function will be invoked when the shortcut is triggered.

4. unregister(webContents: WebContents, accelerator: string | string[]): void

Unregisters the specified keyboard shortcut(s) associated with the given webContents.

5. isRegistered(webContents: WebContents, accelerator: string): boolean

Checks whether a specific keyboard shortcut is registered on the specified webContents.

Example Usage

const {
  isAccelerator,
  register,
  unregister,
  unregisterAll,
  isRegistered,
} = require('electron-webcontents-shortcut');

// Check if a string is a valid accelerator
if (isAccelerator('Ctrl+A')) {
  const webContents = // ... obtain WebContents instance
    // Register a keyboard shortcut
    register(webContents, 'Ctrl+A', () => {
      // Handle the shortcut
    });

  // Check if a shortcut is registered
  if (isRegistered(webContents, 'Ctrl+A')) {
    // Do something
  }

  // Unregister a shortcut
  unregister(webContents, 'Ctrl+A');

  // Unregister all shortcuts
  unregisterAll(webContents);
}

Contribution

If you encounter any issues or have suggestions for improvement, feel free to raise an issue or submit a pull request. We welcome and appreciate your contributions!

License

This project is licensed under the MIT License. For details, see the LICENSE file.