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