Hi,
Can anyone show me how to show an alert box any time ctrl -n is pressed.
Thanks in Advance
Sinky
| SitePoint Sponsor |
Hi,
Can anyone show me how to show an alert box any time ctrl -n is pressed.
Thanks in Advance
Sinky

Ctrl+N opens a new window in many/most browsers, so if you try to apply some other function to it you run the risk of:
- it not working
- it working, but a new window popping up
- annoying someone who wants to open a new window but finds an annoying alert box popping up instead
It would be much more sensible to use something else, like just the letter N:
Code:document.onkeydown = function(e) { if (!e) {keycode = event.keyCode;} else {keycode = e.which;} if (keycode == 78) { alert('You pressed N'); } }




This will work, but ctrl + N opens a new window in most browsers, and your alert, being fired on the old window, will not be visible until either you blur the new window or refocus the old one. One side effect of the alert- it will pause any running scipts on the original window until you return and close it.Code:document.onkeydown = function(e) { e= window.event || e; if (e.ctrlKey && e.keyCode == 78){ alert('You pressed Control + N'); } }
I suggest this order:e= window.event || e;
because listeners added with attachEvent do receive event object arguments. And because it is more 'forward compatible'.Code:e = e || window.event;
Cross-Browser.com, Home of the X Library
Bookmarks