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

Avoid keyUp events from other tabs #61

Open
oskarrough opened this issue Apr 10, 2017 · 1 comment
Open

Avoid keyUp events from other tabs #61

oskarrough opened this issue Apr 10, 2017 · 1 comment

Comments

@oskarrough
Copy link

Hi, my application has a shortcut on the 'w' key on keyUp.

If I land on my application in a browser tab after using the default super+w to close another tab, the shortcut will fire without the user intending to.

So my question is, is there a way to avoid keyUp events that were started (keyDown) in another tab?

@briarsweetbriar
Copy link
Collaborator

Not built into ember-keyboard itself, though you could (kind of) track this locally. For instance, something like:

registerKeyDown: Ember.on(keyDown('KeyW', function() {
  this.set('keyWPressed', true);
}),

doSomething: Ember.on(keyUp('KeyW', function() {
  if (this.get('keyWPressed') {
    this.toggleProperty('keyWPressed');
    . . . .
  }
})

The problem is, this could be painful to support if you have to do it for a lot of keys. It also still buggy. What if the user tabs away with super+w (registering the keyDown event) and then tabs back in the same way? You'd have the same problem.

You could maybe work around that by also watching for visibilitychange events. So something like:

setupVisibilityChangeObserver: Ember.on('init', function() {
  document.addEventListener('visibilitychange', () => {
    this.set('visibilityChange', true);
  });
},

registerKeyDown: Ember.on(keyDown('KeyW', function() {
  this.set('keyWPressed', true);
  this.set('visibilityChange', false);
}),

doSomething: Ember.on(keyUp('KeyW', function() {
  if (this.get('keyWPressed') && !this.get('visibilityChange')) {
    this.toggleProperty('keyWPressed');
    . . . .
  }
})

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

No branches or pull requests

2 participants