Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.javascript > #19088 > unrolled thread

Determining the key pressed within an HTML onkeypress event handler

Started by"John L." <johnlarew@sbcglobal.net>
First post2013-03-31 15:06 -0700
Last post2013-03-31 17:02 -0700
Articles 3 — 2 participants

Back to article view | Back to comp.lang.javascript


Contents

  Determining the key pressed within an HTML onkeypress event handler "John L." <johnlarew@sbcglobal.net> - 2013-03-31 15:06 -0700
    Re: Determining the key pressed within an HTML onkeypress event handler Tim Streater <timstreater@greenbee.net> - 2013-03-31 23:33 +0000
      Re: Determining the key pressed within an HTML onkeypress event handler "John L." <johnlarew@sbcglobal.net> - 2013-03-31 17:02 -0700

#19088 — Determining the key pressed within an HTML onkeypress event handler

From"John L." <johnlarew@sbcglobal.net>
Date2013-03-31 15:06 -0700
SubjectDetermining the key pressed within an HTML onkeypress event handler
Message-ID<85567ec3-133a-4d70-8deb-301ff0876779@googlegroups.com>
I have an HTML JavaScript similar to this:

<HTML>
<SCRIPT>
function pressed() {
   window.alert("Key pressed");
}
</SCRIPT>
<BODY onkeypress="javascript: pressed();">
</BODY>
</HTML>

How can I tell which key was pressed? The JavaScript reference I use describes both Event and KeyboardEvent objects, but gives no examples as to how to access them or use them.

Thanks in advance for any help you can provide. 

[toc] | [next] | [standalone]


#19089

FromTim Streater <timstreater@greenbee.net>
Date2013-03-31 23:33 +0000
Message-ID<timstreater-9A6438.23334731032013@news.individual.net>
In reply to#19088
In article <85567ec3-133a-4d70-8deb-301ff0876779@googlegroups.com>,
 "John L." <johnlarew@sbcglobal.net> wrote:

> I have an HTML JavaScript similar to this:
> 
> <HTML>
> <SCRIPT>
> function pressed() {
>    window.alert("Key pressed");
> }
> </SCRIPT>
> <BODY onkeypress="javascript: pressed();">
> </BODY>
> </HTML>
> 
> How can I tell which key was pressed? The JavaScript reference I use 
> describes both Event and KeyboardEvent objects, but gives no examples as to 
> how to access them or use them.
> 
> Thanks in advance for any help you can provide. 


function donkeyPress (evt)
     {

     userVal  = evt.charCode;
     userChar = String.fromCharCode (userVal);

     }


function donkeyUp (elPtr, evt)
     {

     if  (userVal>0)
          {
          // Process real char-key pressed by user
          }
     else if  (evt.keyCode==8)
          {
          // process backspace here
          }
     else if  (evt.keyCode==some_other_value)
          {
          // process some_other_value here
          }
     else etc

     }

Or possibly use a switch statement. The keyCode values are those I've 
determined empirically by testing. Since I'm only using one browser 
(Safari under OS X) I've not looked into it more deeply that that and it 
satisfies the needs.

As for donkeyPress() and donkeyUp(), I attach those to the element 
accepting the data, in my case a textarea:

tohdrPtr = document.getElementById ("tohdr");
tohdrPtr.onkeypress = donkeyPress;
tohdrPtr.onkeyup    = function (evt) { donkeyUp (tohdrPtr, evt);};

Here, tohdrPtr is a pointer to my textarea:

<textarea id="tohdr"></textarea>

The code you point in your donkey functions depends on what you are 
doing. Also, as I have a restricted environment to worry about (one 
browser one platform), it's possible (even likely) that the actual codes 
returned may vary across other platforms, browsers (I haven't looked 
into it). Other posters may add something about that, but charCode and 
keyCode are where you'll find what the user pressed.

-- 
Tim

"That excessive bail ought not to be required, nor excessive fines imposed,
nor cruel and unusual punishments inflicted"  --  Bill of Rights 1689

[toc] | [prev] | [next] | [standalone]


#19090

From"John L." <johnlarew@sbcglobal.net>
Date2013-03-31 17:02 -0700
Message-ID<9af2af35-8836-488e-a5fd-926609b2c3f2@googlegroups.com>
In reply to#19089
Thanks for something... I'll adapt as needed.

[toc] | [prev] | [standalone]


Back to top | Article view | comp.lang.javascript


csiph-web