Skip to content

Commit 34744fc

Browse files
committed
skip hotkey execution if user is in an input area
1 parent 6801170 commit 34744fc

File tree

1 file changed

+33
-9
lines changed

1 file changed

+33
-9
lines changed

packages/common-ui/src/utils/SkldrMouseTrap.ts

Lines changed: 33 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,21 @@ export interface HotKeyMetaData {
1111
hotkey: string | string[];
1212
}
1313

14+
/**
15+
* Checks if focus is currently on an input element where hotkeys should be ignored
16+
* @returns true if hotkeys should be ignored, false otherwise
17+
*/
18+
function inputElementIsFocused(): boolean {
19+
const activeElement = document.activeElement;
20+
return (
21+
activeElement instanceof HTMLElement &&
22+
(activeElement.tagName === 'INPUT' ||
23+
activeElement.tagName === 'TEXTAREA' ||
24+
activeElement.tagName === 'SELECT' ||
25+
activeElement.isContentEditable)
26+
);
27+
}
28+
1429
export class SkldrMouseTrap {
1530
private static _instance: SkldrMouseTrap;
1631

@@ -45,7 +60,13 @@ export class SkldrMouseTrap {
4560
// Bind each hotkey
4661
hotkeys.forEach((k) => {
4762
Mousetrap.bindGlobal(k.hotkey, (a, b) => {
48-
console.log(`Running ${k.hotkey}`);
63+
// Skip execution if focus is on input elements
64+
if (inputElementIsFocused()) {
65+
console.log(`Ignoring hotkey ${k.hotkey} while input element is focused`);
66+
return;
67+
}
68+
69+
// console.log(`Running ${k.hotkey}`);
4970
k.callback(a, b);
5071
});
5172
});
@@ -58,26 +79,29 @@ export class SkldrMouseTrap {
5879
public static removeBinding(hotkey: string | string[] | Array<string | string[]>) {
5980
const instance = SkldrMouseTrap.instance();
6081
const currentHotkeys = [...instance.hotkeys];
61-
62-
if (Array.isArray(hotkey) && !hotkey.every(k => typeof k === 'string' || typeof k === 'number')) {
82+
83+
if (
84+
Array.isArray(hotkey) &&
85+
!hotkey.every((k) => typeof k === 'string' || typeof k === 'number')
86+
) {
6387
// If it's an array of hotkey specifiers
64-
hotkey.forEach(key => {
88+
hotkey.forEach((key) => {
6589
// Remove from internal registry
66-
instance.hotkeys = instance.hotkeys.filter(k => {
90+
instance.hotkeys = instance.hotkeys.filter((k) => {
6791
return JSON.stringify(k.hotkey) !== JSON.stringify(key);
6892
});
69-
93+
7094
// Unbind from Mousetrap
7195
Mousetrap.unbind(key);
7296
});
7397
} else {
7498
// Single hotkey removal (original implementation)
7599
// Remove from internal registry
76-
instance.hotkeys = currentHotkeys.filter(k => {
100+
instance.hotkeys = currentHotkeys.filter((k) => {
77101
// Convert both to JSON for comparison to handle arrays correctly
78102
return JSON.stringify(k.hotkey) !== JSON.stringify(hotkey);
79103
});
80-
104+
81105
// Unbind from Mousetrap
82106
Mousetrap.unbind(hotkey);
83107
}
@@ -90,7 +114,7 @@ export class SkldrMouseTrap {
90114
public static reset() {
91115
console.warn(
92116
'SkldrMouseTrap.reset() may affect hotkeys registered by other components. ' +
93-
'Consider using removeBinding() with specific hotkeys for better component isolation.'
117+
'Consider using removeBinding() with specific hotkeys for better component isolation.'
94118
);
95119
Mousetrap.reset();
96120
SkldrMouseTrap.instance().mouseTrap.reset();

0 commit comments

Comments
 (0)