...

View Full Version : NS7 doesn't recognize event.altKey && event.ctrlKey, what can I do?



ConfusedOfLife
04-03-2003, 12:34 PM
Hi, I have this code:



<form>
<input type="text" id="oopse" onkeydown="if ( event.altKey && event.ctrlKey ) alert(' you pressed alt + ctrl');">
</form>


but it doesn't work in NS7, what can I do?

liorean
04-03-2003, 04:32 PM
Yes it does - see <http://www.din.or.jp/~hagi3/JavaScript/JSTips/Mozilla/Samples/KeyEvent.htm>.

You have to be careful with what you capture of keydown, keypress and keyup. They vary in what they support between browsers and between eachother in the same browser.

Key events are tricky - no browser does it in exactly the same way.

ConfusedOfLife
04-04-2003, 10:53 PM
Thank you liorean, you really helped!

I acutally wanted to have the same functionality that IE provides in NS too:


if ( event.altKey && event.ctrlKey ) doSomething();


But by going to that page and testing it in NS7, I found out that it's nothing wrong with event.altKey nor event.ctrlKey, the only thing that wasn't like IE was that I couldn't and them, that is if both of them are pressed then doSomething() gets excecuted. Of course you can never press two buttons simultaniously from the computer point, but from the human's view it's a normal thing, you know what I'm talking about?!

So, I wrote this dummy script that simulates the same functionality in NS or any other browser ( based on what your page provides ).


<body>
<input type="text" upKey="" dnKey="" onkeydown="check( event, this,'down');" onkeyup="check(event, this,'up')" onkeypress="return false;">
<br /><br />

<script>
function check( damnEvent, w, onkey )
{
key = damnEvent.keyCode;
if ( w.getAttribute("upKey") == w.getAttribute("dnKey" ) != "" )
{
w.setAttribute("dnKey", "");
w.setAttribute("upKey", "");
}
switch (onkey)
{
case 'up':
if (
(key == 17 && w.getAttribute("dnKey") == 18) ||
(key == 18 && w.getAttribute("dnKey") == 17)
)
alert("HaHa, I got you!");
else
w.setAttribute("upKey", key);

break;
case 'down':
if (
(key == 17 && w.getAttribute("upKey") == 18) ||
(key == 18 && w.getAttribute("upKey") == 17)
)
alert("HaHa, I got you!");
else
w.setAttribute("dnKey", key);

break;

}
}
</script>

</body>


What's your idea? :p Also please rewrite the check() function with your own C/C++ style if you have time, I really wana see if it could be written simpler than this. ( Doesn't matter if you don't, just cuorisity! ).

bijan

liorean
04-05-2003, 12:31 AM
Let's see if this is simpler or just wierder.

Use these on the input element: onkeydown="return fnCheck();" onkeyup="return fnCheck();" onkeypress="return fnCheck();"

function fnCheck(e){
e=e||event;
return ({
keyup:
function(k,t){
(k==17&&t['keydown']==18)||(k==18&&['keydown']=17)?
alert('Haha, I got you!'):
t['keyup']=k;
return true;
},
keypress:
function(){
return false;
},
keydown:
function(k,t){
(k==17&&t['keyup']==18)||(k==18&&['keyup']=17)?
alert('Haha, I got you!'):
t['keydown']=k;
return true;
}
})[e.type](e.keyCode,e.target||e.srcElement);
}

ConfusedOfLife
04-05-2003, 11:55 AM
Ok ok ok, I surrender! Your code is much simpler to YOU and wierder to me! I confess that I couldn't understand even half of your code ( ok, I know I'm weaker, fineeeeeeeeee ) but I really enjoyed when I saw that! So, what's your idea on the whole thing? Did you like it? Did it work for you? Of course it only has one simple problemo: If you press the Alt key and while you are holding it, you press the Ctrl key and then leave the Ctrl key it doesn't work. Huhu! Actually you shouldn't be a baby working with the keyboard, I mean if you press Alt+Ctrl fast ( pressing Alt then pressing Ctrl while Alt is still down, leaving Alt and then Ctrl or exactly vice versa --- the way that we all type ) it definitely answers, what do you think?

bijan
PS: Also your code gave me an error on line 6, where you write that function(k,t) for the first time.

liorean
04-05-2003, 01:01 PM
I'm too lazy - I didn't test it. I seldom do if I have to write an html page for testing the script, too.

I know the language good enough to most of the time output working, bug free code.

If you change the even handlers to onevent="return fnCheck(event);", does it still throw an error?

jkd
04-05-2003, 07:23 PM
Originally posted by liorean
If you change the even handlers to onevent="return fnCheck(event)[}", does it still throw an error?

I'd hope so. :p

liorean
04-05-2003, 07:37 PM
Oops- corrected.

Hmm, nice set of braces/parens I created there - ({})[]() - but at least they're functional. I create an object literal, access a method of that literal, and execute it - all in one statement. The result is then the return value of that execution.

ConfusedOfLife
04-05-2003, 08:37 PM
Well, I'm gonna test it, but you know I just wanted to see how YOU code it liorean. Ok, now that JS Gods are here (I'm talking to you too Jason), what do you think about that little piece of code? Do you think that it's wise if I use it in my scripts instead of ( event.altKey && event.ctrlKey ) ??

liorean
04-05-2003, 08:43 PM
e.altKey etc. seems best - that way you can capture whether a key is held down during other key presses as well as when it's pressed down or released. If you want distinguish whether the user pressed alt/ctrl or was just holding it down while pressing another key, you have to check for 17/18.

Also, I noticed how Opera 7 gave another number, far larger. (Somewhere aaround 58k or so, I believe.)

ConfusedOfLife
04-05-2003, 08:51 PM
So, you agree and you say that I only have to replace event.altKey with e.altKey, right? Well, as for as Opera, I don't think that I really go for that too! I don't have time for it and not many ppl use it in here.



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum