keycode for ctrl ??

O

oeyvind toft

How do I capture the ctrl keypress?

Is it possible for js to intercept the ctrl-n combination and prevent
the creation of a new IE window??

(Have 3 huge books on js and none of them mention this subject)


Oeyvind
 
E

Evertjan.

oeyvind toft wrote on 22 aug 2003 in comp.lang.javascript:
How do I capture the ctrl keypress?

Is it possible for js to intercept the ctrl-n combination and prevent
the creation of a new IE window??

(Have 3 huge books on js and none of them mention this subject)

<body
oncontextmenu="if(event.ctrlKey){alert('ctrl-rightclicked');return false}">


<input onkeydown="if(event.keyCode==17)alert('ctrl key pressed')">
 
L

Lasse Reichstein Nielsen

oeyvind toft said:
How do I capture the ctrl keypress?

You probably want the ctrl keydown, not keypress.

Make any keydown event handler, even
document.onkeydown = function(event){
event = event || window.event; //IE suckes
if ( event.keyCode == 17 ) {
// ctrl pressed
}
}
Is it possible for js to intercept the ctrl-n combination and prevent
the creation of a new IE window??

That depends on the browser, where the focus is, and the phase of the
moon. That is, not always. (Well, you do say IE window, so I assume
you are mostly interested in IE).

This might work in some browsers, including IE6.
document.onkeydown = function(e) {
e=e||window.event;
if (e.keyCode == 78) {
return false;
}
}

The more relevant question is: Why would you think you needed
to do this? Because I am pretty certain that you don't. After all,
you can still open new windows in many other ways.
(Have 3 huge books on js and none of them mention this subject)

Good books, then. They shouldn't tell you how to interfere with the
users' keyboard shortcuts. They are there for a reason, you know.

Sometimes I wish I had a blacklist, just like my bookmarks. If I
blacklist a page, my browser will never send me back to it without
a warning. Then I could blacklist pages that interfered with my
keyboard.

/L
 
L

Lasse Reichstein Nielsen

Lasse Reichstein Nielsen said:
if (e.keyCode == 78) {

Oops. Change this to
if (e.keyCode == 78 && e.ctrlKey) {
unless you want to turn off the n key completely.

/L
 
L

Lasse Reichstein Nielsen

oeyvind toft said:
The reason for blocking ctrl-n is this:

A Flash educational movie embedded in IE is supposed to simulate
various software. The user should be able to use ctrl-n and have it
passed to Flash, at the same time prevent opening another IE window.

They should look into embedding the Flash in an HTML application instead
of a normal browser window. Then most of these problems goes away.
I didnt mean the books lacks info on interfering with user`s
shortcuts, I meant I couldnt find the keycodes themselves.

Bad books, then :)

/L
 
O

oeyvind toft

Thanks again Lasse...

Yup, bad books allright...I cant even find key/keyCode anywhere in the
indicies.

hta looks interesting and I`ll forward the link to my "client".

Oeyvind
 
R

Richard Cornford

Bad books, then :)

I am not so sure. Are keycodes universal or might they be expected to
vary with OS, keyboard layout, local language, etc? Mozilla key events
have a battery of numeric properties such as:-

DOM_VK_CANCEL == 3
DOM_VK_CAPS_LOCK == 20
DOM_VK_CLEAR == 12
DOM_VK_CLOSE_BRACKET == 221
DOM_VK_COMMA == 188
DOM_VK_CONTROL == 17
DOM_VK_D == 68
DOM_VK_DECIMAL == 110
DOM_VK_DELETE == 46
DOM_VK_DIVIDE == 111
DOM_VK_DOWN == 40

- that look like constants (all uppercase, underline separated
identifiers (example values from Window OS/UK English keyboard)) mapping
keys to codes. I imagine that Mozilla would refer to the named property
when making decisions about keyboard input, allowing the number assigned
to vary with the OS (and/or whatever else was relevant).

Richard.
 
L

Lasse Reichstein Nielsen

Richard Cornford said:
Are keycodes universal or might they be expected to vary with OS,
keyboard layout, local language, etc?

They vary.
Some are mostly standardized. The normal letter and digit keys are
represented by the ASCII value of the letter or digit. Even for these
there are differences.

In IE6, Opera 7.2 and Mozilla FB 0.6, pressing "a" and "shift+a" both
give the keyCode 65 (capital A).

In Netscape 4, pressing "a" gives 97, but "shift+a" gives 65.

Opera 7.2 (still in beta) changed the keycodes of some keys (e.g.,
cursor keys) to be compatible with IE. Earlier ones weren't.

/L
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
474,079
Messages
2,570,575
Members
47,207
Latest member
HelenaCani

Latest Threads

Top