hotkeys-js icon indicating copy to clipboard operation
hotkeys-js copied to clipboard

Restore default filter (hotkeys disabled in input, textarea and selects)

Open Haytam95 opened this issue 4 years ago • 2 comments
trafficstars

Hi! I'm using:

hotkeys.filter = function(event){
  return true;
}

To enable hotkeys during certain forms.

When I exit those forms, I would like to restore the default filter to avoid triggering hotkeys when the user is on a input, textarea or select.

How can I do that? Is there some function to restore the custom functionality or I should rewrite hotkeys.filter checking the element that has focus?

Thanks

Haytam95 avatar Sep 16 '21 19:09 Haytam95

did you find a solution?

chrisvidal avatar Jul 28 '22 08:07 chrisvidal

Yes, extracted from the source code:

public static defaultHotkeysFilter = function (event) {
       // hotkey is effective only when filter return true
       const target = event.target || event.srcElement;
       const {tagName} = target;
       let flag = true;
       // ignore: isContentEditable === 'true', <input> and <textarea> when readOnly state is false, <select>
       if (target.isContentEditable || ((tagName === 'INPUT' || tagName === 'TEXTAREA' || tagName === 'SELECT') && !target.readOnly)) {
           flag = false;
       }
       return flag;
   };

(For javascript just replace public static with const)

Here is the usage:

const hotkeys = require('/src/assets/js/hotkeys');


hotkeys.filter = defaultHotkeysFilter;

Haytam95 avatar Jul 28 '22 14:07 Haytam95