Name: rm29839 Date: 10/20/97
Problem:
KeyEvents are handled differently between Win95 and
Solaris with TextFields. Specifically, in the code provided, on
Win95, consume() works correctly (i.e., on an
InputEvent, consume() marks the event so that
default processing from a component does not occur)
It does not on Solaris.
Try the source code below:
import java.awt.*;
import java.awt.event.*;
public class SBRTextField extends TextField {
// INSTANCE VARIABLES
private SBRKeyEventHandler keyHandler;
// CONSTRUCTOR
public SBRTextField(int cols) {
super(cols);
this.keyHandler = new SBRKeyEventHandler();
enableEvents(AWTEvent.KEY_EVENT_MASK);
}
/** Process key events in a special way before sending it on
* to all listeners, etc.
*/
public void processEvent(AWTEvent e) {
if (e.getID() == KeyEvent.KEY_TYPED) {
KeyEvent ke = (KeyEvent) e;
keyHandler.keyTyped(ke);
ke.consume();
}
super.processEvent(e);
}
// INNER CLASS
class SBRKeyEventHandler extends KeyAdapter {
/** Method handles KeyPressed, KeyReleased sequences. It upper-cases
* appropriately, and translates from keyboard to Display characters
*/
public void keyTyped(KeyEvent ke) {
char c = ke.getKeyChar();
c = Character.toUpperCase(c);
ke.setKeyChar(c);
}
}
// DEBUGGING MAIN
public static void main(String[] args) {
Frame f = new Frame();
f.add(new SBRTextField(15));
f.pack();
f.setVisible(true);
}
}
MEANWHILE,
there appears to be a bug on Win95 that does not
occur on Solaris. Specifically, any InputEvents
with set methods are supposed to be COPIED before
being sent to individual listeners; however, if
you remove "ke.consume()" above, you will note
that on Solaris, the component behaves
correctly, i.e., typed characters do not appear
as upper case characters despite the keyTyped()
method in the KeyEventHandler. On Win95,however,
the characters ARE upper-cased.
======================================================================