3

I have downloaded jquery.hotkeys-0.7.9.js plugin and was trying to provide hot key. on pressing ctr+t it should open new url address . i have made this something like this

 jQuery(document).bind('keypress', 'Ctrl+t',function (evt){ alert('ctrl+t is pressed'); window.location.href = ("${createLink(controller:'trip',action:'create')}"); return false }); 

But this is not working, it resonds to any key in my keyboard,(even if i press a,b,c etc). What changes i should make so that it should respond to only ctr+t ?? even if i delete the downloaded plugin from js folder the result is same\

jquery version i am using is jquery-1.1.3.1.pack.js

7
  • What browser are you using ? Is there any errors that you see on the console ? There should be a ; after return false Commented Jun 24, 2011 at 13:57
  • there is ; after return false , there is no error message , on pressing any key it redirects to specified url in window.location.href i am using mozilla browser (current version) Commented Jun 24, 2011 at 14:04
  • 1
    If a website overrides the default <ctrl>+t behaviour (opening a new tab), I would personally try to find out the address of the creator of this website and pay him/her a visit, and punch this person to the face -> ,('^')=@ Commented Jun 24, 2011 at 14:05
  • 3
    @Hussain - @Justus is saying ctrl-t has a purpose and you are breaking it. Commented Jun 24, 2011 at 14:11
  • 2
    Why in the world are you using a version of jquery that is 4 years old? Commented Jun 24, 2011 at 14:15

2 Answers 2

3

According to the example the project gave, you should do this:

$.hotkeys.add('Ctrl+t', function(){ alert("haha"); }); 

But in Chrome(and maybe some other browsers too), Ctrl+t is the default hot key to open a new tab, I don't know how to overwrite it. So when I test, I replace Ctrl+t to Ctrl+v, and it worked.
fiddle: http://jsfiddle.net/QFT8f/
UPDATE:
This is copied from the source file of hotkey.js:

USAGE: $.hotkeys.add('Ctrl+c', function(){ alert('copy anyone?');}); $.hotkeys.add('Ctrl+c', {target:'div#editor', type:'keyup', propagate: true},function(){ alert('copy anyone?');});> $.hotkeys.remove('Ctrl+c'); $.hotkeys.remove('Ctrl+c', {target:'div#editor', type:'keypress'}); 
Sign up to request clarification or add additional context in comments.

14 Comments

what this mean ?? so many confusion.. it gives error $.hotkeys is undefined
@Hussain did you see my jsfiddle? It worked fine with JQuery 1.6 and js-hotkeys.googlecode.com/svn/trunk/jquery.hotkeys.js
I tried your link there also i am getting error message saying $.hotkeys is undefined
@Hussain you can see usage example in the script.
@Hussain I updated the answer, check it. And try to use a newer version of JQuery
|
1

Your code is correct based on the documentation, but the plugin does not work as expected (at least in Firefox). I would ditch the plugin and just handle it based on event attributes.

Here's a fiddle with the plugin installed where you can see the event firing when any key is pressed, and how you would limit to only run certain code when 'Ctrl+t' is pressed. Note: you have to click on the output panel to give it focus for the key events to fire.

JavaScript code:

$(document).bind('keypress', 'Ctrl+t',function (e){ if(e.which==116 && e.ctrlKey){ alert('Ctrl+t was pressed'); return false; } else { alert('Any other key. Bad code...BAD!'); } return true;//Pass the event on }); 

UPDATE

I had previously added the plugin when I was testing, but it appears like I lost it somewhere when I was fiddling, so the example above works in plain JQuery (I'd make the second parameter the function though). Here's the fiddle with the jquery.hotkeys-0.7.9.min.js resource added with the same code, and for me it throws an "elem.getAttribute is not a function" JavaScript error when run with JQuery 1.6. Another reason to ditch the plugin!

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.