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-shortcut-loader

v0.4.0

Published

Loading pre-defined shortcuts with custom events and more options

Downloads

15

Readme

electron-shortcut-loader

Loading pre-defined shortcuts with custom events and more options

Install

$ npm install --save electron-shortcut-loader

Usage

ShortcutLoader can load pre-defined shortcuts data from a javascript file with module.exports in node

'use strict';

module.exports = {
	'Command+A': {
		event: 'select-all'
	},
	'Cmd+Alt+I': {
		event: 'show-devtool'
	},
	'CommandOrControl+?': {
		event: 'focus'
	},
	'Command+?': {
		event: 'focus'
	},
	'Command+J': {
		event: () => {
			console.log('You can register individual event');
		}
	}
};

, or a JSON object in code.

const ShortcutLoader = require('ShortcutLoader');

ShortcutLoader.load({
	'Command+A': {
		event: 'select-all'
	},
	'Cmd+Alt+I': {
		event: 'show-devtool'
	},
});

It supports two types of API, static and instance methods that allow you to manage shortcuts on global and local. For global, the shortcut loader manage the one instances dealing with static method and on the other hand, You can manage multiple set of shortcuts depending on your purpose. For example, it could be possible that create each shortcut loader per browser window.

Let see how to use static method on global mode.

const ShortcutLoader = require('electron-shortcut-loader');

ShortcutLoader.load('./shortcuts', {
	autoRegister: false,
	cmdOrCtrl: true,
}, app);

// register shortcuts in manually
app.on('ready', function () {
	shortcuts.register();
});

app.on('will-quit', function () {
	shortcuts.unregister();
});

// 'shortcut-pressed' is custom event named from Shortcut Loader
app.on('shortcut-pressed', function (e) {
	console.log(e.shortcut, e.event, 'key-event has been fired');
});

, or You can create a instance by hand to create and manage multiple shortcut set.

const loader = new ShortcutLoader();

// with common anonymous function
loader.load('./shortcuts', {
	autoRegister: false,
	cmdOrCtrl: true,
}, (e) =>
	console.log(e.shortcut, e.event, 'key-event has been fired');
});

// You can use `app`, the instance of EventEmitter, as a event handler.
// See demo/index.js for further information.
const loaderWithApp = new ShortcutLoader();
loader.load('./shortcuts2', {
	autoRegister: true,
	cmdOrCtrl: true,
}, app);

// events, registered with loaderWithApp, will be fired with `shortcut-pressed` event through to `app`
app.on('shortcut-pressed', function (e) {
	console.log(e.shortcut, e.event, 'key-event has been fired');
});

API

ShortcutLoader.load, un/register

Static methods for ShortcutLoader. see below information of APIs for further information.

ShortcutLoader()

constructor of ShortcutLoader.

load(input, [options], handler)

Load shortcuts coming from a file or object.

input

Type: string or object

A path of shortcuts file or JSON data which has the set of shortcuts as JSON data, will be registered by register method. the path will be resolved with path of current module willing to import shortcuts. See more detailed structure of JSON data.

options

Will be delivered to each shortcut created by electron-shortcut. See further information of options.

handler

Event handler, the instance of EventEmitter such as app or anonymous function. If is not set, app is the default event handler and shortcut-pressed is the pre-defined name for event handler.

register

shortcuts will be registered.

unregister

shortcuts will be unregistered.

Pre-defined shortcut data

It should consist of accelerator name and event handler. the accelerator name is the key and event handler could be the name of event or anonymous function for an individual event. not allows extra options in a event.

  • event: event name which will be fired to the instance of EventEmitter such as app or through to anonymous function. If is not set? will be fired by the name of accelerator
// predefine shortcuts with events and options in shortcuts.js
'use strict';
module.exports = {
	'Command+A': {
		event: 'select-all'
	},
	'Cmd+Alt+I': {},
	'CommandOrControl+?': {
		event: e => {}
	}
};

Event

When a specific shortcut key has been pressed, event object pass through to the event handler. For EventEmitter event handler, shortcut-pressed is the pre-defined name of the event and you can use the custom event name. If you set anonymous function at event, the shortcut name will be used as the name of the event and both of property of event, shortcut and event will be same.

ShortcutLoader.load('Command+A', e => {
	console.log(
		e.shortcut, // the name of accelerator
		e.event // the name of accelerator
	)
});

app.on('shortcut-pressed', e => {
	console.log(
		e.shortcut, // the name of accelerator
		e.event // the custom name for the event or accelerator
	);
})

Run demo

$ npm install && npm start

License

MIT © ragingwind