PDA

View Full Version : Multiple keyboard event.



shachi
05-24-2006, 01:09 PM
I am developing a key event library for myself which is the code below:



// 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.:)

Twey
05-24-2006, 01:19 PM
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:
if(e.keycode == key_a && e.ctrlKey)Early versions (I think) of Netscape use another property instead, the bitset e.modifiers.

shachi
05-24-2006, 02:02 PM
Oh Thanks!! Let me try that. I tried this:



if(e.keycode == key_a + e.ctrlKey)


instead of



if(e.keycode == key_a && e.ctrlKey)


But if this doesn't work then I am sorry but I need to bother you again. ;)

shachi
05-24-2006, 02:09 PM
Thanks it miraculously worked!! You're a genius!!



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"??:confused: 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.:D

Twey
05-24-2006, 02:19 PM
http://www.javascripter.net/faq/ctrl_alt.htm
The information on boolean properties is outdated; they are no longer IE-only.

shachi
05-24-2006, 02:50 PM
Ok thank you very much!!!

shachi
05-24-2006, 02:56 PM
do you know if this works??



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.

Twey
05-24-2006, 03:33 PM
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.

shachi
05-24-2006, 06:07 PM
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!!

Twey
05-24-2006, 06:19 PM
<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>

shachi
05-25-2006, 06:43 AM
Thank you very much for solving my problem.:)