Intercepting ctrl-n

I would like to intercept the ctrl-n under Netscape and execute a JavaScript function when ctrl-n is pressed. To do so, I tried to disable the browser hotkeys and listen on the onKeyPress event. But this seems to have no effect: pressing ctrl-n still opens a new window instead of calling my listener. Here is the code:

    window.setHotkeys (false);
    window.captureEvents (Event.KEYPRESS);
    window.onkeypress = function (event) {
      if (event.which != 0) { alert (event.which); }

Any idea on what's wrong with the above code and how to execute a function when ctrl-n is pressed (if it is possible in the first place)?
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Try this...

<html><head><script language="JavaScript"><!--"UniversalBrowserWrite");
// -->

and test.html..

<html><head><script language="javascript"> <!--
window.captureEvents (Event.KEYDOWN);
window.onkeydown = function (e) {
  status = e.which+" - "+e.modifiers;
// -->
</script></head><body>KEYPRESS TEST</body></html>

This launches a browser window with hotkeys off.
When you press a key in the window, the key and the modifier are shown in the status.

As you can see if you try it, all the "hotkeys" are masked out, so that the browser just cannot see them... all ctrl-whatever combinations that are hotkeys return 0 in the which property.

This is so that you can't do exactly what you want... to "overwrite" a hotkey with your own functionality.


Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Michel PlungjanIT ExpertCommented:
But avernet's suggestion should work according to the docs:


Enables or disables hot keys in a window which does not have menus.

 Method of
 Implemented in
              JavaScript 1.2





            A Boolean value specifying whether hot keys are enabled:

                true enables hot keys

                false disables hot keys


To enable or disable hot keys, you need the UniversalBrowserWrite privilege. For information on
security, see the Client-Side JavaScript Guide.


By default, hot keys are disabled in a window which does not display a menu. With the setHotKeys
method, you can explicitly enable or disable all hot keys except security and quit, which are always

You can also specify whether to enable hot keys at the time you create a window when you use the method.

Could it be that "Enables or disables hot keys in a window which does not have menus. " is crusial? perhaps the menus must be off first?

or perhaps the space after each of your function calls

I can enabale and disable another window's hotkeys but not my own,"UniversalBrowserWrite");  self.setHotkeys(false);

does not work'','newwin','location');"UniversalBrowserWrite");  winid.setHotkeys(false);

does work

 so Brian's suggestion seems to be the one to use.

As Michel stated from the Netscape docs:

"By default, hot keys are disabled in a window which does not display a menu. With the setHotKeys method, you can explicitly enable or disable all hot keys except security and quit, which are always enabled."

The first sentence just doesn't match their implementation.
The second does stand, as you are able to explicitly enable and disble the hot keys, but only en masse, not individually. No mention is made of re-assignment (and a good job too, IMO).


The Ultimate Tool Kit for Technolgy Solution Provi

Broken down into practical pointers and step-by-step instructions, the IT Service Excellence Tool Kit delivers expert advice for technology solution providers. Get your free copy for valuable how-to assets including sample agreements, checklists, flowcharts, and more!

avernetAuthor Commented:
Thanks Brian. setting the hotkeys=no when opening the window instead of calling setHotkeys(false) seems at least to disable the "open new window" when ctrl-n is pressed feature. But I still don't receive the event. I receive it for instance for ctrl-j (which is not a hotkey in Netscape) but I don't receive the ctrl-n. Is this the way it is supposed to work, or I am overlooking something?

This is what I described in my last comment. The "hot" keys (those for which there is a ctrl modifier built in) can be disabled, but are not re-assignable.

It seems that they are just being masked out.

I believe this is how it is supposed to work.

avernetAuthor Commented:
Ok, thank you very much Brian.

It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today

From novice to tech pro — start learning today.