Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(onKeyStroke): listen to multiple keys #813

Merged
merged 1 commit into from
Oct 7, 2021

Conversation

heyitsarpit
Copy link
Contributor

This PR adds the ability to listen to multiple key for onKeyStroke.
This simply adds a parameter type of string[] so the previous API is still valid and the PR is not a breaking change.

Example:

import { onKeyStroke } from '@vueuse/core'

// Previous API
onKeyStroke('ArrowDown', (e) => {
  e.preventDefault()
})

// Now possible to pass an array of keys
onKeyStroke(['w', 'W', 'ArrowUp'], (e) => {
  e.preventDefault()
})

@antfu antfu changed the title feat(onKeyStroke): Added functionality to listen to multiple keys feat(onKeyStroke): listen to multiple keys Oct 7, 2021
@antfu antfu merged commit 2e14a46 into vueuse:main Oct 7, 2021
@maxrks
Copy link

maxrks commented Oct 9, 2021

How to listen to key with modifier key, i.e. Ctrl + C or Ctrl + Esc?

@heyitsarpit
Copy link
Contributor Author

heyitsarpit commented Oct 9, 2021

How to listen to key with modifier key, i.e. Ctrl + C or Ctrl + Esc?

Hey @maxrks, the function doesn't handled strings with modifier keys, so "Ctrl + C" will not work, there are two work arounds I can think of.

  • Use the event object, onKeyStroke a takes a callback as a parameter which has a KeyboardEvent object as an argument. It contains data on which extra keys were active when the key was pressed. You can see properties like metaKey, ctrlKey altKey etc to indicate whether they were active or not.

    {
      altKey: false,
      ctrlKey: false,
      metaKey: false,
      shiftKey: true,
      bubbles: true,
      cancelBubble: false,
      cancelable: true,
      charCode: 0,
      code: 'KeyW',
      composed: true,
      currentTarget: null,
      defaultPrevented: true,
      detail: 0,
      eventPhase: 0,
      IsComposing: false,
      isTrusted: true,
      key: 'W',
      keyCode: 87,
      layerX: 0,
      layerY: 0,
      location: 0,
      rangeOffset: 0,
      rangeParent: null,
      repeat: false,
      returnValue: false,
      timeStamp: 17335,
      type: 'keydown'
    }
  • Another approach would be to combine usekeymodifier with onKeyStroke to check which key is currently active.

@ivodolenc
Copy link

How to listen to key with modifier key, i.e. Ctrl + C or Ctrl + Esc?

I also came across due to multiple keystrokes at the same time 😀

Here is a simple example:

+ + k

import { onKeyStroke } from '@vueuse/core'

onKeyStroke(e => {
  // Shift + Command + k
  if (e.shiftKey && e.metaKey && e.key === 'k') {
    e.preventDefault()

    console.log('Multiple keys are pressed!')
  }
})
// Logs each keystroke
onKeyStroke(e => {
  console.log(e)
})

Btw awesome work! Cheers 👋🏻

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants