473,324 Members | 2,214 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,324 software developers and data experts.

KeyPress Events in IE

Is there any way in JavaScript to determine which key has been pressed
during any of the key-press, key-down, key-up events in IE6+? It
appears the "which" property only works for Netscape.
Jul 20 '05 #1
4 19461
I can help you with this!

In netscape, it is evt.which. In IE, it's event.keyCode. Here's a snippet
of code I use:

function onlyNumbersXB(obj, e, mask) {

var keyCode;
var returnVal = false;

if (window.event) {
e = window.event;
keyCode = e.keyCode;
} else {
// a key, such as delete, was pressed; let it pass through
if (e.keyCode == e.which) keyCode = null;
// else if not a char we don't know what this is; let it pass through
else if (e.charCode != e.which) keyCode = void 0;
// else a char - set it
else keyCode = e.which;
}

// do whatever you want based on keyCode...

// now for the return
if (window.event) {
e.returnValue = returnVal;
return returnVal; // not really necessary but I'm trying to get
Safari to work
} else
return returnVal;
}

}

This method is invoked on a text field, onKeyPress='return
onlyNumbersXB(this, event, mask)'

My problem, as posted elsewhere, is getting Safari to behave itself. It has
window.event and behaves that way. It sets e.returnValue appropriately. It
still doesn't recognize "false" and let's the key pass.

Anyone?

Julia

"Ron Brooks" <ro*******@dendress.com> wrote in message
news:25**************************@posting.google.c om...
Is there any way in JavaScript to determine which key has been pressed
during any of the key-press, key-down, key-up events in IE6+? It
appears the "which" property only works for Netscape.

Jul 20 '05 #2
"Jules" <ju*******@yahoo.com> wrote in message
news:12******************************@news.teranew s.com...
<snip>
My problem, as posted elsewhere, is getting Safari to behave itself.
It has window.event and behaves that way. It sets e.returnValue
appropriately. It still doesn't recognize "false" and let's the
key pass.


Did you try the W3C DOM events - preventDefault - method, combined with
checking that the event is cancelable by checking the event's W3C DOM
(boolean) - cancelable - property? Also (assuming that is no-go) have
you considered trying to cancel onkeydown and/or onkeyup instead of (or
in addition to) onkeypress?

Richard.
Jul 20 '05 #3

"Richard Cornford" <Ri*****@litotes.demon.co.uk> wrote in message
news:bm*******************@news.demon.co.uk...
"Jules" <ju*******@yahoo.com> wrote in message
news:12******************************@news.teranew s.com...
<snip>
My problem, as posted elsewhere, is getting Safari to behave itself.
It has window.event and behaves that way. It sets e.returnValue
appropriately. It still doesn't recognize "false" and let's the
key pass.
Did you try the W3C DOM events - preventDefault - method, combined with
checking that the event is cancelable by checking the event's W3C DOM
(boolean) - cancelable - property?


I did try preventDefault() -- no go. I don't know what you mean by
"combined with if the event is cancelable". What would combining the two
do?

Also (assuming that is no-go) have
you considered trying to cancel onkeydown and/or onkeyup instead of (or
in addition to) onkeypress?


Yea - except we have problems with onKeyDown and Netscape 7. That's why we
went with onKeyPress.

Canceling keystrokes is something that whole documents have been written
about regarding IE and Netscape. I can't believe that I can be the only
person struggling with this with Safari...

Julia
Jul 20 '05 #4
"Jules" <ju*******@yahoo.com> wrote in message
news:92******************************@news.teranew s.com...
<snip>
I did try preventDefault() -- no go. I don't know what you
mean by "combined with if the event is cancelable". What
would combining the two do?
I mean that if the event object has the W3C DOM events specified
boolean - cancelable - property and it is false then there is not much
point in calling its - preventDefault - method as the event cannot be
cancelled.
Also (assuming that is no-go) have you considered trying to
cancel onkeydown and/or onkeyup instead of (or
in addition to) onkeypress?


Yea - except we have problems with onKeyDown and Netscape 7.
That's why we went with onKeyPress.

Canceling keystrokes is something that whole documents have
been written about regarding IE and Netscape. I can't
believe that I can be the only person struggling with this
with Safari...
Validating user input as they type is problematic at best, clear
instructions often make it unnecessary and you usually won't catch paste
operations so the field would still need to be validated onsubmit
anyway. These days I probably would not bother so I do not know any
currently recommended method.

However, though I don't have access to a Mac at present so I cannot test
Safari, I would expect Konqueror (on which Safari is based) to exhibit
the same problem, so if you could make up a minimal test case page that
does exactly what you require where it is working now (and preferably
nothing else) and post it (or a URL to an online version) then I (and/or
someone else) would probably have a look at it and see if it cannot be
made to do what you want.
Julia


That is not a name you see often these days. You share it with my sister
:)

Richard.
Jul 20 '05 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

12
by: Trevor Fairchild | last post by:
I'm writing a program that is designed for quick navigation using specific buttons on the keyboard. In doing this, I have added a KeyPress event for every control on the form to intercept the key...
3
by: Peter Verburgh | last post by:
Is there an easy way to capture the keypress - keydown events from a LABEL control ? Standard the control doesn't handle these events. The label control inherits from the control class , and this...
1
by: Darren Coleman | last post by:
I need help with 2 keyboard/input questions 1. How do i caputre all keypress events for my application/form? 2. How do I determine which keyboard sent the keypress? What i'm doing is using a...
3
by: Raul M. Colon | last post by:
Is possible to assign a click event to a button control in a Web form just pressing the return key? Something like windows forms where you can assign this action to a default control. For example,...
2
by: rege | last post by:
Hi, I have two applications developed in C# say A and B. When user presses key "L" with applicaition A active , it causes a keypress event which causes application B to run. Application A then...
15
by: Adam J. Schaff | last post by:
I have noticed that if a user closes a form via pressing return (either while the OK button has focus or if AcceptButton is set to OK for the form) then the "ENTER" keypress event fires ON THE...
3
by: Just Me | last post by:
I noticed that I don't get KeyDown nor KetPress events if NumLock is not on. I can probably find a workaround but it would be nice to know what the rules are. Can anyone tell me when KeyPress...
3
by: Fia | last post by:
Hi In Visual Basic 6 I could call keypress with an integer of a choosen key. For example I could call a textbox's keypress with the number 13 (for the enter key). But now in Visual Basic .Net I...
1
by: centur | last post by:
Hello. I created simple asp.net application to search message history over Office Live Communicator conversations. I complete quite all job but stick with one thing - I have autosuggest box...
2
by: Tony Johansson | last post by:
Hello! I have created a Control that consist of a label and a textbox.I have called this class ctlLabelTextbox. public partial class ctlLabelTextbox : UserControl { .... } The class that I...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.