What’s up everyone? I’m wiring up global keyboard shortcuts for a web app (think cmd+k, /, esc) and I’m trying to do it without wrecking normal typing in inputs, contenteditable, or screen reader flows.
Right now it feels like a tradeoff between “catch everything reliably” (keydown on window + capture + preventDefault) and “don’t be a jerk” (let native behavior through), and I’m worried about edge cases like IME composition, repeated keydown, and shortcuts firing while focus is inside a dialog or embedded widget; what event handling pattern do you use to keep this reliable without accumulating brittle exceptions?
I gate on keydown too, but I treat “global” as “only when the user isn’t actively editing.” So I bail if the target is an input/textarea/select, anything contenteditable, or if the event came from inside a modal/dialog subtree that should own its own keys.
IME is the big one: event.isComposing plus tracking compositionstart/compositionend has saved me from weird “/ opens search while I’m typing Japanese” moments. I’m not sure there’s a perfect universal rule, but I try to only preventDefault() for the handful of shortcuts that truly need to override native behavior (and leave everything else alone).