mousetrap icon indicating copy to clipboard operation
mousetrap copied to clipboard

Capture all keys after a combination

Open tifDev opened this issue 5 years ago • 2 comments

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?

tifDev avatar Dec 11 '18 13:12 tifDev

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)
    }
  })
})

mxweaver avatar Jan 08 '19 16:01 mxweaver

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)

mxweaver avatar Jan 08 '19 17:01 mxweaver