jaywcjlove / hotkeys-js

➷ A robust Javascript library for capturing keyboard input. It has no dependencies.
https://jaywcjlove.github.io/hotkeys-js
MIT License
6.61k stars 406 forks source link

Allow default filter to work within ShadowDOM boundaries #398

Open diervo opened 1 year ago

diervo commented 1 year ago

I modified the filter function to work within Shadow boundaries.

Would the maintainer's team be interested in adding this? Happy to do a PR to port it over.

sllvn commented 1 year ago

I also encountered this and would appreciate it as part of library so that hotkeys "just works" for web components. For now, I was able to override the default filter by replacing event.target.tagName with event.composedPath()[0].tagName, a la:

hotkeys.filter = (event) => {
  const target = event.target || event.srcElement;
  const tagName = event.composedPath()[0].tagName // this is the change from default filter
  let flag = true;
  if (
    target.isContentEditable
    || ((tagName === 'INPUT' || tagName === 'TEXTAREA' || tagName === 'SELECT') && !target.readOnly)
  ) {
    flag = false;
  }
  return flag;
}

composedPath docs

Thanks for the great library, @jaywcjlove