how to event more keys within javascript -
i want eventing more keys in javascript code:
<script> function open(e) { if (e.type !== "blur") { if (e.keycode === 70) { alert("pressed f"); } } } document.onkeydown = open; </script>
what getting question want detect more keys presses. best way detect key presses switch statement
function open(e) { if (e.type !== "blur") { switch (e.keycode) { case 70: alert("pressed f"); break; case 65: alert("pressed a"); break; default: alert("i don't know key!");//this line removable break; } } } document.onkeydown = open;
how works
the way switch
works is:
switch (value) { case this_value: code break; default: code break; }
that worst explanation you've seen can read here
without keycode
keycodes kind of irritating figure out, can use:
function open(e) { if (e.type !== "blur") { switch (string.fromcharcode(e.keycode)) { case "f": alert("pressed f"); break; case "a": alert("pressed a"); break; case "b": alert("pressed b"); default: alert("i don't know key!");//this line removable break; } } } document.onkeydown = open;
detect key combinations
when detecting key combinations, can use &&
make sure both key's pressed. without more complicated. can use:
e.metakey
window key on windows, command key on mac
e.ctrlkey
control key
e.shiftkey
shift key
e.altkey
alt key
use them as:
if (e.ctrlkey && e.keycode === 65) { alert("control , key pressed"); }
to detect keys pressed (multiple) found this fiddle (not mine), , question here
Comments
Post a Comment