Skip to content Skip to sidebar Skip to footer

Javascript -> Hotkeys -> Disable For Input Fields

okay so I have the hotkey working just can't make it stop $(document).keypress(function(e){ if(e.which == 13){ //Enter key is press do what you want } else i

Solution 1:

I understand, that since you attach your event listener to the document object, all input accepting elements, such as textfields, selects, etc. will handle hotkeys, hence lose their normal behavior.

Take a look at line 44 in the jquery.hotkeys plugin. It excludes all input-accepting elements on initialization.

P.S. Maybe this plugin is useful as a whole for your task.

The key is to check, whether an event comes from a text-accepting input.

# only bind event to text-accepting elements, if they have been# explicitly selected# if your event variable happens to be called e, please adjust accordinglyif ( this !== event.target && 
    ( /textarea|select/i.test( event.target.nodeName ) ||
      event.target.type === "text") ) {
    return;
}

As your code stands now, you would need to insert this snippet at the beginning of the anonymous function, you bind to the keypress event.

Solution 2:

Seems to be working just fine :)

example: First example: http://jsfiddle.net/HenryGarle/SG5Um/ Second example: http://jsfiddle.net/HenryGarle/SG5Um/1/

New code:

$(document).keypress(function(e){

   if(e.which == 13){
        alert("Enter");
   }
   else if(e.which == 67 || e.which == 99){
        alert("c");
       //window.location = 'whateveryouwant';
   }
    else if(e.which == 32){
        alert("Space pressed");
    }

});

$("input.registerform.bgcolor2").live('keypress', function(e){
    alert("Stopped");
    e.stopPropagation();
});

Stops:
<input class="registerform bgcolor2" type="text">
<br>
Does not stop:
<input class="registerform" type="text">

Using this anything with ONLY registerform will act as normal but if it ALSO has bgcolor2 it will stop the event.

Post a Comment for "Javascript -> Hotkeys -> Disable For Input Fields"