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

Detecting pressed key during onChange Event

I am wondering if it's possible in Firefox (I know IE can do this) to detect what key a user is holding down during an onChange event on a select tag.

If so, how?
0
chadsaun
Asked:
chadsaun
  • 5
  • 4
1 Solution
 
ZvonkoSystems architectCommented:
Show your IE version and we can extend it for FF.
0
 
chadsaunAuthor Commented:
Here is my code...

function whatever2(oEvent) {
      alert(oEvent.ctrlKey);
      if(oEvent.ctrlKey) {
            alert("ctrlKey!");
      }
      alert(oEvent.keyCode);
}

for this piece of HTML...

<select id="selStuff" onChange="whatever2(event)">
      <option value="one">One</option>
      <option value="two">Two</option>
      <option value="three">Three</option>
      <option value="four">Four</option>
</select>

In IE, this will alert "true", then "ctrlKey!", and then 0

In Firefox, this will alert "undefined", "undefined"
0
 
ZvonkoSystems architectCommented:
0
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
chadsaunAuthor Commented:
Thanks, I've already visited that site though before I came here and couldn't come up with an alternative....yet.
0
 
ZvonkoSystems architectCommented:
It seams in onChange event handler are NO keystrokes attributes available.
So far this is the closest approach that I could get:

<html>
<head>
<title>Zvonko &#42;</title>
<script>
var mask = 0;
var keyCode = 0;
function whatever2(oEvent) {
  var ctrlKey = (oEvent.ctrlKey)?oEvent.ctrlKey:mask&2;
    //alert(ctrlKey);
    if(ctrlKey) {
      alert("ctrlKey!");
    }
    //alert(keyCode);
}
function storeMask(e){
  mask = (e.altKey)?1:0;
  mask += (e.ctrlKey)?2:0;
  mask += (e.shiftKey)?4:-0;
  keyCode = (e.which)?e.which:e.keyCode;
}

</script>
<body>
<form>
<select id="selStuff" onChange="whatever2(event)" onKeyDown="storeMask(event)" onKeyUp="storeMask(event)">
     <option value="one">One</option>
     <option value="two">Two</option>
     <option value="three">Three</option>
     <option value="four">Four</option>
</select>
</form>
</body>
</html>

0
 
chadsaunAuthor Commented:
That's a good try Zvonko. Not quite what I'm looking for though. I'd like to have to require them to be holding down the ctrl Key when they change the option.

This might not be possible since IE does give you the key properties even when it's not a "key" event like onChange. I know Firefox doesn't give you those attributes but I was looking for an alternative that worked the same.

I don't know if this is possible, but can you put an event listener on another event listener?
0
 
ZvonkoSystems architectCommented:
You can. But then you get only the attributes and properties of the current event handler.
That does say: you can call in onChange event handler the onKeyPress event handler, but you get only onchange attributes.


0
 
chadsaunAuthor Commented:
Oh I see. Okay.
0
 
chadsaunAuthor Commented:
I found that it wasn't possible in Firefox. Only IE since the keystrokes attributes are only available in IE. Thanks for the help Zvonko.
0

Featured Post

New feature and membership benefit!

New feature! Upgrade and increase expert visibility of your issues with Priority Questions.

  • 5
  • 4
Tackle projects and never again get stuck behind a technical roadblock.
Join Now