Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 11 of 11
  1. #1
    Regular Coder
    Join Date
    Jul 2002
    Location
    Iran
    Posts
    695
    Thanks
    0
    Thanked 0 Times in 0 Posts

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

    Hi, I have this code:

    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?

  • #2
    Master Coder
    Join Date
    Feb 2003
    Location
    UmeŚ, Sweden
    Posts
    5,575
    Thanks
    0
    Thanked 83 Times in 74 Posts
    Yes it does - see <http://www.din.or.jp/~hagi3/JavaScri...s/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.
    liorean <[lio@wg]>
    Articles: RegEx evolt wsabstract , Named Arguments
    Useful Threads: JavaScript Docs & Refs, FAQ - HTML & CSS Docs, FAQ - XML Doc & Refs
    Moz: JavaScript DOM Interfaces MSDN: JScript DHTML KDE: KJS KHTML Opera: Standards

  • #3
    Regular Coder
    Join Date
    Jul 2002
    Location
    Iran
    Posts
    695
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Thank you liorean, you really helped!

    I acutally wanted to have the same functionality that IE provides in NS too:
    Code:
    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 ).
    Code:
    <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? 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
    Last edited by ConfusedOfLife; 04-04-2003 at 09:57 PM.

  • #4
    Master Coder
    Join Date
    Feb 2003
    Location
    UmeŚ, Sweden
    Posts
    5,575
    Thanks
    0
    Thanked 83 Times in 74 Posts

    Aha! A Challenge!

    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();"
    Code:
    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);
    }
    Last edited by liorean; 04-05-2003 at 12:05 AM.
    liorean <[lio@wg]>
    Articles: RegEx evolt wsabstract , Named Arguments
    Useful Threads: JavaScript Docs & Refs, FAQ - HTML & CSS Docs, FAQ - XML Doc & Refs
    Moz: JavaScript DOM Interfaces MSDN: JScript DHTML KDE: KJS KHTML Opera: Standards

  • #5
    Regular Coder
    Join Date
    Jul 2002
    Location
    Iran
    Posts
    695
    Thanks
    0
    Thanked 0 Times in 0 Posts
    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.

  • #6
    Master Coder
    Join Date
    Feb 2003
    Location
    UmeŚ, Sweden
    Posts
    5,575
    Thanks
    0
    Thanked 83 Times in 74 Posts
    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?
    Last edited by liorean; 04-05-2003 at 06:29 PM.
    liorean <[lio@wg]>
    Articles: RegEx evolt wsabstract , Named Arguments
    Useful Threads: JavaScript Docs & Refs, FAQ - HTML & CSS Docs, FAQ - XML Doc & Refs
    Moz: JavaScript DOM Interfaces MSDN: JScript DHTML KDE: KJS KHTML Opera: Standards

  • #7
    jkd
    jkd is offline
    Senior Coder jkd's Avatar
    Join Date
    May 2002
    Location
    metro DC
    Posts
    3,163
    Thanks
    1
    Thanked 18 Times in 18 Posts
    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.

  • #8
    Master Coder
    Join Date
    Feb 2003
    Location
    UmeŚ, Sweden
    Posts
    5,575
    Thanks
    0
    Thanked 83 Times in 74 Posts
    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.
    liorean <[lio@wg]>
    Articles: RegEx evolt wsabstract , Named Arguments
    Useful Threads: JavaScript Docs & Refs, FAQ - HTML & CSS Docs, FAQ - XML Doc & Refs
    Moz: JavaScript DOM Interfaces MSDN: JScript DHTML KDE: KJS KHTML Opera: Standards

  • #9
    Regular Coder
    Join Date
    Jul 2002
    Location
    Iran
    Posts
    695
    Thanks
    0
    Thanked 0 Times in 0 Posts
    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 ) ??

  • #10
    Master Coder
    Join Date
    Feb 2003
    Location
    UmeŚ, Sweden
    Posts
    5,575
    Thanks
    0
    Thanked 83 Times in 74 Posts
    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.)
    liorean <[lio@wg]>
    Articles: RegEx evolt wsabstract , Named Arguments
    Useful Threads: JavaScript Docs & Refs, FAQ - HTML & CSS Docs, FAQ - XML Doc & Refs
    Moz: JavaScript DOM Interfaces MSDN: JScript DHTML KDE: KJS KHTML Opera: Standards

  • #11
    Regular Coder
    Join Date
    Jul 2002
    Location
    Iran
    Posts
    695
    Thanks
    0
    Thanked 0 Times in 0 Posts
    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.


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •