Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to bypass Quick Search Firefox feature and capture forward slash keypress

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.

like image 359
tridium Avatar asked Oct 17 '11 18:10

tridium


1 Answers

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;
    }
});
like image 152
Jimmy Byrum Avatar answered Nov 06 '22 02:11

Jimmy Byrum