Page 1 of 2 12 LastLast
Results 1 to 10 of 11

Thread: Multiple keyboard event.

  1. #1
    Join Date
    Aug 2005
    Posts
    971
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Question Multiple keyboard event.

    I am developing a key event library for myself which is the code below:

    Code:
    // functional keys
    var key_insert		= 45;
    var key_delete		= 46;
    var key_pgup		= 33;
    var key_pgdwn		= 34;
    var key_home		= 36;
    var key_end		= 35;
    var key_prtscn		= 44;
    var key_scrl_lck	= 145;
    var key_num_lck		= 144;
    var key_pause		= 19;
    
    // mathematical
    var key_modulus 	= 53;
    var key_expo		= 54;
    var key_tilde		= 192;
    var key_equal		= 61;
    
    // num pad
    var key_numpad_0	= 96;
    var key_numpad_1	= 97;
    var key_numpad_2	= 98;
    var key_numpad_3	= 99;
    var key_numpad_4	= 100;
    var key_numpad_5	= 101;
    var key_numpad_6	= 102;
    var key_numpad_7	= 103;
    var key_numpad_8	= 104;
    var key_numpad_9	= 105;
    var key_asterisk	= 106;
    var key_plus		= 107;
    var key_hyphen		= 109;
    var key_decimal		= 110;
    var key_divide		= 111;
    
    // Formatting keys(enter,backspace,etc..)
    var key_Enter		= 13;
    var key_BackSpc	= 8;
    var key_Tab		= 9;
    var key_Caps		= 20;
    var key_Shift		= 16;
    var key_Esc		= 27;
    var key_Spc		= 32;
    
    // Alt/Shift/Ctrl/Win
    var key_Shift 		= 16;
    var key_Ctrl 		= 17
    var key_Alt 		= 18;
    var key_win 		= 0;
    var key_click		= 93;
    
    // f1-f12
    var key_f1 		= 112;
    var key_f2 		= 113;
    var key_f3 		= 114;
    var key_f4 		= 115;
    var key_f5 		= 116;
    var key_f6 		= 117;
    var key_f7 		= 118;
    var key_f8 		= 119;
    var key_f9 		= 120;
    var key_f10 		= 121;
    var key_f11 		= 122;
    var key_f12 		= 123;
    
    // arrow keys
    var key_left 		= 37;
    var key_up   		= 38;
    var key_right		= 39;
    var key_down 		= 40;
    
    // alphabets
    var key_a 		= 65;
    var key_b			= 66;
    var key_c 		= 67;
    var key_d 		= 68;
    var key_e 		= 69;
    var key_f 		= 70;
    var key_g 		= 71;
    var key_h 		= 72;
    var key_i 		= 73;
    var key_j 		= 74;
    var key_k 		= 75;
    var key_l 		= 76;
    var key_m 		= 77;
    var key_n 		= 78;
    var key_o 		= 79;
    var key_p 		= 80;
    var key_q 		= 81;
    var key_r 		= 82;
    var key_s 		= 83;
    var key_t 		= 84;
    var key_u 		= 85;
    var key_v 		= 86;
    var key_w 		= 87;
    var key_x 		= 88;
    var key_y 		= 89;
    var key_z 		= 90;
    
    // numbers
    var key_0  		= 48;
    var key_1  		= 49;
    var key_2  		= 50;
    var key_3  		= 51;
    var key_4  		= 52;
    var key_5  		= 53;
    var key_6  		= 54;
    var key_7  		= 55;
    var key_8  		= 56;
    var key_9  		= 57;
    
    
    function keyListener(e){
       if(!e){
         
          e = window.event;
       }
       if(e.keyCode==key_a){
    	//some functions
     	}
    }
    
    function init(){
       document.onkeydown = keyListener; 
    }
    I can make events that run when user presses 1 key at a time but can anyone modify this script so that it also accepts multiple key presses(like ctrl+i...etc).

    I am not able to do this alone because of my negligible knowledge in javascript.
    I would be grateful if someone helps me with this problem.
    Thanks anyways.

  2. #2
    Join Date
    Jun 2005
    Location
    英国
    Posts
    11,876
    Thanks
    1
    Thanked 180 Times in 172 Posts
    Blog Entries
    2

    Default

    Ctrl, Alt, Meta, &c. are special keys, known as modifiers. They must be accessed through boolean properties of the event object. So, to check for ctrl-a:
    Code:
    if(e.keycode == key_a && e.ctrlKey)
    Early versions (I think) of Netscape use another property instead, the bitset e.modifiers.
    Twey | I understand English | 日本語が分かります | mi jimpe fi le jbobau | mi esperanton komprenas | je comprends français | entiendo español | tôi ít hiểu tiếng Việt | ich verstehe ein bisschen Deutsch | beware XHTML | common coding mistakes | tutorials | various stuff | argh PHP!

  3. #3
    Join Date
    Aug 2005
    Posts
    971
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Talking

    Oh Thanks!! Let me try that. I tried this:

    Code:
    if(e.keycode == key_a + e.ctrlKey)
    instead of

    Code:
    if(e.keycode == key_a && e.ctrlKey)
    But if this doesn't work then I am sorry but I need to bother you again.

  4. #4
    Join Date
    Aug 2005
    Posts
    971
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default You're a genius!!

    Thanks it miraculously worked!! You're a genius!!

    Quote Originally Posted by Twey
    Early versions (I think) of Netscape use another property instead, the bitset e.modifiers.
    Early versions like which?? And can you tell me about sth called "bitset e.modifiers"?? Or give me a link to it's description?? I am trying to make it work with cross-browsers.

    I am a newbie so please be explicit.

  5. #5
    Join Date
    Jun 2005
    Location
    英国
    Posts
    11,876
    Thanks
    1
    Thanked 180 Times in 172 Posts
    Blog Entries
    2

    Default

    http://www.javascripter.net/faq/ctrl_alt.htm
    The information on boolean properties is outdated; they are no longer IE-only.
    Twey | I understand English | 日本語が分かります | mi jimpe fi le jbobau | mi esperanton komprenas | je comprends français | entiendo español | tôi ít hiểu tiếng Việt | ich verstehe ein bisschen Deutsch | beware XHTML | common coding mistakes | tutorials | various stuff | argh PHP!

  6. #6
    Join Date
    Aug 2005
    Posts
    971
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default

    Ok thank you very much!!!

  7. #7
    Join Date
    Aug 2005
    Posts
    971
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default

    do you know if this works??

    Code:
    if(document.onmousemove = false){
    //do something
    } else {
    //go on...
    }
    I mean if there is something that detects if the mouse is moving or not?? I am making a page which should be changed after the mouse is'nt moved for a certain time. Something like a screensaver??

    I hope I was able to clear my point.

  8. #8
    Join Date
    Jun 2005
    Location
    英国
    Posts
    11,876
    Thanks
    1
    Thanked 180 Times in 172 Posts
    Blog Entries
    2

    Default

    No, it doesn't work like that. Have a boolean variable; when the mouse moves, clear the delay with clearTimeout(), set it to true and use setTimeout() to set a delay, after which it will be set back to false.
    Twey | I understand English | 日本語が分かります | mi jimpe fi le jbobau | mi esperanton komprenas | je comprends français | entiendo español | tôi ít hiểu tiếng Việt | ich verstehe ein bisschen Deutsch | beware XHTML | common coding mistakes | tutorials | various stuff | argh PHP!

  9. #9
    Join Date
    Aug 2005
    Posts
    971
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default

    I apologise but I wasn't quite able to follow you. Could you describe it a bit briefly please??

    I still have a lots of questions to be answered please try to help me as I am a newbie in this dynamic world of scripting!!

  10. #10
    Join Date
    Jun 2005
    Location
    英国
    Posts
    11,876
    Thanks
    1
    Thanked 180 Times in 172 Posts
    Blog Entries
    2

    Default

    Code:
    <script type="text/javascript">
    var mousetimeout = null;
    function runScreenSaver() {
      // Your "screen saver" code
    }
    document.onmousemove = function() {
      if(mousetimeout !== null) window.clearTimeout(mousetimeout);
      mousetimeout = window.setTimeout(runScreenSaver, 60000);
    };
    </script>
    Twey | I understand English | 日本語が分かります | mi jimpe fi le jbobau | mi esperanton komprenas | je comprends français | entiendo español | tôi ít hiểu tiếng Việt | ich verstehe ein bisschen Deutsch | beware XHTML | common coding mistakes | tutorials | various stuff | argh PHP!

Bookmarks

Posting Permissions

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