The Problem
Browser extensions (password managers, ad blockers, grammar checkers, etc.) can intercept keyboard events before they reach your web application. This causes keyboard shortcuts to fail silently or behave unexpectedly.
Symptoms
- Keyboard shortcuts work on some computers but not others
- Ctrl+S, Ctrl+F, or other shortcuts trigger browser/extension behavior
- Keydown event fires but the key is not detected correctly
- Works in incognito mode but not regular browsing
- Users report shortcuts "not working" randomly
Common Conflicts
- Grammarly intercepts all keyboard input
- Password managers intercept Ctrl+S, Ctrl+F
- Ad blockers block certain key combinations
- Translation extensions intercept text input
- Dark mode extensions modify key behavior
Real Error Scenario
// Your app's shortcut
document.addEventListener('keydown', (e) => {
if (e.ctrlKey && e.key === 's') {
e.preventDefault();
saveDocument(); // Never fires because extension intercepted it
}
});How to Fix It
Fix 1: Use capture Phase
// Listen in capture phase (before extensions can intercept)
document.addEventListener('keydown', (e) => {
if (e.ctrlKey && e.key === 's') {
e.preventDefault();
saveDocument();
}
}, true); // capture: trueFix 2: Avoid Common Browser Shortcuts
```javascript // AVOID these (conflict with browser/extension shortcuts): // Ctrl+S, Ctrl+F, Ctrl+P, Ctrl+T, Ctrl+W, Ctrl+N
// USE these instead: const SHORTCUTS = { save: { key: 's', ctrl: true, shift: true }, // Ctrl+Shift+S search: { key: 'k', ctrl: true }, // Ctrl+K (common pattern) print: { key: 'p', ctrl: true, shift: true }, // Ctrl+Shift+P newFile: { key: 'n', ctrl: true, shift: true }, // Ctrl+Shift+N };
document.addEventListener('keydown', (e) => { Object.entries(SHORTCUTS).forEach(([action, shortcut]) => { if ( e.key.toLowerCase() === shortcut.key && e.ctrlKey === shortcut.ctrl && e.shiftKey === shortcut.shift ) { e.preventDefault(); executeAction(action); } }); }); ```
Fix 3: Detect Extension Interference
```javascript function detectExtensionInterference() { const issues = [];
// Check for Grammarly if (document.querySelector('[data-grammarly-shadow-root]')) { issues.push('Grammarly detected'); }
// Check for LastPass if (document.querySelector('script[src*="lastpass"]')) { issues.push('LastPass detected'); }
if (issues.length > 0) { console.warn('Extensions detected that may interfere with keyboard shortcuts:', issues); } }
detectExtensionInterference(); ```
Fix 4: Provide Alternative Input Methods
```html <!-- Always provide clickable alternatives to keyboard shortcuts --> <button onclick="saveDocument()" title="Save (Ctrl+Shift+S)"> <svg>...</svg> Save </button>
<!-- Show shortcuts in UI --> <div class="shortcut-hint"> <kbd>Ctrl</kbd>+<kbd>Shift</kbd>+<kbd>S</kbd> </div> ```
Fix 5: Inform Users About Conflicts
```javascript // Show a warning if shortcuts are not working let shortcutTestPassed = false;
document.addEventListener('keydown', (e) => { if (e.key === 'F12') { // Unlikely to be intercepted shortcutTestPassed = true; } if (e.ctrlKey && e.key === 's' && !shortcutTestPassed) { showNotification('Keyboard shortcuts may be blocked by a browser extension. Try using the Save button instead.'); } }); ```