Docjar: A Java Source and Docuemnt Enginecom.*    java.*    javax.*    org.*    all    new    plug-in

Quick Search    Search Deep

Source code: jpicedt/widgets/KeyEventWorkaround.java


1   /*
2    * KeyEventWorkaround.java - Works around bugs in Java event handling
3    * Copyright (C) 2000 Slava Pestov
4    *
5    * This program is free software; you can redistribute it and/or
6    * modify it under the terms of the GNU General Public License
7    * as published by the Free Software Foundation; either version 2
8    * of the License, or any later version.
9    *
10   * This program is distributed in the hope that it will be useful,
11   * but WITHOUT ANY WARRANTY; without even the implied warranty of
12   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13   * GNU General Public License for more details.
14   *
15   * You should have received a copy of the GNU General Public License
16   * along with this program; if not, write to the Free Software
17   * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
18   */
19  
20  package jpicedt.widgets;
21  
22  import java.awt.event.*;
23  import java.awt.*;
24  
25  public class KeyEventWorkaround
26  {
27    // from JDK 1.2 InputEvent.java
28    public static final int ALT_GRAPH_MASK = 1 << 5;
29  
30    public static KeyEvent processKeyEvent(KeyEvent evt)
31    {
32      int keyCode = evt.getKeyCode();
33      int modifiers = evt.getModifiers();
34      char ch = evt.getKeyChar();
35  
36      switch(evt.getID())
37      {
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          return null;
45  
46        // get rid of undefined keys
47        if(keyCode == '\0')
48          return null;
49  
50        handleBrokenKeys(modifiers,keyCode);
51  
52        return evt;
53      case KeyEvent.KEY_TYPED:
54        if((modifiers & (~ (ALT_GRAPH_MASK | KeyEvent.SHIFT_MASK))) != 0)
55          return null;
56  
57        // need to let \b through so that backspace will work
58        // in HistoryTextFields
59        if((ch < 0x20 || ch == 0x7f) && ch != '\b')
60          return null;
61  
62        // if the last key was a broken key, filter
63        // out all except 'a'-'z' that occur 750 ms after.
64        if(last == LAST_BROKEN && System.currentTimeMillis()
65          - lastKeyTime < 750 && !Character.isLetter(ch))
66        {
67          last = LAST_NOTHING;
68          return null;
69        }
70        // otherwise, if it was ALT, filter out everything.
71        else if(last == LAST_ALT && System.currentTimeMillis()
72          - lastKeyTime < 750)
73        {
74          last = LAST_NOTHING;
75          return null;
76        }
77  
78        return evt;
79      default:
80        return evt;
81      }
82    }
83  
84    // private members
85    private static long lastKeyTime;
86  
87    private static int last;
88    private static final int LAST_NOTHING = 0;
89    private static final int LAST_ALTGR = 1;
90    private static final int LAST_ALT = 2;
91    private static final int LAST_BROKEN = 3;
92  
93    private static void handleBrokenKeys(int modifiers, int keyCode)
94    {
95      // If you have any keys you would like to add to this list,
96      // e-mail me
97  
98      if(modifiers == (KeyEvent.ALT_MASK | KeyEvent.CTRL_MASK)
99        || modifiers == (KeyEvent.ALT_MASK | KeyEvent.CTRL_MASK
100       | KeyEvent.SHIFT_MASK))
101     {
102       last = LAST_ALTGR;
103       return;
104     }
105     else if((modifiers & (~ (ALT_GRAPH_MASK | KeyEvent.SHIFT_MASK))) == 0)
106     {
107       last = LAST_NOTHING;
108       return;
109     }
110 
111     if((modifiers & KeyEvent.ALT_MASK) != 0)
112       last = LAST_ALT;
113     else if((keyCode < KeyEvent.VK_A || keyCode > KeyEvent.VK_Z)
114       && keyCode != KeyEvent.VK_LEFT && keyCode != KeyEvent.VK_RIGHT
115       && keyCode != KeyEvent.VK_UP && keyCode != KeyEvent.VK_DOWN
116       && keyCode != KeyEvent.VK_DELETE && keyCode != KeyEvent.VK_BACK_SPACE
117        && keyCode != KeyEvent.VK_TAB && keyCode != KeyEvent.VK_ENTER)
118       last = LAST_BROKEN;
119     else
120       last = LAST_NOTHING;
121 
122     lastKeyTime = System.currentTimeMillis();
123   }
124 }
125 
126 /*
127  * ChangeLog:
128  * $Log: KeyEventWorkaround.java,v $
129  * Revision 1.1.1.1  2002/04/18 16:23:02  reynal
130  * First import of 1_3_2_beta9
131  *
132  * Revision 1.1.1.1  2002/04/01 18:04:27  reynal
133  *
134  * First time import to CVS of release 1_3_2_beta6.
135  *
136  *
137  * Revision 1.9  2000/11/26 01:43:35  sp
138  * x86 assembly mode, various other stuff
139  *
140  * Revision 1.8  2000/11/13 11:19:27  sp
141  * Search bar reintroduced, more BeanShell stuff
142  *
143  * Revision 1.7  2000/11/12 05:36:50  sp
144  * BeanShell integration started
145  *
146  * Revision 1.6  2000/10/28 00:36:58  sp
147  * ML mode, Haskell mode
148  *
149  * Revision 1.5  2000/10/05 04:30:10  sp
150  * *** empty log message ***
151  *
152  * Revision 1.4  2000/09/26 10:19:47  sp
153  * Bug fixes, spit and polish
154  *
155  * Revision 1.3  2000/09/23 03:01:10  sp
156  * pre7 yayayay
157  *
158  * Revision 1.2  2000/09/09 04:00:34  sp
159  * 2.6pre6
160  *
161  * Revision 1.1  2000/09/07 04:46:08  sp
162  * bug fixes
163  *
164  */