keyhandler.html 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  1. <!DOCTYPE html>
  2. <html>
  3. <!--
  4. Copyright 2010 The Closure Library Authors. All Rights Reserved.
  5. Use of this source code is governed by the Apache License, Version 2.0.
  6. See the COPYING file for details.
  7. -->
  8. <head>
  9. <title>goog.events.KeyHandler</title>
  10. <link rel="stylesheet" href="css/demo.css">
  11. <style>
  12. .input-group {
  13. border: 1px solid black;
  14. clear: both;
  15. float: left;
  16. margin-bottom: 1em;
  17. padding: 1em;
  18. }
  19. #console {
  20. position: fixed;
  21. right: 0;
  22. top: 0;
  23. left: 400px;
  24. bottom: 0;
  25. overflow: auto;
  26. }
  27. #no-tab {
  28. background: pink;
  29. }
  30. </style>
  31. <script src="../base.js"></script>
  32. <script>
  33. goog.require('goog.debug.DivConsole');
  34. goog.require('goog.events');
  35. goog.require('goog.events.KeyCodes');
  36. goog.require('goog.events.KeyHandler');
  37. goog.require('goog.log');
  38. </script>
  39. </head>
  40. <body>
  41. <h1>goog.events.KeyHandler</h1>
  42. <p><button onclick="divConsole.clear()">Clear Console</button></p>
  43. <div class="input-group">
  44. <label>Plain input <input></label><br>
  45. <label>Number input <input class="number-input"></label><br>
  46. <label>Text area <textarea></textarea></label><br>
  47. <div tabIndex="0">Focusable div</div>
  48. </div>
  49. <div id="no-tab" class="input-group">
  50. No Tab inside this<br><br>
  51. <label>Plain input <input></label><br>
  52. <label>Number input <input class="number-input"></label><br>
  53. <label>Text area <textarea></textarea></label><br>
  54. <div tabIndex="0">Focusable div</div>
  55. </div>
  56. <div id="console"></div>
  57. <script>
  58. var divConsole = new goog.debug.DivConsole(document.getElementById('console'));
  59. divConsole.setCapturing(true);
  60. var logger = new goog.log.getLogger('demo');
  61. var docKh = new goog.events.KeyHandler(document);
  62. goog.events.listen(docKh, 'key', function(e) {
  63. goog.log.info(logger, 'keyCode: ' + e.keyCode +
  64. ', charCode: ' + e.charCode +
  65. ', repeat: ' + e.repeat +
  66. ', target: ' + e.target +
  67. ', native event: ' + e.getBrowserEvent().type);
  68. });
  69. var noTabKh = new goog.events.KeyHandler(document.getElementById('no-tab'));
  70. goog.events.listen(noTabKh, goog.events.KeyHandler.EventType.KEY, function(e) {
  71. if (e.keyCode == goog.events.KeyCodes.TAB) {
  72. e.preventDefault();
  73. }
  74. });
  75. function numberInputKeyHandler(e) {
  76. goog.log.info(logger, 'isTextModifyingKeyEvent: ' +
  77. goog.events.KeyCodes.isTextModifyingKeyEvent(e));
  78. if (e.target.className != 'number-input' ||
  79. e.ctrlKey ||
  80. !e.shiftKey && e.keyCode >= goog.events.KeyCodes.ZERO &&
  81. e.keyCode <= goog.events.KeyCodes.NINE ||
  82. !goog.events.KeyCodes.isTextModifyingKeyEvent(e)) {
  83. return;
  84. }
  85. switch (e.keyCode) {
  86. // Allow these
  87. case goog.events.KeyCodes.DELETE:
  88. case goog.events.KeyCodes.BACKSPACE:
  89. case goog.events.KeyCodes.TAB:
  90. return;
  91. }
  92. // prevent default for the rest
  93. e.preventDefault();
  94. }
  95. goog.events.listen(docKh, goog.events.KeyHandler.EventType.KEY,
  96. numberInputKeyHandler);
  97. </script>
  98. </body>
  99. </html>