Your problem is JavaScript’s strict comparison in switch statements. event.keyCode returns a number (65), but you’re checking against strings (‘65’). Switch statements don’t do type conversion like == does - they use strict equality. So the number 65 never matches the string ‘65’. I ran into this exact issue building hotkeys for a web app and wasted hours debugging before I caught the type mismatch. Just remove the quotes and use case 65: instead of case '65':. This matches the actual keyCode data type and skips unnecessary conversions.
The problem is event.keyCode returns numbers, but you’re comparing them to strings in your switch cases. Switch statements don’t convert types automatically like == does.
I hit this same issue building keyboard shortcuts for one of our internal tools. Instead of wrestling with type conversions and edge cases, I just automated the whole keyboard event handling.
Set up a workflow that captures keyboard events, handles type conversions automatically, then routes key combos to different actions or API calls. You can build complex shortcut schemes that trigger different automations based on modifier keys.
This way you skip JavaScript’s weird type system entirely - define your shortcuts once and let automation handle the messy stuff. Way better than debugging switch statements.
This happens because JavaScript handles switch statements differently than equality operators. Your event.keyCode returns a number, but you’re comparing it against strings like ‘65’ in your cases. The == operator converts types automatically, so 65 matches ‘65’. Switch statements don’t - they work like === with strict comparison. I ran into this same thing building a text editor where my keyboard shortcuts wouldn’t work. Easy fix: either use numbers in your cases (case 65: instead of case '65':), or convert keyCode to a string first with event.keyCode.toString(). I’d go with numbers since you skip the conversion step.
This is JavaScript’s type coercion messing with you. When you use == in the if statement, JS automatically converts the string ‘65’ to match the number 65 from keyCode. But switch statements use strict equality (===) under the hood - no type conversion happens. So event.keyCode returns 65 (number) but you’re checking against ‘65’ (string) in your cases, and they don’t match. Easy fix: either use case 65: with the actual number, or convert keyCode to a string first with String(event.keyCode). I’ve hit this exact bug before and wasted way too much time before realizing it was just a type mismatch.
totally get it! i faced this issue too. your keycode is a number, while you’re checking strings in switch. the if statement uses ==, which auto-converts types, but switch is strict. just use case 65 instead of case ‘65’ and it should work fine!