2

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 if(e.which == 67 || e.which == 99){
      //C key is press do what you want

      window.location.href = "/html/credits.php";

   }
    else if(e.which == 32){
        alert("Space pressed");

    }

    });

     $("input.registerform").keypress(function(e){

     e.stopPropagation(); });

Here is what I have to make it stop, the class of my input form is "registerform bgcolor2" but it wont work with either "input.registerform" neither with "input.registerform bgcolor2" I tried adding an ID to it with registerform as ID didn't work either :/

Is it being caused my AJAX? or am I missing something here?

(Sorry I reposted this just made a new account and cant find my old question back >.<)

SergeS
  • 11,533
  • 3
  • 29
  • 35
Sjaak piraat
  • 43
  • 1
  • 6

2 Answers2

4

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 accordingly
if ( 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.

miku
  • 181,842
  • 47
  • 306
  • 310
  • So what exactly is your suggestion, remove the code I have, take jquery.hotkeys? And how will I configure this correctly, sorry, javascript is something I'm fairly new to. – Sjaak piraat Aug 11 '11 at 11:17
  • Yes, I'd suggest you look into jquery.hotkeys.js. Take a look at the project's homepage on github - it has a short README with examples (plus more examples in the project itself). – miku Aug 11 '11 at 11:19
  • Well the script I have now is something I completely understand and the redirect is working perfectly just want to "deactive" it in text fields / input fields, when I see the jquery script to do this I'm getting totally lost and have no idea where to even start. – Sjaak piraat Aug 11 '11 at 11:24
  • @Sjaak, I understand you concern, but the hotkeys plugin is not more than 81 sloc - it's not complex but a proven and probably more robust snippet of code than some reinvented solution. – miku Aug 11 '11 at 11:28
  • Well can you maybe give a general diretion on how to use this jquery hotkeys? I already have jquery 1.4.2 I'm supose to download the hotkeys, add that, and then what? – Sjaak piraat Aug 11 '11 at 11:29
  • @Sjaak, it's all good. I could not give better examples than the ones provided by the project. If you think it's overwhelming, maybe just step back for a minute and learn a bit about javascript. I might think that this would be no lost time. Good luck. – miku Aug 11 '11 at 11:32
  • I get what you mean but reading up about it in General will take me months to understand it, a little guidance to what I do will be easier to help me understand it in General, and this so far hasn't really been helpful at all. And certainly didn't fix the problem I'm experiencing. Its like going to a car dealer cause you have broken brakes and he tells you to just get a new car cause it'll work better. – Sjaak piraat Aug 11 '11 at 11:36
  • @Sjaak, Months? A few hours, maybe days. Javascript is very much about events, so you can read a lot about it. If you are pressed for time and programming isn't your job, then I understand your position - otherwise - not really. Btw. I updated my answer with code, you should be able to just copy and paste :) – miku Aug 11 '11 at 11:46
  • Yeah coding isn't what I do for job, its something I do in my sparetime. I started out not to long ago hence why I think it'll take months to understand it all not just this specific part. so if I copy paste this, and put it within the file where my inputs are, it should work? what about the hotkey settings aren't they different or can I keep the current code I posted above? – Sjaak piraat Aug 11 '11 at 11:53
0

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.

Henry
  • 2,187
  • 1
  • 15
  • 28
  • I'm doing a redirect not an Alert, secondly my class = class="registerform bgcolor2" and thirtly, I dont have any javascript in the page where I have my inputs, I have the javascript in a different file and I'm retrieving it with the command – Sjaak piraat Aug 11 '11 at 12:09
  • 1
    You can replace the alert with what ever you want .. I dont see how thats a problem. As for the class, it has registerform so it will pick up on it, you can add a second selector (Which I will amend my answer with) and as for not having it in the same file you can use a live event which I will also update inside my answer. These are all fairly basic usages of not only jQuery but javascript and programming in general. I really think you should go and read up on the documentation, this isnt a tutorial site its a technical help site. – Henry Aug 11 '11 at 12:35
  • Does this now do what you wanted? – Henry Aug 11 '11 at 13:17