Handling Keyboard Shortcuts in JavaScript

Despite the many JavaScript libraries that are available today, I cannot find one that makes it easy to add keyboard shortcuts(or accelerators) to your javascript app. This is because keyboard shortcuts where only used in JavaScript games - no serious web application used keyboard shortcuts to navigate around its interface. But Google apps like Google Reader and Gmail changed that. So, I have created a function to make adding shortcuts to your application much easier.

Update

This is the second version of this script. The following modifications were made...

The documentation for the last version is still available.

Thanks for all the comments and suggestions, guys.

Demo

Just press the shown combinations - if the script works, the status will changed to 'Called'. For example, press the key '1'...

1
Function Called : Not Yet
Ctrl+1
Function Called : Not Yet
Alt+1
Function Called : Not Yet
Shift+1
Function Called : Not Yet
Ctrl+Shift+1
Function Called : Not Yet
Ctrl+Alt+1
Function Called : Not Yet
Shift+Alt+1
Function Called : Not Yet
Ctrl+2 - Remove Shortcut
Function Called : Not Yet
If you click the 'Remove Shortcut' Link before pressing the Ctrl+2 Combination, the function should NOT be called.
3 - With disable_in_input = true
The function should NOT be called when typing '3' in the input field
Function Called : Not Yet
4
Function Called : Not Yet
This uses the 'keycode' option.
Ctrl+A - With propagate = true
Function Called : Not Yet
If Ctrl+A is pressed, it will call the function and then propagate the event to the browser - selecting the text.

Try it out...

Live Evaluation

Documentation

shortcut.add()

First Argument : The Shortcut Key Combination - String
The shortcut key combination should be specified in this format ... Modifier[+Modifier..]+Key. More about this in the Supported Keys section.
Second Argument : Function to be called - Function
Specify the function here. This function will be called if the shortcut key is pressed. The event variable will be passed to it.
Third Argument[OPTIONAL] : Options - Associative Array
This argument must be an associative array with any of these three options...
type - String
The event type - can be 'keydown','keyup','keypress'. Default: 'keydown'
disable_in_input - Boolean
If this is set to true, keyboard capture will be disabled in input and textarea fields. If these elements have focus, the keyboard shortcut will not work. This is very useful for single key shortcuts. Default: false
target - DOM Node
The element that should be watched for the keyboard event. Default : document
propagate - Boolean
Allow the event to propagate? Default : false
keycode - Integer
Watch for this keycode. For eg., the keycode '65' is 'a'.
Example...
{
'type':'keydown',
'propagate':false,
'disable_in_input':true,
'target':document,
'keycode':65
}

Example Code


shortcut.add("Ctrl+B",function() {
	alert("The bookmarks of your browser will show up after this alert...");
},{
	'type':'keydown',
	'propagate':true,
	'target':document
});

shortcut.remove()

Just one argument - the shortcut combination that was attached to a function earlier. Make sure that this is exactly the same string that you used while adding the shortcut.

Example Code


shortcut.add("Ctrl+B",function() {
	alert("Bold");
});
//Remove the shortcut
shortcut.remove("Ctrl+B");

Supported Keys

The shortcut keys should be specified in this format ...

Modifier[+Modifier..]+Key

Example...

Ctrl+A

The valid modifiers are

You can specify a sequence without a modifier as well - like this...

shortcut.add("X",function() {
	alert("Hello!");
});

The valid Keys are...

These keys are case insensitive - so don't worry about using the correct case.

This library is in beta - so expect some problems. Suggestions are welcome.

Code

Download the shortcuts.js file.

License

BSD License

Other Libraries

There is a jQuery plugin available for this library - jQuery Keyboard Hooker Plugin by Tzury Bar Yochay

Comments

Michael at 12 Jul, 2007 11:35
Hi,

I love your scripts. They are very easy to use, they work perfect and the update with the disabling in input/textarea is the last thing I missed.

Great work, keep it coming :-)

Michael
Reply to this.
Binny V A at 13 Jul, 2007 01:58
Thanks for the encouragement :-) The new version is in beta - so please let me know if you find any problems with it.
Reply to this.
Anonymous at 13 Jul, 2007 01:18
Thank you SOOOOOOOOOO much for adding "shortcut remove" function.
Reply to this.
paolo at 13 Jul, 2007 06:47
And it works with Safari too... but I can't put an exclamation mark nor the digit 'one' in this comment because their key is binded... Safari's bad? Anyway, really nice work. Keep on.
Reply to this.
Binny V A at 13 Jul, 2007 10:45
Thats is to be expected - I did not use 'disable_in_input' for that binding. However, you should be able to use the digit '3' here - like I just did :-), as that binding has the option disable_in_input.
Reply to this.
Lalit Patel at 13 Jul, 2007 10:20
Hi Binny,
You code works just fine for me.
I was looking disabling in input/textarea feature. Its great that you added it.

Cheers :)
Reply to this.
Aaron Rosenzweig at 14 Jul, 2007 10:08
Great Script, thanks for sharing. To get the "Command" modifier of a Mac keyboard to be recognized you need to look for "Event.metaKey" just like you look for Event.shiftKey and Event.ctrlKey. The "Command" key is the one that looks like a cloverleaf and an "open apple".
Reply to this.
Ram at 15 Jul, 2007 09:25
Great work. Keep it coming
Reply to this.
esam bdair at 15 Jul, 2007 11:58
hi ..
Great Script, thanks for sharing.
i want to ask you one qustion ?
when i try to add a shortcut(Ctrl+S) it's work but after calling my function the save dialog appear how i can remove this dialog?
this is my code

shortcut.add("Ctrl+S",function() {
alert("test");
return false;
},{
'type':'keydown',
'propagate':false,
'target':document
});
Reply to this.
Mark Lewis at 20 Jul, 2007 07:53
I echo everyone else's sentiments: this is a beautiful script.

However, I can't seem to capture the following characters: , < . >

Just trying to get the ","/"<" key to work, I've tried all of the following combinations:
Ctrl+,
Ctrl+<
Ctrl+Shift+,
Ctrl+Shift+<

None of the events trigger in IE7. This may apply to other special characters as well.
Reply to this.
Seth at 20 Jul, 2007 12:58
I cannot seem to capture Ctrl+Enter in Firefox for the Mac OS X. I can capture lots of other Ctrl+ or +Enter combos, but not Ctrl+Enter. Any thoughts? Here is the command I'm testing with:

shortcut.add('ctrl+enter',function(){alert("Hello World");})

-Seth
Reply to this.
kUI at 23 Jul, 2007 07:55
This is not working with JSF...
Reply to this.
YJ at 26 Jul, 2007 12:12
Help me
i'm make test page and not execute....script error
i want know this code problem...
anyone show me correct source code ^^
thank you
=======================error source=============================================
<html>

<HEAD><TITLE>Handling Keyboard Shortcuts in JavaScript</TITLE>

<script type=text/javascript src="shortcut.js">
</script>


<script type=text/javascript >
function init() {
alert("init");
shortcut("Shift+F1",function() {
alert("Help Me!");
});
shortcut("Ctrl+S",function() {
alert("Saved!");
});
shortcut("Right",function() {
alert("Right");
});
}

</script>
</head>
<body onload ="init()">
test
</body>
</html>
======================================================================
Reply to this.
Binny V A at 26 Jul, 2007 01:12
That code is for the old version of this library. Please use shortcut.add() if you are using the latest version.
Reply to this.
Anonymous at 08 Aug, 2007 05:32
maybe use

shortcut.add( instead of shortcut(
Reply to this.
Anonymous at 31 Jul, 2007 11:15
Mr. Binny, This lib is compatible with IE6? Thanks.
Reply to this.
Binny V A at 01 Aug, 2007 05:58
Yeah - I tested it in IE6 - it worked without any problems.
Reply to this.
Anonymous at 01 Aug, 2007 06:52
Thank You, Mr. Binny. Congratulations for very well done work.
Reply to this.
Anonymous at 01 Aug, 2007 07:25
Dear Mr. Binny:

this code propagate and open de file open box. But if we add a alert box in 'func' then will not to propagate. What is wrong?
shortcut.add("Ctrl+O",function() {
func("obj","id") ;
},{
'type':'keydown',
'propagate':false,
'target':document
});

function func(obj,id) {
var a;
a=1;
// if add this don't propagate. OK.
//alert("Hello Mr. Binny");
//but if remove this alert will propagate. Not OK.
}
Reply to this.
Anonymous at 03 Aug, 2007 10:56
Dear Mr. Binny:

With IE7, I add 'e.keyCode = 0;' to stop the event print for CTRL + P:
...
e.cancelBubble = true;
e.returnValue = false;
e.keyCode = 0;

But this don't work for IE6.
Thanks for your suport.
Reply to this.
Quân at 01 Aug, 2007 06:36
Thanks, thanks, thanks
Reply to this.
Chad Whitacre at 06 Aug, 2007 01:30
The "Ctrl-Alt-2" event propagates in FF <one>.5 when it shouldn't. If you have an alert in your callback it dies properly, but w/o that alert it bubbles up and switches tabs. Any ideas?
Reply to this.
Binny V A at 06 Aug, 2007 08:12
Prorogation is the issue for most people with this script. I am still looking into it.
Reply to this.
Aileen at 06 Aug, 2007 07:18
Hi, i really love your script :D
It helps me a lot for my final project.
But i was thinking, if i want to disable the printscreen by using your script.
Is it achievable?
Reply to this.
Yoga at 07 Aug, 2007 03:16
anyway, is it possible using "print screen" as a shortcut keys? actually, i wanna disable "print screen", but why "print screen" didn't detected as keypress or keydowm, though i know its ascii is 44. anybody can give any idea to solve my problem??
thanks indeed.. great script anyway..
Reply to this.
Binny V A at 07 Aug, 2007 06:01
Hmm - I did not include Printscreen in my shortcut list. An oversight on my part. I will update the script by the end of this week. I will try and make this change.

I will let you know then the script is updated
Reply to this.
Anonymous at 15 Sep, 2007 07:29
That would be awsome for protecting image of web site
Reply to this.
pekue at 08 Aug, 2007 03:22
Hi Binny and yall,

I just started to work my way into your script - fantastic lib, simply working and what I was looking for: thnx.
I found a single issue: is it true that there is no way to capture the release of the keyup of a pressed standalone ctrl?
shortcut.add('c',function(){cPressed=true;},{'disable_in_input':true});
shortcut.add('c',function(){cPressed=false;},{'disable_in_input':true,'type':'keyup'});
works as expected
shortcut.add('Ctrl',function(){cPressed=true;},{'disable_in_input':true});
shortcut.add('Ctrl',function(){cPressed=false;},{'disable_in_input':true,'type':'keyup'});
sets cPressed to true but not back to false :(

Id love to hear from you and once again:
thnx for you great work

pekue
Reply to this.
Binny V A at 17 Aug, 2007 01:09
That will not work. You have to do this...


var cPressed = false;
shortcut.add('Ctrl',function(){
cPressed = (cPressed) ? false : true;
},{'disable_in_input':true,'type':'keyup'});
Reply to this.
Anonymous at 05 Dec, 2007 04:45
Can any give a better explanation how to use 'this' in this shortcut. Using this can i add shortcuts to particular controls/iframe....
Reply to this.
Nitin at 08 Aug, 2007 11:10
This is excellent. Thanks for putting it up.
Reply to this.
BradW at 09 Aug, 2007 09:24
So, I like what I see. We have a need to also use the F4 key. Looks like we can trap it, but the event still gets propogated as we also get the address list after trapping the event. The same thing happens when I try Alt+F4. We trap the event and do our stuff, then explorer closes.

Thoughts on this? Is there a way around this?
Reply to this.
Binny V A at 09 Aug, 2007 02:25
I think F4 issue can be solved - but I cannot do anything about Alt+F4. That is handled by the OS and not the browser. JS can do nothing about that.
Reply to this.
Narendra Naidu at 30 Aug, 2007 04:49
Function keys such as F-four and F-five are still getting propogated?
Is there any way out?
Reply to this.
joo at 12 Aug, 2007 10:10
thanks for your present
I like it very much
Reply to this.
Jason at 13 Aug, 2007 11:38

I can't seem to get CAPS lock to work on the Mac (haven't tried a PC yet) in Safari/Firefox 2.0.0.6/Camino .5

Thanks for this script.

Jason
Reply to this.
Blake at 17 Aug, 2007 05:14
I've got some warnings in Firefox Error Console:
1) assignment to undeclared variable shortcut
2) anonymous function does not always return a value

Easy to correct:
1) var shortcut = { (add "var")
2) "return undefined" instead of plain "return" in function "func" ?

Someone may integrate this into a larger application, so better to prevent any additional warnings :)
Reply to this.
Binny V A at 18 Aug, 2007 02:08
I am not getting any errors. Anyone else seeing this error? If so, what is the version of your Firefox?
Reply to this.
Phil Nelson at 22 Aug, 2007 02:33
Is there a problem with IE7 and propagation ?

I've bound F1 to an alert, and the dialog box pops up fine. Nothing else happens on Firefox (even although without the binding it would pop up the help) but on IE7 it also switches to the Help screens.

Apart from that the library is wonderful.

Thanks

Phil Nelson
Reply to this.
Michal Till at 22 Aug, 2007 06:48
This might be handy for someone: www.json.cz/canceling-browsers-keyboard-shortcuts
Reply to this.
John at 23 Aug, 2007 02:31
Any ideas on getting the ? key to work?
I've tried

Shift+/
?
Shift+?
/

No luck :( also Shift+3 is a bit funny in safari, it seems to do the action of the last pressed shortcut.

Cheers for the great script
Reply to this.
Patrick at 28 Aug, 2007 12:49
Hi,
great work on this script. Easy to use, working on all browsers and efficient.
I took the liberty to add a feature that I use all the time: the ability to add an execution scope to the callback function. All my javascript code is structured in classes, and when I declare a shortcut inside a class, I want the callback function to have access to the "this" variable as well.

Below is my modification of the add function for this in case you are interested:

Line 309, replace:
callback(e);
with:
if(typeof obj == "object" && scope == true) {
  obj.tmpShortcutCallback = callback;
  obj.tmpShortcutCallback(e, obj);
  delete obj.tmpShortcutCallback;
}
else {
  callback(e, obj);
}
AND add this to the "add" function definition:
'add': function(shortcut_combination, callback, opt, obj, scope) { ...

You will then be able to do stuff like this:

foo = {
    id: 5,
    bar: function() {
        alert(this.id);
    },
    init: function() {
        uic.shortcut.add("Shift+V", this.bar, {}, this, true);
    }
}

foo.init();
Cheers for the good script.
Patrick
Reply to this.
Binny V A at 29 Aug, 2007 07:23
Thanks for giving back the code - I will try to include this functionality in the next version. If you have anymore suggestions, dont hesitate to shoot me an email.
Reply to this.
Anonymous at 31 Aug, 2007 02:17
CTRL+S opens the save dialog too in FireFox.
shortcut.add
("Ctrl+S", function()
{
alert("You have pressed CTRL+S");
return;
}
,
{
'type':'keydown',
'propagate':true,
'target':document
}
);
Reply to this.
Paolo Dona at 21 Sep, 2007 01:56
I could stop the CTRL-S event from propagating (and thus showhing the 'save as...' dialog in firefox) just using 'keypress'

shortcut.add("Ctrl+S",
function(evt) { alert('it works'); },
{'type':'keypress','propagate':false,'target':document}
);

tested with Fireox 2.0.0.7
Reply to this.
Anonymous at 31 Aug, 2007 02:18
Having the below script and pressing CTRL+F4 shows 2 alert ( for ctrl+f4 and the other for ctrl+s)
< script>
shortcut.add
("Ctrl+S", function()
{
alert("You have pressed CTRL+S");
return;
}
,
{
'type':'keydown',
'propagate':true,
'target':document
}
);

shortcut.add
("Ctrl+F4", function()
{
alert("You have pressed CTRL+F4");
return;
}
,
{
'type':'keydown',
'propagate':true,
'target':document,
'keycode':115
}
);
</ script>
Reply to this.
Omar at 03 Sep, 2007 10:18
Hi, thanks for the script

I use the script in a web aplication, and i need a shorcut to submit a form, I use too the form plugin of jQuery.

I have a big problem, when I pulse F4 for submit the form, the form submit many times, one, two, three, or any. I dont understand why because I only submit one time.

I need your help, tks
Reply to this.
Tzury Bar Yochay at 08 Sep, 2007 09:08
There is a version of this library as jQuery-plugin which might be good in case you are using jQuery Forms plugin
Reply to this.
Ken at 05 Sep, 2007 10:10
Is there a way to block backspace as shortcut for "Back"? I've tried adding a shortcut for backspace with a simple alert, but it performs a Back and then displays my message.

Is there a way to handle mousewheel up and down? shift+mousewheelup is another shortcut for Back which I would like to block.
Reply to this.
Flo at 06 Sep, 2007 08:46
Hey, Thanks for this library, you did a great work here!

This script is working pretty well except for "Ctrl+B" which is still propagated and display my bookmarks in Firefox 2.
Any idea on how to go over this ?


Reply to this.
yangbo at 11 Sep, 2007 08:17
之前用的是你的第一个版本,很好用,很简单,方便,但是当我想要去掉快捷键时,确找不到这个方法。我又来到你的网站,看到你改进后的版本,我很开心。非常感谢你的代码。
也许你看不懂这汉字。
Thank you very much!
Reply to this.
yangbo at 11 Sep, 2007 08:20
Thanks for this library.
Thanks for function "remove()".
Reply to this.
NitinR at 17 Sep, 2007 05:47
I love your scripts. They are very easy to use and perfect.
Excellent..Great work.

Thanks you very much for this library.
Reply to this.
Pete Warden at 20 Sep, 2007 05:57
Thanks for this, great work! I've been wrestling with similar issues on my Firefox extension, Google Hot Keys. I've found it impossible to block the propagation of the down arrow, it always ends up moving the page down. I see the same thing happen if I test this code:
shortcut.add("Down",function() {
alert("Hi there!");
},
{
'propagate':false,
'disable_in_input':true,
}
);

Is that something you've run across, or have any ideas on? Thanks,

Pete
Reply to this.
jerone at 05 Oct, 2007 07:38
Looks like a very interesting script, but i'm having the following problems -with the newest browsers atm- so far:
FF2 > can't use the numpad numbers,
Opera 9.2 > Alt + any number doesn't work,
IE7 > also can't use the numpad numbers.
Reply to this.
Anonymous at 26 Oct, 2007 08:01
Same issue...
Reply to this.
Anonymous at 05 Nov, 2007 05:58
Narf

Define in shortcut.js this:
'num0' :96,
'num1' :97,
'num2' :98,
'num3' :99,
'num4' :100,
'num5' :101,
'num6' :102,
'num7' :103,
'num8' :104,
'num9' :105,

then

shortcut.add("num0",function() {
alert("num0");
});
Reply to this.
Furkan at 11 Oct, 2007 04:14
Your script is really awesome, good job.

I ve question,

can we define all keys?I mean any keys of keyboard.
Reply to this.
vanvan at 17 Oct, 2007 04:46
When and set some key already used by the Firefox (Ex: Crtl + N) it performs the action of the script and after the browser, something that would not perform the action of the browser?
Reply to this.
Anonymous at 18 Oct, 2007 09:59
I'm trying to remove ctrl a but it seems to still be working.
shortcut.remove("Ctrl+A");

is this all i need?
Reply to this.
Binny V A at 18 Oct, 2007 09:34
If you have defined that shortcut, thats all you need. Please keep in mind that the shortcuts provided by the browser cannot be removed like that.
Reply to this.
Johnny at 18 Oct, 2007 05:06
I'll just say WOW
Reply to this.
Johnny at 18 Oct, 2007 05:07
BETTER ...

I'll just say WOW and THANKS man.
Reply to this.
Anonymous at 18 Oct, 2007 11:53
Hi All,

I would like to use this function to disable F3 function in IE so that my Flash Application can use that. But still it opens up the search function. How is it? I had given propagate : false also. Nothing happens. Am I doing something wrong...

shortcut.add("F3",function() {
alert("Hi there!");
},{
'propagate':false
}
);
Reply to this.
Steffen at 31 Oct, 2007 08:53
great work, very useful for me, get on with it!
Reply to this.
TRS at 03 Nov, 2007 07:06
Man , your js shortcut was fabulous. Only thing that is lagging how to make it work across frames.And one more thing I have two shortcuts (Ctrl+T and Ctrl+F5). On pressing Ctrl+F5 both the events are fired.

Note: As far you scripts propagation issue are considered. In I.E make e.keycode = 0 for special shortcuts like Ctrl+F and F5. As firefox is considered, event.preventDefault() works only on keypress. Hence use keydown in I.E and keypress in FireFox.
Reply to this.
TRS at 04 Nov, 2007 08:51
and same problem with iFrames.
Reply to this.
Anonymous at 07 Nov, 2007 07:59
I need to use this keys: '+' and '-'.
It would be great that for a new version of the library this keys will be implemented
Thankz
Reply to this.
Chacho at 03 Dec, 2007 06:01
Thx for this ;)
Reply to this.
Manny at 04 Dec, 2007 04:36
Thanks for the script man. This solved my problem. I'm converting a DOS based program to web-based and the client wants to have the same shortcut keys. I thought something like this was impossible in browsers and I was looking at the limited capabilities of accesskey.

This is just awesome.
Reply to this.
trs at 05 Dec, 2007 05:27
The shortcuts do not work on if focus is on any flash object/objects, can any one help.

Reply to this.
trs at 05 Dec, 2007 05:31
How any explain how use 'this' for this shortcut script . can we target particular controls or frame objects.
Reply to this.
trs at 05 Dec, 2007 09:01
event capturing gives permission denied error.
Reply to this.
trs at 05 Dec, 2007 02:11
use below code to make + and - work:
//Find Which key is pressed
var code =0;
if (e.keyCode)
{
code = e.keyCode;
if(code>90)
code= code - 64;
}
else if (e.which) code = e.which;
Reply to this.
trs at 05 Dec, 2007 04:34
more over to make '+' work , use other name say 'plus' : make this modification in shortcut.js

for(var i=0; k=keys[i],i<keys.length; i++) {
//Modifiers
if (k == 'plus')
k = '+';
Reply to this.
trs at 05 Dec, 2007 04:34
shortcut.add("Ctrl+plus",
Reply to this.
trs at 05 Dec, 2007 04:35
I have one question : how to stop propogation alt+F, alt+e ,...in IE?
Reply to this.
Binny V A at 05 Dec, 2007 08:38
> I have one question : how to stop propagation alt+F, alt+e ,...in IE?
Try
shortcut.add("Alt+f",function(e) { alert('it works'); },{'propagate':false});
If that did not work, try with a different event
shortcut.add("Alt+f",function(e) { alert('it works'); },{'type':'keydown', 'propagate':false});
or
shortcut.add("Alt+f",function(e) { alert('it works'); },{'type':'keyup', 'propagate':false});
or
shortcut.add("Alt+f",function(e) { alert('it works'); },{'type':'keypress', 'propagate':false});

Hopefully, one of these will work - but there is no guarantee.
Reply to this.
Anonymous at 07 Dec, 2007 04:06
propagation is not stopping in case Alt+D,Alt+F,......
Reply to this.
Anonymous at 09 Dec, 2007 11:49
Hello. First of all, please, sorry my poor english.

I have certain little problem with shorcut.js in the case of assign "t" key and not F5 key. In this case, when you press the F5 key shorcut.js trigger the event assigned to the "t" key.

The same issue with the "s" and F keys, and for the "u" and F6 keys. You can obtain information about this issue in this link of the jQuery hotkeys plugin page.

I dont now if is the better way, but, the above issue can fixed adding this lines of code before the line forty six of shortcut.js:


// The user press "s" key and not only the F4 key
if( (code == 115) && (character == 's') ) character = '';
// The user press "t" key and not only the F5 key
if( (code == 116) && (character == 't') ) character = '';
// The user press "u" key and not only the F6 key
if( (code == 117) && (character == 'u') ) character = '';


Hope be usefull for anyone. Thanks a lot for your shortcut.js library :)
Reply to this.
Anonymous at 12 Dec, 2007 01:35
nice thing, that solved all my shortcut problems and saved a lot of time.
Thumbs Up!
Reply to this.
Varun.Paval at 14 Dec, 2007 02:57
Hi Binny,

Its really interesting...

Varun.P
Reply to this.
jakjr at 19 Dec, 2007 05:08
Great Script.

only one thing:
This code doesnt work on I.E. Only with keydow:

shortcut.add("Down",function() {
alert("Hi there!");
},
{'type':'keypress'}
);

Any sugestions ?
Reply to this.
jakjr at 19 Dec, 2007 06:01
The answer:
unixpapa.com/js/key.html

Thanks
Reply to this.
Scott at 24 Dec, 2007 03:21
Thank you for making this elegant solution available.
Reply to this.
LJ at 25 Dec, 2007 09:25
can i create a hotkey for input-text? which onMouseDown event.
Reply to this.
LJ at 25 Dec, 2007 09:30
can i create a hotkey for input(text) form only, which in onMouseDown,OnKeyPress events..
Reply to this.
Anonymous at 08 Jan, 2008 12:24
quick and dirty way to stop propagating key events in IE is to set event.keyCode=0
Reply to this.
gad at 09 Jan, 2008 07:02
good solution. thank you
Reply to this.
Anonymous at 17 Jan, 2008 04:44
So how about the problem of the Print Screen key ... i'm trying to catch event from print screen key pressed but unfortunatly it doesent work in IE 7.
Have some ideea??
Reply to this.
Anonymous at 20 Jan, 2008 02:10
What about the windows key? or the Apple Key?
Reply to this.
Anonymous at 22 Jan, 2008 04:40
pls help me how to disable ctrl+n key in text field...........on keypress,keydown is not working on the press of ctrl+n keys
Reply to this.
Wouter van Reeven at 24 Jan, 2008 02:51
Hi,


Thanks for this great piece of work. Totally excellent. Too bad I cannot type exclamation marks in this editor ;-) Anyway, I too ran into differences between FF and IE. The difference with keypress and keydown is annoying when trying to develop web apps that will be used on FF and IE. In order to solve this issue, I added thses lines to your script

var keyEvent = 'keydown';
if (navigator.appVersion.indexOf("MSIE")==-1) {
keyEvent='keypress';
}

Then I modified the defaults as follows:

var default_options = {
'type':keyEvent,
'propagate':false,
'disable_in_input':false,
'target':document,
'keycode':false
}

This basically solves my issue, at least under Windows. I haven't tried this with Linux yet but will. Please consider adding these lines to your otherwise great script or tell me if there are any issues with this "solution".


thanks, Wouter
Reply to this.
Wouter van Reeven at 24 Jan, 2008 03:03
OK I now also tried this in FF under Linux (actually IceWeasel under Debian but that is FF) and also IE6 under Linux (using the ielinux project) and in these two versions this also works.


Wouter
Reply to this.
Anonymous at 24 Jan, 2008 03:59
well, i tried and it worked.
i want to know that
if i press P
is there anyhow that ctrl+p is executed followed by Atl+r followed by Alt+l
This is done to set printer setting to Landscape.
Reply to this.
Paul at 31 Jan, 2008 06:51
Hi,
Good work. Works good.
I added a help function.
something like this:

// get a html overview of all the current shortcuts
'getShortCutsInfo':function() {
var str = '<table style="margin: 10 10 10 10">';
for (prop in this.all_shortcuts)
{
str += '<tr><td align="center">' + prop + '</td><td> - ' + this.all_shortcuts[prop].description + '</td></tr>';
}
str += '</table>';
return str;
}

and don't forget to add
'description':'unknown'
around line 17
and
'description': opt['description']
around line 203

print this list in an alert or a modalwindow
Regards
Reply to this.
Anonymous at 01 Feb, 2008 02:10
could anyone make this work as a script using greasemonke? so it could be used as a shortcut for browsergames e.g.?

great work by the way
Reply to this.
Anonymous at 07 Feb, 2008 07:52
How make this script on if design mode is "on" for iframe/html
Reply to this.
Anonymous at 09 Feb, 2008 09:29
Thanks for the code. I used it to add keyboard shortcuts for navigating through my photo gallery.
Reply to this.
Anonymous at 11 Feb, 2008 05:58
Just a side note having come upon this site, you cannot catch print screen on a keydown or keypress event. Browsers are very funny with print screen, IE will not let you capture it (Version 7 onwards I believe). Firefox and a select few others will let you, however it is a fussy and buggy key and uses keyup.
Other than that, you're a bit screwed for a decent/easy way to capture it. Perhaps consider some other language if you require it; maybe even a little applet?

Crisp
www.crispycrisp.org
Reply to this.
Anonymous at 15 Feb, 2008 12:48
Variables "code" and "k" have global scope.
Here is the patch.

44d43
< var code;
145d143
< var k;
225a224
>
Reply to this.
Chat at 17 Feb, 2008 06:44
Perhaps consider some other language if you require it; maybe even a little applet?
Reply to this.
Anonymous at 18 Feb, 2008 12:36
Please suggest me the solution for IE6 to cancel event when user press esc key.It stops the downloading process in my web application.
I have tried-event.returnValue=false,
return=false,event.keyCode=0,
event.Cancel=true,
event.cancelBubble=true.But it is not working.
Reply to this.
iddaa at 19 Feb, 2008 04:16
This is a great script that i need.
thank you very much.

regards
Reply to this.
Anonymous at 22 Feb, 2008 06:19
Thank you for making this elegant solution available.
Reply to this.
Anonymous at 27 Feb, 2008 03:09
Hi, I'm using prototype.js and rico.js in the same page that I want to use shorcut.js. The problem is that in this page it don't load the function. If I remove ajax function it's work.

On the other hand, works fine in Mozilla, but I need to work in IE, any idea about that?

I've tested with different version of rico and prototype, but not work. I need to solve my problem asap.

Regards.
Reply to this.
Anonymous at 29 Feb, 2008 05:11
Hi, I'm using prototype.js and rico.js in the same page that I want to use shorcut.js. The problem is that in this page it don't load the function. If I remove ajax function it's work.

On the other hand, works fine in Mozilla, but I need to work in IE, any idea about that?

I've tested with different version of rico and prototype, but not work. I need to solve my problem asap.
Reply to this.
Anonymous at 04 Mar, 2008 12:50
Hello,

I used javascript(.js) file for adobe illustrator cs. Now i like to add shortct key for my .js file. Could you please advice me.

Thanks,
Sel.
Reply to this.
Álvaro G. Vicario at 04 Mar, 2008 09:31
I see some webmasters pretend to disable the "PrintScr" in order to "protect" site contents. There s-o-o-o-o-o many ways to override this that only completely inexpert users will be stopped. On the way, you can create lots of annoyances to the rest of the world (just think of all those stupid "Disable right-click" scripts out there that popup up even with the left button if you just don't browse with IE).

Would you take the risk of making your site unusable so it can't be stolen by some hypothetical leecher who won't have the ability to do anything with it?
Reply to this.
Anonymous at 06 Mar, 2008 02:32
is there a way to enable/dissable shortcuts by clicking checkbox using cookies.
and i there a way to redirect to another page in a different frame with target.
Reply to this.
Anonymous at 10 Mar, 2008 09:08
I have a question, how can I use this, only when I hit a key inside a input box or a asp textbox control?? How can I use de 'target' property ??
Reply to this.
Anonymous at 11 Mar, 2008 12:13
Other than that, you're a bit screwed for a decent/easy way to capture it. Perhaps consider some other language if you require it; maybe even a little applet? cet
Reply to this.
Anonymous at 12 Mar, 2008 08:43
Can I invoke Ctrl+f (Find in IE) through a mouse click using javascript.

thanks for your help
Reply to this.
Anonymous at 17 Mar, 2008 01:18
First of all, excellent library.
I'm trying to use the library to popup a window when the user presses a function key. The url of the popup window would change based on the textbox.
I'm using ASP.NET as development environment.

Is it possible to have like target a asp TextBox, i.e. target:TextBox2

Thanks, Blerim
Reply to this.
Anonymous at 18 Mar, 2008 04:43
Thanks On the other hand, works fine in Mozilla, but I need to work in IE, any idea about that?

Reply to this.
Anonymous at 18 Mar, 2008 10:27
Is there a way to capture a range of characters ('a'-'z','A'-'Z',' ','0'-'9'? I'm trying to update the innerHTML of a label on the fly... ;-) Thanks for the script - it's helped IMMENSELY....
Reply to this.
JT at 22 Mar, 2008 10:37
Hi, thanks by the library.
-keypress- does not work for me. Happens the samething with keypress such as keydown. I suposse keypress is activated just when you hold pressed the keyboard button. Please explainme what is the correct way. Thanks again.
Reply to this.
Anonymous at 27 Mar, 2008 06:58
very good thank you
Reply to this.
Anonymous at 31 Mar, 2008 04:35
is there a way to enable/dissable shortcuts by clicking checkbox using cookies.
and i there a way to redirect to another page in a different frame with target.
Reply to this.
Anonymous at 31 Mar, 2008 04:36
is there a way to enable/dissable shortcuts by clicking checkbox using cookies.
and i there a way to redirect to another page in a different frame with target.
Reply to this.
Anonymous at 31 Mar, 2008 04:52
Hi, thanks by the library.
-keypress- does not work for me. Happens the samething with keypress such as keydown. I suposse keypress is activated just when you hold pressed the keyboard button. Please explainme what is the correct way. Thanks again.
Reply to this.
Grup Hepsi at 03 Apr, 2008 01:50
very good thank you
Reply to this.
Anonymous at 09 Apr, 2008 08:51
Hi,
Thanks a lot to provide such a great script. As my requirement is as follows:
when i open a modal dialog window through java script, i don't want to allow user to take print screen. how to disable print screen. also tell me is there any way by which I can enable java script at client machine if it is disable.
Rajeev
Reply to this.
Scippie at 12 Apr, 2008 07:26
Just what I needed!!! Thanks!!!
Reply to this.
Emon at 13 Apr, 2008 12:28
You are a pioneer. Thanks tends to infinity for you effort. gr8 ( *_* )
Reply to this.
Anonymous at 16 Apr, 2008 11:23
Thanks and Kudos for this really simple and easy to use library. Can anyone tell me why does it not work with IE6 and Mozilla, It works just fine with IE7 Is any patch work required????
Reply to this.
China Landscape at 17 Apr, 2008 08:10
It works great and can disable the dialog box like save pag as... when ctrl+S.

Very simple to use, and works well with FF and linux

Thanks
Reply to this.
sahibinden at 21 Apr, 2008 08:05
woow great script.
thank you very much
Reply to this.
Dave at 23 Apr, 2008 12:31
Think I've found a bug. If I've told the script to ignore shortcut keystrokes when a form-element has focus with "disable_in_input" it works for everthing execpt select boxes. While a user can't type in a select box, they may hit a character that has been bound to a shortcut in order to jump forward in the list. I had to add:

|| element.tagName == "SELECT"

to the code where it shows:

if(element.tagName == 'INPUT' || element.tagName == 'TEXTAREA') return;

This will prevent the shortcut from being called when the user is working with an active select-box.

Reply to this.
govind at 24 Apr, 2008 06:46
excellant work Binny. I have a question. Iam working with multiple iFrames. How can I propogate events fired in one iFrame to another iFrame, so that actuall work happens in second iFrame?
Reply to this.
Anonymous at 25 Apr, 2008 11:41
Is there is any way we can stop going to another IE or App using Alt+Tab using this script?? i tried but doesn't seems to work. Can anybody capture those keystokes and restrict its event to not to trigger
Reply to this.
Anonymous at 01 May, 2008 01:28
this wery nice script thanks
Reply to this.
vuha at 02 May, 2008 05:48
Great script. It helped me with alot of issues. However, there remains a problem with IE6.0 and iframes. I added an iframe on a test page that was working perfectly (use this page for example). This caused the shortcut keys to stop working. Clicking anywhere on the main document cause the shortcut keys to start working again. Anyone have ideas?

Thanks.
Reply to this.
Anonymous at 02 May, 2008 06:28
i have a drop down menu which has menu which comes on mouse move.i want to create short cut keys for this menu items.how to do it??
Reply to this.
Anonymous at 02 May, 2008 10:54
Im not a JavaScript geek but would love to get asistance from you on how to use this script so can be called and be active on page load. My intention is to disable use of keyboard shortcuts on a web page in a javascript enabled browser.
Thanks in advance
Reply to this.
Anonymous at 03 May, 2008 03:06
wow very good work. this will help me quite a lot in my projects and it is very user friendly. thus thanks very much.
Reply to this.
sakarya at 08 May, 2008 02:42
this wery nice script thanks
Reply to this.
şiir at 08 May, 2008 02:42
wow very good work. this will help me quite a lot in my projects and it is very user friendly. thus thanks very much.
Reply to this.
Anonymous at 09 May, 2008 10:39
please help me to find the new version of the js file.
Reply to this.
Comment


Comment




Comment Formating : HTML tags a, strong, em, b, i, code, pre, p and br allowed. Other tags will be shown as code(< will become &lt;). Urls, Line breaks will be auto-formated.