• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 390
  • Last Modified:

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:

<script>
    netscape.security.PrivilegeManager.enablePrivilege
        ("UniversalBrowserWrite");
    window.setHotkeys (false);
    window.captureEvents (Event.KEYPRESS);
    window.onkeypress = function (event) {
      if (event.which != 0) { alert (event.which); }
    };  
</script>

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)?
0
avernet
Asked:
avernet
  • 3
  • 2
1 Solution
 
brigmarCommented:
Try this...

<html><head><script language="JavaScript"><!--
  netscape.security.PrivilegeManager.enablePrivilege("UniversalBrowserWrite");
  open('TEST.htm','','height=100,width=200,hotkeys=no,status');
// -->
</script></head><body>LAUNCHER</body></html>

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.

Brian
0
 
Michel PlungjanIT ExpertCommented:
But avernet's suggestion should work according to the docs:

setHotKeys

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

 Method of
              window
 Implemented in
              JavaScript 1.2


Syntax

setHotKeys(trueOrFalse)

Parameters

 trueOrFalse

            A Boolean value specifying whether hot keys are enabled:

                true enables hot keys

                false disables hot keys


Security

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

Description

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.

You can also specify whether to enable hot keys at the time you create a window when you use the window.open 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,
javascript:netscape.security.PrivilegeManager.enablePrivilege("UniversalBrowserWrite");  self.setHotkeys(false);

does not work

javascript:winid=window.open('','newwin','location');netscape.security.PrivilegeManager.enablePrivilege("UniversalBrowserWrite");  winid.setHotkeys(false);

does work

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

Michel
0
 
brigmarCommented:
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).

Brian

0
The new generation of project management tools

With monday.com’s project management tool, you can see what everyone on your team is working in a single glance. Its intuitive dashboards are customizable, so you can create systems that work for you.

 
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?

Alex
0
 
brigmarCommented:
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.

Brian
0
 
avernetAuthor Commented:
Ok, thank you very much Brian.

Alex
0

Featured Post

The new generation of project management tools

With monday.com’s project management tool, you can see what everyone on your team is working in a single glance. Its intuitive dashboards are customizable, so you can create systems that work for you.

  • 3
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now