PageRenderTime 61ms CodeModel.GetById 31ms app.highlight 25ms RepoModel.GetById 1ms app.codeStats 0ms

/jEdit/tags/jedit-4-5-pre1/org/gjt/sp/jedit/gui/KeyEventWorkaround.java

#
Java | 395 lines | 285 code | 16 blank | 94 comment | 48 complexity | 95b89beb431df037f17e6f6762b4cbe6 MD5 | raw file
  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, 2005 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
 23package org.gjt.sp.jedit.gui;
 24
 25//{{{ Imports
 26import java.awt.event.*;
 27import org.gjt.sp.jedit.Debug;
 28import org.gjt.sp.jedit.OperatingSystem;
 29import org.gjt.sp.jedit.input.AbstractInputHandler;
 30import org.gjt.sp.util.Log;
 31//}}}
 32
 33/**
 34 * Various hacks to get keyboard event handling to behave in a consistent manner
 35 * across Java implementations. This type of stuff should not be necessary, but
 36 * Java's keyboard handling is crap, to put it mildly.
 37 *
 38 * @author Slava Pestov
 39 * @version $Id: KeyEventWorkaround.java 17503 2010-03-20 09:47:18Z k_satoda $
 40 */
 41public class KeyEventWorkaround
 42{
 43	//{{{ isBindable() method
 44	public static boolean isBindable(int keyCode)
 45	{
 46		switch(keyCode)
 47		{
 48		case KeyEvent.VK_ALT:
 49		case KeyEvent.VK_ALT_GRAPH:
 50		case KeyEvent.VK_CONTROL:
 51		case KeyEvent.VK_SHIFT:
 52		case KeyEvent.VK_META:
 53		case KeyEvent.VK_DEAD_GRAVE:
 54		case KeyEvent.VK_DEAD_ACUTE:
 55		case KeyEvent.VK_DEAD_CIRCUMFLEX:
 56		case KeyEvent.VK_DEAD_TILDE:
 57		case KeyEvent.VK_DEAD_MACRON:
 58		case KeyEvent.VK_DEAD_BREVE:
 59		case KeyEvent.VK_DEAD_ABOVEDOT:
 60		case KeyEvent.VK_DEAD_DIAERESIS:
 61		case KeyEvent.VK_DEAD_ABOVERING:
 62		case KeyEvent.VK_DEAD_DOUBLEACUTE:
 63		case KeyEvent.VK_DEAD_CARON:
 64		case KeyEvent.VK_DEAD_CEDILLA:
 65		case KeyEvent.VK_DEAD_OGONEK:
 66		case KeyEvent.VK_DEAD_IOTA:
 67		case KeyEvent.VK_DEAD_VOICED_SOUND:
 68		case KeyEvent.VK_DEAD_SEMIVOICED_SOUND:
 69			return false;
 70		default:
 71			return true;
 72		}
 73	} //}}}
 74
 75	//{{{ isPrintable() method
 76	/**
 77	 * We need to know if a keycode can potentially result in a
 78	 * keytyped.
 79	 * @since jEdit 4.3pre2
 80	 */
 81	public static boolean isPrintable(int keyCode)
 82	{
 83		switch(keyCode)
 84		{
 85		/* case KeyEvent.VK_ENTER:
 86		case KeyEvent.VK_TAB: */
 87		case KeyEvent.VK_SPACE:
 88		case KeyEvent.VK_COMMA:
 89		case KeyEvent.VK_MINUS:
 90		case KeyEvent.VK_PERIOD:
 91		case KeyEvent.VK_SLASH:
 92		case KeyEvent.VK_0:
 93		case KeyEvent.VK_1:
 94		case KeyEvent.VK_2:
 95		case KeyEvent.VK_3:
 96		case KeyEvent.VK_4:
 97		case KeyEvent.VK_5:
 98		case KeyEvent.VK_6:
 99		case KeyEvent.VK_7:
100		case KeyEvent.VK_8:
101		case KeyEvent.VK_9:
102		case KeyEvent.VK_SEMICOLON:
103		case KeyEvent.VK_EQUALS   :
104		case KeyEvent.VK_A:
105		case KeyEvent.VK_B:
106		case KeyEvent.VK_C:
107		case KeyEvent.VK_D:
108		case KeyEvent.VK_E:
109		case KeyEvent.VK_F:
110		case KeyEvent.VK_G:
111		case KeyEvent.VK_H:
112		case KeyEvent.VK_I:
113		case KeyEvent.VK_J:
114		case KeyEvent.VK_K:
115		case KeyEvent.VK_L:
116		case KeyEvent.VK_M:
117		case KeyEvent.VK_N:
118		case KeyEvent.VK_O:
119		case KeyEvent.VK_P:
120		case KeyEvent.VK_Q:
121		case KeyEvent.VK_R:
122		case KeyEvent.VK_S:
123		case KeyEvent.VK_T:
124		case KeyEvent.VK_U:
125		case KeyEvent.VK_V:
126		case KeyEvent.VK_W:
127		case KeyEvent.VK_X:
128		case KeyEvent.VK_Y:
129		case KeyEvent.VK_Z:
130		case KeyEvent.VK_OPEN_BRACKET :
131		case KeyEvent.VK_BACK_SLASH   :
132		case KeyEvent.VK_CLOSE_BRACKET:
133	/*	case KeyEvent.VK_NUMPAD0 :
134		case KeyEvent.VK_NUMPAD1 :
135		case KeyEvent.VK_NUMPAD2 :
136		case KeyEvent.VK_NUMPAD3 :
137		case KeyEvent.VK_NUMPAD4 :
138		case KeyEvent.VK_NUMPAD5 :
139		case KeyEvent.VK_NUMPAD6 :
140		case KeyEvent.VK_NUMPAD7 :
141		case KeyEvent.VK_NUMPAD8 :
142		case KeyEvent.VK_NUMPAD9 :
143		case KeyEvent.VK_MULTIPLY:
144		case KeyEvent.VK_ADD     :
145		case KeyEvent.VK_SEPARATOR:
146		case KeyEvent.VK_SUBTRACT   :
147		case KeyEvent.VK_DECIMAL    :
148		case KeyEvent.VK_DIVIDE     :*/
149		case KeyEvent.VK_BACK_QUOTE:
150		case KeyEvent.VK_QUOTE:
151		case KeyEvent.VK_DEAD_GRAVE:
152		case KeyEvent.VK_DEAD_ACUTE:
153		case KeyEvent.VK_DEAD_CIRCUMFLEX:
154		case KeyEvent.VK_DEAD_TILDE:
155		case KeyEvent.VK_DEAD_MACRON:
156		case KeyEvent.VK_DEAD_BREVE:
157		case KeyEvent.VK_DEAD_ABOVEDOT:
158		case KeyEvent.VK_DEAD_DIAERESIS:
159		case KeyEvent.VK_DEAD_ABOVERING:
160		case KeyEvent.VK_DEAD_DOUBLEACUTE:
161		case KeyEvent.VK_DEAD_CARON:
162		case KeyEvent.VK_DEAD_CEDILLA:
163		case KeyEvent.VK_DEAD_OGONEK:
164		case KeyEvent.VK_DEAD_IOTA:
165		case KeyEvent.VK_DEAD_VOICED_SOUND:
166		case KeyEvent.VK_DEAD_SEMIVOICED_SOUND:
167		case KeyEvent.VK_AMPERSAND:
168		case KeyEvent.VK_ASTERISK:
169		case KeyEvent.VK_QUOTEDBL:
170		case KeyEvent.VK_LESS:
171		case KeyEvent.VK_GREATER:
172		case KeyEvent.VK_BRACELEFT:
173		case KeyEvent.VK_BRACERIGHT:
174		case KeyEvent.VK_AT:
175		case KeyEvent.VK_COLON:
176		case KeyEvent.VK_CIRCUMFLEX:
177		case KeyEvent.VK_DOLLAR:
178		case KeyEvent.VK_EURO_SIGN:
179		case KeyEvent.VK_EXCLAMATION_MARK:
180		case KeyEvent.VK_INVERTED_EXCLAMATION_MARK:
181		case KeyEvent.VK_LEFT_PARENTHESIS:
182		case KeyEvent.VK_NUMBER_SIGN:
183		case KeyEvent.VK_PLUS:
184		case KeyEvent.VK_RIGHT_PARENTHESIS:
185		case KeyEvent.VK_UNDERSCORE:
186			return true;
187		default:
188			return false;
189		}
190	} //}}}
191
192	//{{{ isMacControl() method
193	/**
194	 * Apple sucks.
195	 */
196	public static boolean isMacControl(KeyEvent evt)
197	{
198		return (OperatingSystem.isMacOS() &&
199			(evt.getModifiers() & InputEvent.CTRL_MASK) != 0
200			&& evt.getKeyChar() <= 0x2B);
201	} //}}}
202
203	//{{{ isNumericKeypad() method
204	public static boolean isNumericKeypad(int keyCode)
205	{
206		switch(keyCode)
207		{
208		case KeyEvent.VK_NUMPAD0:
209		case KeyEvent.VK_NUMPAD1:
210		case KeyEvent.VK_NUMPAD2:
211		case KeyEvent.VK_NUMPAD3:
212		case KeyEvent.VK_NUMPAD4:
213		case KeyEvent.VK_NUMPAD5:
214		case KeyEvent.VK_NUMPAD6:
215		case KeyEvent.VK_NUMPAD7:
216		case KeyEvent.VK_NUMPAD8:
217		case KeyEvent.VK_NUMPAD9:
218		case KeyEvent.VK_MULTIPLY:
219		case KeyEvent.VK_ADD:
220		/* case KeyEvent.VK_SEPARATOR: */
221		case KeyEvent.VK_SUBTRACT:
222		case KeyEvent.VK_DECIMAL:
223		case KeyEvent.VK_DIVIDE:
224			return true;
225		default:
226			return false;
227		}
228	} //}}}
229
230	//{{{ processKeyEvent() method
231	public static KeyEvent processKeyEvent(KeyEvent evt)
232	{
233		int keyCode = evt.getKeyCode();
234		char ch = evt.getKeyChar();
235		int modifiers = evt.getModifiers();
236
237		switch(evt.getID())
238		{
239		//{{{ KEY_PRESSED...
240		case KeyEvent.KEY_PRESSED:
241			// get rid of keys we never need to handle
242			switch(keyCode)
243			{
244			case '\0':
245				return null;
246			case KeyEvent.VK_ALT:
247			case KeyEvent.VK_ALT_GRAPH:
248			case KeyEvent.VK_CONTROL:
249			case KeyEvent.VK_SHIFT:
250			case KeyEvent.VK_META:
251				break;
252			default:
253				if(!evt.isMetaDown())
254				{
255					if(!evt.isControlDown()
256						&& !evt.isAltDown())
257					{
258						if(isPrintable(keyCode))
259						{
260							return null;
261						}
262					}
263				}
264
265				if(Debug.ALT_KEY_PRESSED_DISABLED)
266				{
267					/* we don't handle key pressed A+ */
268					/* they're too troublesome */
269					if((modifiers & InputEvent.ALT_MASK) != 0)
270						return null;
271				}
272
273				if(isNumericKeypad(keyCode))
274					last = LAST_NUMKEYPAD;
275				else
276					last = LAST_NOTHING;
277
278				break;
279			}
280			break;
281		//}}}
282		//{{{ KEY_TYPED...
283		case KeyEvent.KEY_TYPED:
284			// need to let \b through so that backspace will work
285			// in HistoryTextFields
286			if(!isMacControl(evt)
287				&& (ch < 0x20 || ch == 0x7f || ch == 0xff)
288				&& ch != '\b' && ch != '\t' && ch != '\n')
289			{
290				return null;
291			}
292
293			if(Debug.DUMP_KEY_EVENTS)
294			{
295				Log.log(Log.DEBUG,"KEWa","Key event (working around): "
296					+ AbstractInputHandler.toString(evt)+": last="+last+".");
297			}
298
299			if(!Debug.ALTERNATIVE_DISPATCHER)
300			{
301				if(((modifiers & InputEvent.CTRL_MASK) != 0
302					^ (modifiers & InputEvent.ALT_MASK) != 0)
303					|| (modifiers & InputEvent.META_MASK) != 0)
304				{
305					return null;
306				}
307			}
308
309			// if the last key was a numeric keypad key
310			// and NumLock is off, filter it out
311			if(last == LAST_NUMKEYPAD)
312			{
313				last = LAST_NOTHING;
314				if((ch >= '0' && ch <= '9') || ch == '.'
315					|| ch == '/' || ch == '*'
316					|| ch == '-' || ch == '+')
317				{
318					return null;
319				}
320			}
321			// Windows JDK workaround
322			else if(last == LAST_ALT)
323			{
324				last = LAST_NOTHING;
325				switch(ch)
326				{
327				case 'B':
328				case 'M':
329				case 'X':
330				case 'c':
331				case '!':
332				case ',':
333				case '?':
334					return null;
335				}
336			}
337			break;
338		//}}}
339		//{{{ KEY_RELEASED...
340		case KeyEvent.KEY_RELEASED:
341			switch(keyCode)
342			{
343			case KeyEvent.VK_ALT:
344				// we consume this to work around the bug
345				// where A+TAB window switching activates
346				// the menu bar on Windows.
347				// http://bugs.sun.com/view_bug.do?bug_id=6458497
348				//
349				// This should be removed if the fix for the
350				// above problem became widely available, to
351				// allow the menu bar activation.
352				evt.consume();
353				break;
354			case KeyEvent.VK_ALT_GRAPH:
355			case KeyEvent.VK_CONTROL:
356			case KeyEvent.VK_SHIFT:
357			case KeyEvent.VK_META:
358				break;
359			case KeyEvent.VK_LEFT:
360			case KeyEvent.VK_RIGHT:
361			case KeyEvent.VK_UP:
362			case KeyEvent.VK_DOWN:
363			case KeyEvent.VK_PAGE_UP:
364			case KeyEvent.VK_PAGE_DOWN:
365			case KeyEvent.VK_END:
366			case KeyEvent.VK_HOME:
367				/* workaround for A+keys producing
368				 * garbage on Windows */
369				if(modifiers == InputEvent.ALT_MASK)
370					last = LAST_ALT;
371				break;
372			}
373			break;
374		//}}}
375		}
376		return evt;
377	} //}}}
378
379	//{{{ numericKeypadKey() method
380	/**
381	 * A workaround for non-working NumLock status in some Java versions.
382	 * @since jEdit 4.0pre8
383	 */
384	public static void numericKeypadKey()
385	{
386		last = LAST_NOTHING;
387	} //}}}
388
389	//{{{ Private members
390	private static int last;
391	private static final int LAST_NOTHING = 0;
392	private static final int LAST_NUMKEYPAD = 1;
393	private static final int LAST_ALT = 2;
394	//}}}
395}