Last active
February 27, 2017 23:07
-
-
Save bayleedev/643dbc656da7c3c4ca050a7d8ed0a222 to your computer and use it in GitHub Desktop.
Mousetrap will only bind a key event to one function, this makes it so you can bind it to as many as you like.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class MousetrapProxy { | |
constructor () { | |
this.events = {} | |
} | |
emit (key) { | |
if (!this.events[key]) return | |
this.events[key].forEach((cb) => cb()) | |
} | |
bind (keys, cb) { | |
const keysArray = typeof keys === 'string' ? [keys] : keys | |
keysArray.forEach((key) => { | |
if (!this.events[key]) { | |
this.events[key] = [] | |
Mousetrap.bind(key, () => { | |
this.emit(key) | |
}) | |
} | |
this.events[key].push(cb) | |
}) | |
} | |
} | |
var ee = new MousetrapProxy(); | |
ee.bind(['command+shift+k', 'up'], function (e) { | |
console.log('meow') | |
}) | |
ee.bind('command+shift+k', function (e) { | |
console.log('ruff') | |
}) |
Author
bayleedev
commented
Feb 21, 2017
•
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment