mousetrap
mousetrap copied to clipboard
Capture all keys after a combination
I want to allow users to add comments after hitting 'a' then 'r' then capture the comment/all keydown and then stop when click on 'enter' like:
Mousetrap.bind( 'a r [capture all keydown/comment] enter', function( e ) {
e.preventDefault();
var comment = [all keys captured after hitting the combination 'a r'];
} );
How can i achieve this?
You could use this library to trigger your own "recorder". For example:
Mousetrap.bind('a r', e => {
e.preventDefault()
const keys = []
const listener = document.addEventListener('keypress', ({ key }) => {
if (key === 'Enter') {
document.removeEventListener('keypress', listener)
console.log(keys) // do something with keys
} else {
keys.push(e.key)
}
})
})
By the way, you can color your code blocks in github comments (or any other markdown) by adding a file extension after the opening ```. For example:
'''js
const message = 'Hello, world!'
console.log(message)
'''
will produce
const message = 'Hello, world!'
console.log(message)