PageRenderTime 47ms CodeModel.GetById 21ms RepoModel.GetById 1ms app.codeStats 0ms

/jEdit/tags/jedit-4-0-pre3/org/gjt/sp/jedit/gui/KeyEventWorkaround.java

#
Java | 151 lines | 90 code | 18 blank | 43 comment | 55 complexity | c82ea706882f74cb25d823f0a554e966 MD5 | raw file
Possible License(s): BSD-3-Clause, AGPL-1.0, Apache-2.0, LGPL-2.0, LGPL-3.0, GPL-2.0, CC-BY-SA-3.0, LGPL-2.1, GPL-3.0, MPL-2.0-no-copyleft-exception, IPL-1.0
  1. /*
  2. * KeyEventWorkaround.java - Works around bugs in Java event handling
  3. * :tabSize=8:indentSize=8:noTabs=false:
  4. * :folding=explicit:collapseFolds=1:
  5. *
  6. * Copyright (C) 2000 Slava Pestov
  7. *
  8. * This program is free software; you can redistribute it and/or
  9. * modify it under the terms of the GNU General Public License
  10. * as published by the Free Software Foundation; either version 2
  11. * of the License, or any later version.
  12. *
  13. * This program is distributed in the hope that it will be useful,
  14. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  16. * GNU General Public License for more details.
  17. *
  18. * You should have received a copy of the GNU General Public License
  19. * along with this program; if not, write to the Free Software
  20. * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
  21. */
  22. package org.gjt.sp.jedit.gui;
  23. //{{{ Imports
  24. import java.awt.event.*;
  25. import java.awt.*;
  26. //}}}
  27. public class KeyEventWorkaround
  28. {
  29. // from JDK 1.2 InputEvent.java
  30. public static final int ALT_GRAPH_MASK = 1 << 5;
  31. //{{{ processKeyEvent() method
  32. public static KeyEvent processKeyEvent(KeyEvent evt)
  33. {
  34. int keyCode = evt.getKeyCode();
  35. char ch = evt.getKeyChar();
  36. switch(evt.getID())
  37. {
  38. //{{{ KEY_PRESSED...
  39. case KeyEvent.KEY_PRESSED:
  40. // get rid of keys we never need to handle
  41. if(keyCode == KeyEvent.VK_CONTROL ||
  42. keyCode == KeyEvent.VK_SHIFT ||
  43. keyCode == KeyEvent.VK_ALT ||
  44. keyCode == KeyEvent.VK_META ||
  45. keyCode == '\0')
  46. return null;
  47. if(!mac)
  48. handleBrokenKeys(evt,keyCode);
  49. return evt;
  50. //}}}
  51. //{{{ KEY_TYPED...
  52. case KeyEvent.KEY_TYPED:
  53. // need to let \b through so that backspace will work
  54. // in HistoryTextFields
  55. if((ch < 0x20 || ch == 0x7f || ch == 0xff) && ch != '\b')
  56. return null;
  57. // "Alt" is the option key on MacOS, and it can generate
  58. // user input
  59. if(mac)
  60. {
  61. if(evt.isControlDown() || evt.isMetaDown())
  62. return null;
  63. }
  64. else
  65. {
  66. if((evt.isControlDown() ^ evt.isAltDown())
  67. || evt.isMetaDown())
  68. return null;
  69. // if the last key was a broken key, filter
  70. // out all except 'a'-'z' that occur 750 ms after.
  71. if(last == LAST_BROKEN && System.currentTimeMillis()
  72. - lastKeyTime < 750 && !Character.isLetter(ch))
  73. {
  74. last = LAST_NOTHING;
  75. return null;
  76. }
  77. // otherwise, if it was ALT, filter out everything.
  78. else if(last == LAST_ALT && System.currentTimeMillis()
  79. - lastKeyTime < 750)
  80. {
  81. last = LAST_NOTHING;
  82. return null;
  83. }
  84. }
  85. return evt;
  86. //}}}
  87. default:
  88. return evt;
  89. }
  90. } //}}}
  91. //{{{ Private members
  92. //{{{ Static variables
  93. private static boolean mac;
  94. private static long lastKeyTime;
  95. private static int last;
  96. private static final int LAST_NOTHING = 0;
  97. private static final int LAST_ALTGR = 1;
  98. private static final int LAST_ALT = 2;
  99. private static final int LAST_BROKEN = 3;
  100. //}}}
  101. //{{{ Class initializer
  102. static
  103. {
  104. mac = (System.getProperty("os.name").indexOf("Mac OS") != -1);
  105. } //}}}
  106. //{{{ handleBrokenKeys() method
  107. private static void handleBrokenKeys(KeyEvent evt, int keyCode)
  108. {
  109. if(evt.isAltDown() && evt.isControlDown()
  110. && !evt.isMetaDown())
  111. {
  112. last = LAST_ALTGR;
  113. return;
  114. }
  115. else if(!(evt.isAltDown() || evt.isControlDown() || evt.isMetaDown()))
  116. {
  117. last = LAST_NOTHING;
  118. return;
  119. }
  120. if(evt.isAltDown())
  121. last = LAST_ALT;
  122. else if((keyCode < KeyEvent.VK_A || keyCode > KeyEvent.VK_Z)
  123. && keyCode != KeyEvent.VK_LEFT && keyCode != KeyEvent.VK_RIGHT
  124. && keyCode != KeyEvent.VK_UP && keyCode != KeyEvent.VK_DOWN
  125. && keyCode != KeyEvent.VK_DELETE && keyCode != KeyEvent.VK_BACK_SPACE
  126. && keyCode != KeyEvent.VK_TAB && keyCode != KeyEvent.VK_ENTER)
  127. last = LAST_BROKEN;
  128. else
  129. last = LAST_NOTHING;
  130. lastKeyTime = System.currentTimeMillis();
  131. } //}}}
  132. //}}}
  133. }