-
Notifications
You must be signed in to change notification settings - Fork 1
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
Prevent hotkeys used when our dialog is open - take 2 #10
Conversation
The latest updates on your projects. Learn more about Vercel for Git ↗︎
|
typescript-eslint/prefer-nullish-coalescing
Thanks, but this approach also block other user defined shortcuts (e.g. |
Hi @PhilETaylor there is simple way how to solve this problem without corrupt import hotkeys from 'hotkeys-js';
hotkeys.filter = (event) => {
return event.target.tagName === 'CMD-DIALOG' ? event.key !== 'a' : true;
}; you can import hotkeys from source and then handle you |
I don't think what Im doing is uncommon - Im just having single char hotkeys like When pressing s or a in the input box of the dialog, hotkeys should be intelligent enough to not trigger - this is a bug in hotkeys not your code, and would best be reported and fixed in hotkeys (but Im guessing they will say they cannot support Lit Elements) I personally believe that no hotkey should trigger while the dialog is shown (this is what github does) - but thats personal choice, you would be having the same issue as me if your demo site did not use the command prefix on the keyboard shortcuts - unfortunately I have almost a decade of muscle memory and 1000s of paying clients using single digit keyboard shortcuts (like Github uses) and so cannot change all these to be prefixed with a command or modifer. Im happy to revert my changes to your code as they are currently broken as you explain - and adding the second approach directly in my own JS code seems to work perfectly for my own needs and architectural decision above. |
According to my research there are both approaches, some cmd dialogs block hotkeys some not |
What your project and Ninja Keys have in common is your examples use modifiers (such as CMD + h) whereas single char keyboard hot keys such as The "problem" is that is I type "bugs" in the input box of the web component (Lit) then on pressing the I was playing this afternoon and remembered why I moved from HotKeys to Mousetrap some years ago - and it was sequential keys (Pressing g and then i for example) - Hotkeys doesnt support that, Mousetrap does There are many issues open in the Hotkeys repo requesting sequential keystroke support but like most of the keyboard navigation repos, they all seem abandoned with little action and little movement forward since release. :( As my app already has sequential keystrokes, as well as an old cmd+k dialog I think Im going to need to fork this project - but thats on the back burner now as I have other code to write first so will come back to this (sorry traveling this week so away from desk) Appreciate your hard work on this - this project alone has taught me about Lit and ts to JS compiling so its been very very worthwhile for this PHP developer :) |
I am decide to switch from HotKeys to tinykeys There are some BC breaks but now you can use single letter inside dialog. Check latest version from master. |
Doesnt seem to work like that - I still get navigated when pressing Sorry :) |
What IS now working is a shortcut containing more than one key |
Sorry I forgot |
This is how |
Hmmm so now I have to write custom handlers for each key stroke to check if its been made in a INPUT box or not? |
Can be handled with single line of code: if (!/TEXTAREA|INPUT|CMD-DIALOG/.test(event.target.tagName)) {
// ... do action
} else {
// .. do nothing
} I can't cover all possibilities. |
ok for me I have added if (event instanceof KeyboardEvent && event.key !== 'Enter' && this.dialog.open){
return;
} in the |
I just added a custom const dialog = document.querySelector('cmd-dialog');
dialog.addEventListener('action', (event) => {
if (
dialog.isOpen &&
event.detail.parentEvent instanceof KeyboardEvent &&
event.detail.parentEvent.key !== 'Enter'
) {
event.preventDefault(); // do not perform action
}
}); |
Thanks - im going to have to come back to all this, I just realised that if I have an action for |
bookmarking jamiebuilds/tinykeys#17 :-( |
This is perfect! dialog.addEventListener('action', (event) => {
if (
dialog.isOpen &&
event.detail.parentEvent instanceof KeyboardEvent &&
event.detail.parentEvent.key !== 'Enter'
) {
event.preventDefault(); // do not perform action
}
if (!dialog.isOpen && ['INPUT', 'TEXTAREA'].includes(event.detail.parentEvent.target.tagName)){
event.preventDefault();
}
}); THANKS! |
Different approach here, using the event data to determine if we are in the dialog and allowing the arrows and tab (shift tab also works as does escape)