I'm capturing the key press value of '191' for the forward slash (/) for a feature on my site. Works fine on every browser except Firefox due to its Quick Search feature. The '191' still registers and the action is executed (focus on an input field, popup help text), but the focus goes to the Quick Search.
I read in another StackOverflow question saying that Firefox captures the forward slash as character code '0', but that didn't do anything.
Is there a way I can ignore the Firefox Quick Search and get control of the forward slash back? Using JavaScript and jQuery.
I agree it's important to question whether you should be using that shortcut. However, if you decide to (as others have- that's the search shortcut in gmail as well), you just need to capture the document keydown event (not keypress or keyup) and then prevent the default action, which will intercept in time to stop the default firefox behavior. Also, be sure to check that the user isn't already typing in a text field. Here's a quick example:
$(document).keydown(function(e) {
var _target = $(e.target);
var _focused = $(document.activeElement);
var _inputting = _focused.get(0).tagName.toLowerCase()==="textarea" || _focused.get(0).tagName.toLowerCase()==="input";
// / (forward slash) key = search
if (!_inputting && e.keyCode===191) {
e.preventDefault();
$("#search-input").focus();
return;
}
});
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With