Article View: borland.public.jbuilder.java.api
Article #3072Keyboard Event Handling Woes...
From: Gene Britton
Date: Tue, 21 Apr 1998 00:00
Date: Tue, 21 Apr 1998 00:00
193 lines
6473 bytes
6473 bytes
Greetings to all:
I'm yet another one having problems with keyboard event handling. My
problem is this: I need to be able to create a form containing multiple
controls, and then be able to listen for keyboard events on a FORM
basis, as well as on a COMPONENT basis! My form is just a part of a
multi-form application, and will be encapsulated as a Bean. I have no
idea what extra controls will be added in the future, and certainly
cannot add event handlers to or fill event handlers in for controls that
my users will be adding! My Bean will be defining a set of "Hot Keys",
and need to be able to trap them not only on the form that I am
developing, but throughout the application. It appears, however, that
either Java or the AWT is not capable of dispatching keyboard events to
multiple clients "simultaneously", even using Multi-Casting!
Apparently, whatever component on the active form has focus is directed
ALL keyboard events EXCLUSIVELY! A corrolary to this is that if there
are ANY components on the active form, then keyboard events NEVER appear
at the form level, they are always directed to the component that has
focus. What I need is a way to be able to listen in to keyboard activity
not only on the form level, but also at the higher level of the
application or applet.
In the following code example, you will find a JBuilder 1.01 IDE
generated applet that illustrates this problem. This is just a simple
applet with 1 TextField component added to it. Keyboard handlers were
then added to both the applet and the TextField. When run, the event
handlers for the TextField fire, but for the applet do not. If the event
handlers for the TextField are removed, the event handlers for the
applet still do not fire. If the TextField is removed from the form,
however, the applet event handlers do fire. My project requires that
BOTH the component AND the applet event handlers fire! The Multi-Casting
event mechanism is supposed to do just that! I am certain that I could
go in and modify the source to the AWT and provide for the desired (and
correct!) behavior, but then the app would not be portable, and
modifying the core classes is a great big No-No, anyway. Heaven Forbid
that we actually fix the problem...
Any Suggestions?
Gene Britton
Software Engineer
Stratasoft
//Title: Java Test 9
//Version:
//Copyright: Copyright (c) 1998
//Author: Gene Britton
//Company: Stratasoft
//Description: Java Test 9 -- test of keyboard processing
import java.awt.*;
import java.awt.event.*;
import java.applet.*;
import borland.jbcl.layout.*;
import borland.jbcl.control.*;
public class Jt9Applet extends Applet {
XYLayout xYLayout1 = new XYLayout();
boolean isStandalone = false;
TextField textField1 = new TextField();
//Get a parameter value
public String getParameter(String key, String def) {
return isStandalone ? System.getProperty(key, def) :
(getParameter(key) != null ? getParameter(key) : def);
}
//Construct the applet
public Jt9Applet() {
}
//Initialize the applet
public void init() {
try { jbInit(); } catch (Exception e) { e.printStackTrace(); }
}
//Component initialization
private void jbInit() throws Exception{
this.addKeyListener(new Jt9Applet_this_keyAdapter(this));
xYLayout1.setWidth(400);
xYLayout1.setHeight(300);
textField1.addKeyListener(new
Jt9Applet_textField1_keyAdapter(this));
this.setLayout(xYLayout1);
this.add(textField1, new XYConstraints(31, 33, 79, 25));
}
//Start the applet
public void start() {
}
//Stop the applet
public void stop() {
}
//Destroy the applet
public void destroy() {
}
//Get Applet information
public String getAppletInfo() {
return "Applet Information";
}
//Get parameter info
public String[][] getParameterInfo() {
return null;
}
//Main method
public static void main(String[] args) {
Jt9Applet applet = new Jt9Applet();
applet.isStandalone = true;
DecoratedFrame frame = new DecoratedFrame();
frame.setTitle("Applet Frame");
frame.add(applet, BorderLayout.CENTER);
applet.init();
applet.start();
frame.pack();
Dimension d = Toolkit.getDefaultToolkit().getScreenSize();
frame.setLocation((d.width - frame.getSize().width) / 2, (d.height -
frame.getSize().height) / 2);
frame.setVisible(true);
}
void this_keyPressed(KeyEvent e) {
System.out.println("applet key pressed event!");
}
void this_keyReleased(KeyEvent e) {
System.out.println("applet key released event!");
}
void this_keyTyped(KeyEvent e) {
System.out.println("applet key typed event!");
}
void textField1_keyPressed(KeyEvent e) {
System.out.println("control key pressed event!");
}
void textField1_keyReleased(KeyEvent e) {
System.out.println("control key released event!");
}
void textField1_keyTyped(KeyEvent e) {
System.out.println("control key typed event!");
}
}
class Jt9Applet_this_keyAdapter extends java.awt.event.KeyAdapter {
Jt9Applet adaptee;
Jt9Applet_this_keyAdapter(Jt9Applet adaptee) {
this.adaptee = adaptee;
}
public void keyPressed(KeyEvent e) {
adaptee.this_keyPressed(e);
}
public void keyReleased(KeyEvent e) {
adaptee.this_keyReleased(e);
}
public void keyTyped(KeyEvent e) {
adaptee.this_keyTyped(e);
}
}
class Jt9Applet_textField1_keyAdapter extends java.awt.event.KeyAdapter
{
Jt9Applet adaptee;
Jt9Applet_textField1_keyAdapter(Jt9Applet adaptee) {
this.adaptee = adaptee;
}
public void keyPressed(KeyEvent e) {
adaptee.textField1_keyPressed(e);
}
public void keyReleased(KeyEvent e) {
adaptee.textField1_keyReleased(e);
}
public void keyTyped(KeyEvent e) {
adaptee.textField1_keyTyped(e);
}
}
Technical Details
Message-ID:
<353D120C.4738D27E@allstar.com>Path:
arc1-ca.pugleaf.net!unknown!not-for-mailShow Full Headers
Complete Article Headers
X-Google-Language: ENGLISH,ASCII-7-bit X-Google-Thread: 1089d0,dde16fc43e58b89b,start X-Google-Attributes: gid1089d0,public From: Gene Britton <gbritton@allstar.com> Subject: Keyboard Event Handling Woes... Date: 1998/04/21 Message-ID: <353D120C.4738D27E@allstar.com> X-Deja-AN: 346430291 Content-Transfer-Encoding: 7bit Content-Type: text/plain; charset=us-ascii Organization: Another Netscape Collabra Server User Mime-Version: 1.0 Newsgroups: borland.public.jbuilder.java.api