Key stroke capture


/ Published in: JavaScript
Save to your folder(s)



Copy this code and paste it in your HTML
  1. function trap(e) {
  2. if (!e) {
  3. e = event;
  4. e.which = e.keyCode;
  5. }
  6. try {
  7. modifierKey = e.ctrlKey || e.altKey || e.metaKey;
  8. }
  9. catch(e) {
  10. modifierKey = false;
  11. }
  12. return modifierKey || e.which == 0;
  13. }
  14. function keys(key) {
  15. if (!key) {
  16. key = event;
  17. key.which = key.keyCode;
  18. }
  19. if (key.which == 84) {
  20. //toggle();
  21. return;
  22. }
  23. switch (key.which) {
  24. case 32: // spacebar
  25. case 34: // page down
  26. case 39: // rightkey
  27. case 40: // downkey
  28. // Do something
  29. break;
  30. case 33: // page up
  31. case 37: // leftkey
  32. case 38: // upkey
  33. // Do something
  34. break;
  35. case 36: // home
  36. // Do something
  37. break;
  38. case 35: // end
  39. // Do something
  40. break;
  41. case 27: // escape
  42. case 81: // q
  43. // Do something
  44. break;
  45. }
  46.  
  47. return false;
  48. }
  49.  
  50. function keyCap() {
  51. document.onkeypress = trap;
  52. document.onkeyup = keys;
  53. }

URL: http://rmhh.co.uk/ascii.html

Report this snippet


Comments

RSS Icon Subscribe to comments

You need to login to post a comment.