madrobby / keymaster

A simple micro-library for defining and dispatching keyboard shortcuts. It has no dependencies.
MIT License
6.52k stars 475 forks source link

Also filter out keypresses when focused on contenteditable elements #45

Open crittermike opened 12 years ago

crittermike commented 12 years ago

I just overrode key.filter() to also ignore keypresses when the current element is contenteditable, using this code:

key.filter = function(event) {
  var tagName = (event.target || event.srcElement).tagName;
  var editable = (event.target || event.srcElement).getAttribute('contenteditable');
  return !(tagName == 'INPUT' || tagName == 'SELECT' || tagName == 'TEXTAREA' || editable);
}

I'm not sure if that's the best way to implement it, but it seems like this would be a good default, no?

davidchambers commented 12 years ago

The element's isContentEditable property is possibly a better source of information, though I don't know how widely supported it is.

key.filter = function(event) {
  var el = event.target || event.srcElement;
  return !(el.isContentEditable || el.tagName == 'INPUT' ||
           el.tagName == 'SELECT' || el.tagName == 'TEXTAREA');
}
focusaurus commented 11 years ago

+1

davidchambers commented 11 years ago

Would you accept a pull request for this change, @madrobby?

ajb commented 9 years ago

:+1: is there any reason this has been open for a year and half? I'm happy to PR if that's all it needs.

madrobby commented 9 years ago

@ajb not everyone needs this, that's why the filter function is easily overridable. I'd merge a PR with extensive tests tho. It needs detection of the browser supports isContentEditable, I have no idea about how widespread support is. Remember that kemaster should work on older browsers.

ajb commented 9 years ago

Thanks -- to your credit, key.filter is very-well documented :grinning:. I'll see if I can whip something together...

manoharank commented 9 years ago

@madrobby Awesome library so far. :+1: to add contenteditable elements in the default filter.