1
function initKeys() {

    $(document).bind("keydown", "U", LoadPlayer);
}

window.onload = initKeys;

I want to execute the function 'LoadPlayer' on pressing the u key. What I'm getting is that for any pressed key the 'LoadPlayer' is executed.

The HotKeys library is added like this:

<script language="javascript" type="text/javascript" src="./libraries/jquery.hotkeys.js"></script>

But it cannot be found. I've putted it in the exact same place as other libraries. No problem with other ones

What am I doing wrong?

Sanich
  • 1,739
  • 6
  • 25
  • 43
  • Which jQuery Hotkeys are you using? There are multiple versions that are invoked differently. – Tyler Crompton Apr 09 '13 at 13:15
  • @Tyler Crompton - jquery.hotkeys.js – Sanich Apr 09 '13 at 13:15
  • What I am asking is where you got it from. It would be helpful if you could provide a jsfiddle demonstrating the issue. – Tyler Crompton Apr 09 '13 at 13:16
  • https://github.com/jeresig/jquery.hotkeys – Sanich Apr 09 '13 at 13:18
  • @TylerCrompton You like downvoting? – Ron van der Heijden Apr 09 '13 at 13:19
  • @Bondye, fix your answer and I will be glad to change it, but you did not correctly answer the question. – Tyler Crompton Apr 09 '13 at 13:20
  • @TylerCrompton [Read this](http://stackoverflow.com/privileges/vote-down) `Use your downvotes whenever you encounter an egregiously sloppy, no-effort-expended post, or an answer that is clearly and perhaps dangerously incorrect.` – Ron van der Heijden Apr 09 '13 at 13:21
  • The tag says hotkeys, but the question doesn't say anything about requiring hotkeys in the solution. – thelr Apr 09 '13 at 13:21
  • 1
    Please provide a jsfiddle demonstrating the issue. Help us help you. Also, what version of jQuery are you using? – Tyler Crompton Apr 09 '13 at 13:22
  • @Bondye I'm not trying to be a butt, but again, you did not answer the question correctly. Sorry that I upset you. – Tyler Crompton Apr 09 '13 at 13:23
  • @TylerCrompton You are just not following the SO rules. None of these answeres are egregiously, sloppy, no-effort-expended or dangerously. I guess you are the one who is angry... – Ron van der Heijden Apr 09 '13 at 13:28
  • @Bondye "An answer that is clearly and perhaps dangerously incorrect" means an answer that is obviously and *possibly* dangerously incorrect. That's what "perhaps" means: possibly. Therefor, it does not have to be considered dangerously incorrect if it is clearly incorrect to yield downvotes. – Tyler Crompton Apr 09 '13 at 13:31
  • @TylerCrompton Please stop your excuse. You don't follow the SO rules. None of the answeres have a little possibility to be dangerously to anyone... – Ron van der Heijden Apr 09 '13 at 14:01
  • @Bondye, you clearly don't understand what I am saying at all. That fact that the answers are not dangerously incorrect is irrelevant. They are **clearly** incorrect, which qualifies them for downvotes. I'm sorry for upsetting you, but you're answer was incorrect. – Tyler Crompton Apr 09 '13 at 14:18

3 Answers3

1

Try this, it should work. It checks the key pressed in an anonymous function (so that you can add as many hotkeys as you need).

$(document).ready(function(){
    $(document).bind("keydown", function(e){ 
        e = e || window.event;
        var charCode = e.which || e.keyCode;
        if(charCode == 85) LoadPlayer();
    });
});

Demo: http://jsfiddle.net/HULgw/ (click in the result block after running to make the keydown event listened :) )

Robin Leboeuf
  • 2,096
  • 1
  • 13
  • 14
0

You're binding the keydown event on all keys. that "U" is the parameter you will pass to the handler, loadPlayer (q.v http://api.jquery.com/bind/). Instead bind keydown directly, and filter inside it on the keycode.

Technologeeks
  • 7,674
  • 25
  • 36
  • 2
    Technologeeks may be on to something here. Maybe OP isn't loading hotkeys correctly, and is executing the jquery core bind() instead. – thelr Apr 09 '13 at 13:22
  • @Seth, everybody else was onto this too, but all of their answers were incorrect. But I think you may be the one to actually figure it out. It sounds reasonable to me. – Tyler Crompton Apr 09 '13 at 13:26
  • @Seth - You are right. The jquery.hotkeys.js is not recognized despite the fact it is exists in the proper path. I guess that already another question, but what can be the reason for this? – Sanich Apr 09 '13 at 13:31
  • @Sanich, you don't have any typos in your path and you are certain that the file contains the jQuery Hotkeys code? – Tyler Crompton Apr 09 '13 at 13:32
  • No Typos :). How can i verify that the file contains the jQuery Hotkeys code? (Sorry for silly question) – Sanich Apr 09 '13 at 13:35
  • I am getting a message that the file jquery.hotkeys.js was not found. – Sanich Apr 09 '13 at 13:39
  • The problem is probably with the jquery.hotkeys.js file that cannot be recognized. What is the right thing to do? Do I have to open another question on edit the original one. – Sanich Apr 09 '13 at 13:42
  • @Sanich, A question asking that will get closed. Look at your file structure and determine the correct path to your Hotkeys file. There is clearly a typo since you are getting a 404. – Tyler Crompton Apr 09 '13 at 13:46
  • I'm not getting 404. I'm working with VS editor end i'm getting a message the the file was not found. The file is in the same path as other libraries which is no problem with them. – Sanich Apr 09 '13 at 13:52
0

Try deleting ./ from your javascript src path. Also check file permissions on your hotkeys library.

thelr
  • 1,134
  • 11
  • 30