PageRenderTime 14ms CodeModel.GetById 2ms app.highlight 9ms 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
  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
 23package org.gjt.sp.jedit.gui;
 24
 25//{{{ Imports
 26import java.awt.event.*;
 27import java.awt.*;
 28//}}}
 29
 30public class KeyEventWorkaround
 31{
 32	// from JDK 1.2 InputEvent.java
 33	public static final int ALT_GRAPH_MASK = 1 << 5;
 34
 35	//{{{ processKeyEvent() method
 36	public static KeyEvent processKeyEvent(KeyEvent evt)
 37	{
 38		int keyCode = evt.getKeyCode();
 39		char ch = evt.getKeyChar();
 40
 41		switch(evt.getID())
 42		{
 43		//{{{ KEY_PRESSED...
 44		case KeyEvent.KEY_PRESSED:
 45			// get rid of keys we never need to handle
 46			if(keyCode == KeyEvent.VK_CONTROL ||
 47				keyCode == KeyEvent.VK_SHIFT ||
 48				keyCode == KeyEvent.VK_ALT ||
 49				keyCode == KeyEvent.VK_META ||
 50				keyCode == '\0')
 51				return null;
 52
 53			if(!mac)
 54				handleBrokenKeys(evt,keyCode);
 55
 56			return evt;
 57		//}}}
 58		//{{{ KEY_TYPED...
 59		case KeyEvent.KEY_TYPED:
 60			// need to let \b through so that backspace will work
 61			// in HistoryTextFields
 62			if((ch < 0x20 || ch == 0x7f || ch == 0xff) && ch != '\b')
 63				return null;
 64
 65			// "Alt" is the option key on MacOS, and it can generate
 66			// user input
 67			if(mac)
 68			{
 69				if(evt.isControlDown() || evt.isMetaDown())
 70					return null;
 71			}
 72			else
 73			{
 74				if((evt.isControlDown() ^ evt.isAltDown())
 75					|| evt.isMetaDown())
 76					return null;
 77
 78				// if the last key was a broken key, filter
 79				// out all except 'a'-'z' that occur 750 ms after.
 80				if(last == LAST_BROKEN && System.currentTimeMillis()
 81					- lastKeyTime < 750 && !Character.isLetter(ch))
 82				{
 83					last = LAST_NOTHING;
 84					return null;
 85				}
 86				// otherwise, if it was ALT, filter out everything.
 87				else if(last == LAST_ALT && System.currentTimeMillis()
 88					- lastKeyTime < 750)
 89				{
 90					last = LAST_NOTHING;
 91					return null;
 92				}
 93			}
 94
 95			return evt;
 96		//}}}
 97		default:
 98			return evt;
 99		}
100	} //}}}
101
102	//{{{ Private members
103
104	//{{{ Static variables
105	private static boolean mac;
106	private static long lastKeyTime;
107
108	private static int last;
109	private static final int LAST_NOTHING = 0;
110	private static final int LAST_ALTGR = 1;
111	private static final int LAST_ALT = 2;
112	private static final int LAST_BROKEN = 3;
113	//}}}
114
115	//{{{ Class initializer
116	static
117	{
118		mac = (System.getProperty("os.name").indexOf("Mac OS") != -1);
119	} //}}}
120
121	//{{{ handleBrokenKeys() method
122	private static void handleBrokenKeys(KeyEvent evt, int keyCode)
123	{
124		if(evt.isAltDown() && evt.isControlDown()
125			&& !evt.isMetaDown())
126		{
127			last = LAST_ALTGR;
128			return;
129		}
130		else if(!(evt.isAltDown() || evt.isControlDown() || evt.isMetaDown()))
131		{
132			last = LAST_NOTHING;
133			return;
134		}
135
136		if(evt.isAltDown())
137			last = LAST_ALT;
138		else if((keyCode < KeyEvent.VK_A || keyCode > KeyEvent.VK_Z)
139			&& keyCode != KeyEvent.VK_LEFT && keyCode != KeyEvent.VK_RIGHT
140			&& keyCode != KeyEvent.VK_UP && keyCode != KeyEvent.VK_DOWN
141			&& keyCode != KeyEvent.VK_DELETE && keyCode != KeyEvent.VK_BACK_SPACE
142			 && keyCode != KeyEvent.VK_TAB && keyCode != KeyEvent.VK_ENTER)
143			last = LAST_BROKEN;
144		else
145			last = LAST_NOTHING;
146
147		lastKeyTime = System.currentTimeMillis();
148	} //}}}
149
150	//}}}
151}