How to detect Key Press that works for IE, Firefox etc ?

K

Krist

Hi All,

In our web application we want to disable user from pressing CTRL key.
and we want to implement this for IE, Firefox and if possible other
browser.

I have tried this code, doesn't work :

<script type="text/javascript">
document.onkeyup = KeyCheck;
function KeyCheck()
{
var KeyID = (window.event) ? event.keyCode : e.keyCode;
if ( KeyID == 17) {
alert("Test Alert System");
}
}
</script>

Pls help me to fix the code...

Thank you very much,
Krist
 
T

The Natural Philosopher

Krist said:
Hi All,

In our web application we want to disable user from pressing CTRL key.
and we want to implement this for IE, Firefox and if possible other
browser.

I have tried this code, doesn't work :

<script type="text/javascript">
document.onkeyup = KeyCheck;
function KeyCheck()
{
var KeyID = (window.event) ? event.keyCode : e.keyCode;
if ( KeyID == 17) {
alert("Test Alert System");
}
}
</script>

Pls help me to fix the code...

Thank you very much,
Krist
I think you will find that since javascript only works INSIDE a browser,
it can only deal with key presses that the operating system passes
through to the browser.

CTTRL is not necessarily one of those, since at least on windows, you
NEED Ctrl-Alt-del to maintain normal operation of the computer.;-0
 
A

Amrit Ranjan

I think you will find that since javascript only works INSIDE a browser,
it can only deal with key presses that the operating system passes
through to the browser.

CTTRL is not necessarily one of those, since at least on windows, you
NEED Ctrl-Alt-del to maintain normal operation of the computer.;-0

Hi your code will work fine with IE, Safari, Chrome, Opera except
Mozilla. It is because the function definition KeyCheck() should look
like KeyCheck(e). Actually Mozilla passes event object as an
arguments. I had added "e" with your code. Do you want to cancel this
event?

function KeyCheck(e)
{
var KeyID = (window.event) ? event.keyCode : e.keyCode; //e is not
global variable is is the argument
if ( KeyID == 17) {
alert("Test Alert System");
}
}
 
K

Krist

Hi your code will work fine with IE, Safari, Chrome, Opera except
Mozilla. It is because the function definition KeyCheck() should look
like KeyCheck(e). Actually Mozilla passes event object as an
arguments. I had added "e" with your code. Do you want to cancel this
event?

function KeyCheck(e)
{
   var KeyID = (window.event) ? event.keyCode : e.keyCode; //e is not
global variable is is the argument
   if ( KeyID  == 17) {
        alert("Test Alert System");
   }



}- Sembunyikan teks kutipan -

- Perlihatkan teks kutipan -- Sembunyikan teks kutipan -

- Perlihatkan teks kutipan -

Hi Sir,
Thank you for your reply. further question..

To tell the page to run KeyCheck, I use this code :

document.onkeyup = KeyCheck;

How to make it conditionally call KeyCheck or KeyCheck(event) based on
what browser it is running ?

Thank you,
Krist
 
J

JR

Hi All,

In our web application we want to disable user from pressing CTRL key.
and we want to implement this for IE, Firefox and if possible other
browser.

I have tried this code, doesn't work :

<script type="text/javascript">
document.onkeyup = KeyCheck;
function KeyCheck()
{
   var KeyID = (window.event) ? event.keyCode : e.keyCode;
   if ( KeyID  == 17) {
        alert("Test Alert System");
   }}

</script>

Pls help me to fix the code...

Thank you very much,
Krist

Use document.onkeydown and the code below:

<script type="text/javascript">
document.onkeydown = keyCheck;
function keyCheck(e) {
e = e || window.event;
if (e.ctrlKey) {
alert("CTRL key is not allowed");
}
}
</script>

Cheers,
JR
 
K

Krist

Use document.onkeydown and the code below:

<script type="text/javascript">
  document.onkeydown = keyCheck;
  function keyCheck(e) {
    e = e || window.event;
    if (e.ctrlKey) {
      alert("CTRL key is not allowed");
    }
  }
</script>

Cheers,
JR- Sembunyikan teks kutipan -

- Perlihatkan teks kutipan -

Thanks JR,
That is what I need.

God bless you
 
T

Thomas 'PointedEars' Lahn

Responsible developers feature-test host object's properties instead, and
use this only as a fallback for the standards-compliant approach.

Reasonable people use a function expression here instead.

Reasonable people use an `if' statement here instead.

Responsible developers feature-test host object's properties before they are
accessing them in a type-converting test. Then again, responsible
developers would not try this whole nonsense in the first place.

window.alert(...);

but neither should not be used here.
}
}
</script>

Cheers,
[...]

Thanks JR,
That is what I need.

No, what you *need* is a dosage of common sense. Leave *my* keyboard alone!
God bless you

Said the blind led by the blind.


PointedEars
 
G

Garrett Smith

Austin said:
Would you please elaborate? I'm curious why an "if" statement would
be better.
Yes, what is unreasonable about that?
The statement "e = e || window.event" appears in a
comp.lang.javascript FAQ example: <http://www.jibbering.com/faq/
#FAQ4_26>

So it does!

I like this idiom because it is more concise. I would like it even
better as:-

// Invalid syntax. This code will result in error.
e ||= window.event;

But alas, no such ||= operator.

About the closest I can getis

e = e || window.event;

How did you arrive at the URL you posted? The FAQ itself links to:
http://jibbering.com/faq/#detectBrowser
 
J

JR

Exactly! Thank you, that needed to be said.

I use Ctrl a *lot*. I use it to operate my browser, to scroll the page,
to copy and paste text, etc. If I saw a site where ctrl triggered a
JavaScript alert, I would automatically mentally file it as evil / scum
/ target for abuse. At the very least, I'd add it to my list of blocked
sites. If I'm bored I might have a look around and see what else looked
broken. It's not very clever to annoy your guests.

Why people always tend to generalize particular cases in c.l.js? Who
said the CTRL key should be blocked in the whole site. What if it's
done in a specific page for an intranet app?
 
J

JR

Responsible developers feature-test host object's properties instead, and
use this only as a fallback for the standards-compliant approach.

Feature-test document in a web browser? It seems to be a naive
advice...

Reasonable people use a function expression here instead.

Is it really necessary or an alternative?
Reasonable people use an `if' statement here instead.

You must be kidding.
Responsible developers feature-test host object's properties before they are
accessing them in a type-converting test.  Then again, responsible
developers would not try this whole nonsense in the first place.

  window.alert(...);

but neither should not be used here.
}
}
</script>
Cheers,
[...]
Thanks JR,
That is what I need.

No, what you *need* is a dosage of common sense.

You should look at the man in the mirror because it applies to him
too.
Said the blind led by the blind.

Poor thing! Thomas was feeling blue because no one thanked him first.
 
T

Thomas 'PointedEars' Lahn

Austin said:
Would you please elaborate? I'm curious why an "if" statement would
be better.

It saves one needless evaluation step, and because it allows for a block
statement it allows for a finer control, including proper feature-testing.
The statement "e = e || window.event" appears in a
comp.lang.javascript FAQ example: <http://www.jibbering.com/faq/
#FAQ4_26>

That's a bug.


PointedEars
 
A

Asen Bozhilov

Thomas said:
It saves one needless evaluation step

What is the difference between:

var foo = 0 || 1; //1

AND

var foo;
if (0)
{
foo = 0;
}
else {
foo = 1;
}

In concrete case which discussed here, you are absolutely right. But
in general plan i don't see any difference. If you see, please
explain.

Regards.
 
A

Asen Bozhilov

'PointedEars' Lahn said:
Asen Bozhilov wrote:
But you needed to post something anyway.

if (!e)
{
e = window.event;
}

It is enough?

However, in different case you can be incorrect.

| 11.11 Binary Logical Operators
| The production
| LogicalORExpression : LogicalORExpression || LogicalANDExpression
| is evaluated as follows:
| 1. Evaluate LogicalORExpression.
| 2. Call GetValue(Result(1)).
| 3. Call ToBoolean(Result(2)).
| 4. If Result(3) is true, return Result(2).
| 5. Evaluate LogicalANDExpression.
| 6. Call GetValue(Result(5)).
| 7. Return Result(6).


| 12.5 The if Statement
| The production
| IfStatement : if ( Expression ) Statement else Statement
| is evaluated as follows:
| 1. Evaluate Expression.
| 2. Call GetValue(Result(1)).
| 3. Call ToBoolean(Result(2)).
| 4. If Result(3) is false, go to step 7.
| 5. Evaluate the first Statement.
| 6. Return Result(5).
| 7. Evaluate the second Statement.
8. Return Result(7).

Again, i don't see any differences between my two examples in previous
post. Because of that i don't think the part in FAQ is bug.

Regards.
 
A

Asen Bozhilov

Thomas 'PointedEars' Lahn said:
[...]
In concrete case which discussed here, you are absolutely right.

But you needed to post something anyway.

Actually you are absolutely right. The part in FAQ is the same as like
that been discussed here. And again have one evaluation step much
more.

e = e || window.event;

In any case will be:

1. Evaluate LeftHandSideExpression.
2. Evaluate AssignmentExpression.
3. GetValue(e).
4. ToBoolean(3).
5. If results 4 is true. Return 3.
6. PutValue(1, 5);
 
T

Thomas 'PointedEars' Lahn

Asen Bozhilov wrote:

[Fixed quotation]
if (!e)
{
e = window.event;
}

It is enough?

Probably not, and I pointed that out, too.
However, in different case you can be incorrect.

I am correct in this case and other cases that belong to the same problem
class.
| 11.11 Binary Logical Operators
[...]

You better don't quote the Specification unless you are sure what you are
talking about.
Again, i don't see any differences between my two examples in previous
post.

Because your example targets another problem class, if any.
Because of that i don't think the part in FAQ is bug.

"Bug" was short for "suboptimal example". Quite obviously, there is no
assignment necessary when `e' type-converts to true (indeed, since `e'
refers to a host object, it is error-prone to use it in a type-converting
test). The cost thus spared may be negligible with one call, but you need
to consider how many times an event listener is likely to be called. That
is one point.


PointedEars
 
J

Jorge

It saves one needless evaluation step, and because it allows for a block
statement it allows for a finer control, including proper feature-testing..

!e && (e= window.event);
 
A

Asen Bozhilov

Thomas said:
You better don't quote the Specification unless you are sure what you are
talking about.

What is irrelevant here? I quote specification for the problem, which
are you talking about.
"Bug" was short for "suboptimal example".  Quite obviously, there is no
assignment necessary when `e' type-converts to true (indeed, since `e'
refers to a host object, it is error-prone to use it in a type-converting
test).  

You wrote "it is error-prone", but you use exactly in the same manner.

| for (var tRows = tBody.rows, i = tRows && tRows.length; i--;)

Here again `tRows` refer host object and again we have type
conversion. Please, don't suggest something which you don't observe.

Regards.
 

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
473,744
Messages
2,569,483
Members
44,901
Latest member
Noble71S45

Latest Threads

Top