Security News
Node.js EOL Versions CVE Dubbed the "Worst CVE of the Year" by Security Experts
Critics call the Node.js EOL CVE a misuse of the system, sparking debate over CVE standards and the growing noise in vulnerability databases.
ember-keyboard-service
Advanced tools
The keyboard service helps you let your app respond to keyboard input.
You can either do this declaratively with a DSL mixin, or use the lower level service, that gives you more control.
ember install ember-keyboard-service
You can specify normal key characters by using the literal value. Examples:
'a'
, '$'
, ' '
.
You can add modifier keys using 'ctrl'
, 'alt'
, 'shift'
or 'meta'
.
'cmd'
is an alias for 'meta'
, 'option'
is an alias for 'alt'
.
An example of a combo with modifiers: 'ctrl+v'
.
There is also a special modifier: 'nctrl'
, on OS X this modifier is an alias
for 'cmd'
, on any other system it is just 'ctrl'
.
Use it do declare shortcuts in a simple manner.
import KeyboardMixin from 'ember-keyboard-service/mixins/keyboard';
Ember.Object.extend(KeyboardMixin, {
keyboardHandlers: [
{ key: 'ctrl+x', handler: 'cut' }
{ key: 'ctrl+c', handler: 'copy' }
{ key: 'ctrl+v', handler: 'paste' }
],
cut() {
console.log("every day i'm cuttin");
},
copy() {
console.log("every day i'm copyin");
},
paste() {
console.log("every day i'm pastin");
}
});
You can also specify static arguments for keyboard handlers:
Ember.Object.extend(KeyboardMixin, {
keyboardHandlers: [
{ key: 'ctrl+g', handler: 'goto', arguments: [42] }
],
goto(e, line) {
console.log(`going to line ${line}`);
}
});
You can choose to bind multiple key shortcuts to the same handler:
keyboardHandlers: [
{ key: ['a', 'b'], handler: 'doStuff' }
]
You can use some of the Ember run loop features:
keyboardHandlers: [
// debounces key handlers by 30ms
{ key: 'a', handler: 'debouncedHandler', debounce: 30 },
// throttles key handlers by 30ms
{ key: 'b', handler: 'throttledHandler', throttle: 30 },
// only calls the handler once every run loop
{ key: 'c', handler: 'scheduleOnceHandler', scheduleOnce: true }
]
For more usage examples you can check out the tests
Use Ember.inject.service
to inject the service onto your Ember object.
Ember.Object.extend({
keyboard: service()
});
Then use listenFor
to start listening for keyboard events:
(The key names are equal to those used for KeyboardEvent.key
.)
this.get('keyboard').listenFor('x', this, eventHandler);
You can alternatively pass a function name instead of an eventHandler:
this.get('keyboard').listenFor('x', this, 'xEventHandler');
You can optionally specify modifier keys:
// possible modifiers are: ctrl, cmd, alt, shift
this.get('keyboard').listenFor('ctrl+alt+Delete', this, eventHandler);
You can listen for a key stroke once:
this.get('keyboard').listenForOnce('x', this, eventHandler);
You can stop listening for key strokes, you must supply the exact same
arguments as you did to listenFor
.
this.get('keyboard').stopListeningFor('x', this, eventHandler);
The service will not handle the event if the even target was an input or similar element. To override this you can do:
this.get('keyboard').listenFor('x', this, eventHandler, {
actOnInputElement: true
});
For more usage examples you can check out the tests
FAQs
A keyboard handling service for Ember apps
The npm package ember-keyboard-service receives a total of 10 weekly downloads. As such, ember-keyboard-service popularity was classified as not popular.
We found that ember-keyboard-service demonstrated a not healthy version release cadence and project activity because the last version was released a year ago. It has 2 open source maintainers collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Security News
Critics call the Node.js EOL CVE a misuse of the system, sparking debate over CVE standards and the growing noise in vulnerability databases.
Security News
cURL and Go security teams are publicly rejecting CVSS as flawed for assessing vulnerabilities and are calling for more accurate, context-aware approaches.
Security News
Bun 1.2 enhances its JavaScript runtime with 90% Node.js compatibility, built-in S3 and Postgres support, HTML Imports, and faster, cloud-first performance.