arrow-key-events.html 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  1. <body>
  2. <p>Test for <a href="http://bugs.webkit.org/show_bug.cgi?id=3387">bug 3387</a>:
  3. Redundant keydown, keypress, keyup events sent for arrow keys.</p>
  4. <p>Try pressing arrow keys, PgUp/PgDown/Home/End, Esc, or function keys.
  5. The test passes if the box below doesn't turn red.<p>
  6. <div id="result" style="width:100px; height:100px; background-color:blue;"></div>
  7. <script>
  8. var console_messages = document.createElement("ol");
  9. document.body.appendChild(console_messages);
  10. window.onkeydown = registerWindow;
  11. window.onkeypress = registerWindow;
  12. window.onkeyup = registerWindow;
  13. document.onkeydown = registerDocument;
  14. document.onkeypress = registerDocument;
  15. document.onkeyup = registerDocument;
  16. document.body.onkeydown = registerBody;
  17. document.body.onkeypress = registerBody;
  18. document.body.onkeyup = registerBody;
  19. document.documentElement.onkeydown = registerDocumentElement;
  20. document.documentElement.onkeypress = registerDocumentElement;
  21. document.documentElement.onkeyup = registerDocumentElement;
  22. var bodyKeyDownCount = 0;
  23. var documentElementKeyDownCount = 0;
  24. var windowKeyDownCount = 0;
  25. var documentKeyDownCount = 0;
  26. function log(message)
  27. {
  28. var item = document.createElement("li");
  29. item.appendChild(document.createTextNode(message));
  30. item.style.fontSize = '8px';
  31. console_messages.appendChild(item);
  32. }
  33. function registerBody(e)
  34. {
  35. if ((e.type == "keydown" && ++bodyKeyDownCount != 1)
  36. || (e.type == "keyup" && --bodyKeyDownCount != 0))
  37. document.getElementById("result").style.backgroundColor = "red";
  38. if (!e)
  39. e = window.event;
  40. log("body: " + e.type);
  41. return true;
  42. }
  43. function registerDocumentElement(e)
  44. {
  45. if ((e.type == "keydown" && ++documentElementKeyDownCount != 1)
  46. || (e.type == "keyup" && --documentElementKeyDownCount != 0))
  47. document.getElementById("result").style.backgroundColor = "red";
  48. if (!e)
  49. e = window.event;
  50. log(" documentElement: " + e.type);
  51. return true;
  52. }
  53. function registerDocument(e)
  54. {
  55. if ((e.type == "keydown" && ++documentKeyDownCount != 1)
  56. || (e.type == "keyup" && --documentKeyDownCount != 0))
  57. document.getElementById("result").style.backgroundColor = "red";
  58. if (!e)
  59. e = window.event;
  60. log("  document: " + e.type);
  61. return true;
  62. }
  63. function registerWindow(e)
  64. {
  65. if ((e.type == "keydown" && ++windowKeyDownCount != 1)
  66. || (e.type == "keyup" && --windowKeyDownCount != 0))
  67. document.getElementById("result").style.backgroundColor = "red";
  68. if (!e)
  69. e = window.event;
  70. log("   window: " + e.type);
  71. return true;
  72. }
  73. </script>
  74. </body>