PageRenderTime 35ms CodeModel.GetById 13ms RepoModel.GetById 0ms app.codeStats 0ms

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

#
Java | 142 lines | 85 code | 16 blank | 41 comment | 54 complexity | c3032c11edbfba8e5d58957d36c3accb 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. import org.gjt.sp.jedit.OperatingSystem;
  27. //}}}
  28. public class KeyEventWorkaround
  29. {
  30. //{{{ processKeyEvent() method
  31. public static KeyEvent processKeyEvent(KeyEvent evt)
  32. {
  33. int keyCode = evt.getKeyCode();
  34. char ch = evt.getKeyChar();
  35. switch(evt.getID())
  36. {
  37. //{{{ KEY_PRESSED...
  38. case KeyEvent.KEY_PRESSED:
  39. // get rid of keys we never need to handle
  40. if(keyCode == KeyEvent.VK_CONTROL ||
  41. keyCode == KeyEvent.VK_SHIFT ||
  42. keyCode == KeyEvent.VK_ALT ||
  43. keyCode == KeyEvent.VK_META ||
  44. keyCode == '\0')
  45. return null;
  46. if(!OperatingSystem.isMacOS())
  47. handleBrokenKeys(evt,keyCode);
  48. return evt;
  49. //}}}
  50. //{{{ KEY_TYPED...
  51. case KeyEvent.KEY_TYPED:
  52. // need to let \b through so that backspace will work
  53. // in HistoryTextFields
  54. if((ch < 0x20 || ch == 0x7f || ch == 0xff) && ch != '\b')
  55. return null;
  56. // "Alt" is the option key on MacOS, and it can generate
  57. // user input
  58. if(OperatingSystem.isMacOS())
  59. {
  60. if(evt.isControlDown() || evt.isMetaDown())
  61. return null;
  62. }
  63. else
  64. {
  65. if((evt.isControlDown() ^ evt.isAltDown())
  66. || evt.isMetaDown())
  67. return null;
  68. // if the last key was a broken key, filter
  69. // out all except 'a'-'z' that occur 750 ms after.
  70. if(last == LAST_BROKEN && System.currentTimeMillis()
  71. - lastKeyTime < 750 && !Character.isLetter(ch))
  72. {
  73. last = LAST_NOTHING;
  74. return null;
  75. }
  76. // otherwise, if it was ALT, filter out everything.
  77. else if(last == LAST_ALT && System.currentTimeMillis()
  78. - lastKeyTime < 750)
  79. {
  80. last = LAST_NOTHING;
  81. return null;
  82. }
  83. }
  84. return evt;
  85. //}}}
  86. default:
  87. return evt;
  88. }
  89. } //}}}
  90. //{{{ Private members
  91. //{{{ Static variables
  92. private static long lastKeyTime;
  93. private static int last;
  94. private static final int LAST_NOTHING = 0;
  95. private static final int LAST_ALTGR = 1;
  96. private static final int LAST_ALT = 2;
  97. private static final int LAST_BROKEN = 3;
  98. //}}}
  99. //{{{ handleBrokenKeys() method
  100. private static void handleBrokenKeys(KeyEvent evt, int keyCode)
  101. {
  102. if(evt.isAltDown() && evt.isControlDown()
  103. && !evt.isMetaDown())
  104. {
  105. last = LAST_ALTGR;
  106. return;
  107. }
  108. else if(!(evt.isAltDown() || evt.isControlDown() || evt.isMetaDown()))
  109. {
  110. last = LAST_NOTHING;
  111. return;
  112. }
  113. if(evt.isAltDown())
  114. last = LAST_ALT;
  115. else if((keyCode < KeyEvent.VK_A || keyCode > KeyEvent.VK_Z)
  116. && keyCode != KeyEvent.VK_LEFT && keyCode != KeyEvent.VK_RIGHT
  117. && keyCode != KeyEvent.VK_UP && keyCode != KeyEvent.VK_DOWN
  118. && keyCode != KeyEvent.VK_DELETE && keyCode != KeyEvent.VK_BACK_SPACE
  119. && keyCode != KeyEvent.VK_TAB && keyCode != KeyEvent.VK_ENTER)
  120. last = LAST_BROKEN;
  121. else
  122. last = LAST_NOTHING;
  123. lastKeyTime = System.currentTimeMillis();
  124. } //}}}
  125. //}}}
  126. }