Mailing Lists: Apple Mailing Lists
Image of Mac OS face in stamp
Bug in key input routines in Java/OS X? Advice most appreciated!
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Bug in key input routines in Java/OS X? Advice most appreciated!



The following little bit of code basically sets up a Panel as its own
key listener, and reports when key pressed/typed/released events
are being fired. It works perfectly well for every key on the keyboard
_except_ the Control key--which does work under Windows. (Actually,
the tab key is also nonfunctional, but I believe that has something
to do with the fact that I have not correctly taken complete management
of the focus--so I'm not too worried about that.) But the control key
should certainly be causing these events (and I need to monitor
them), so any feedback as to why this isn't working would be most
appreciated.

Thanks,
Ken McDonald

import javax.swing.*;
import java.awt.*;
import java.awt.font.*;
import java.awt.event.*;
import java.util.*;

public class TwoProblems extends JPanel {

private Container _display;
private DocPanel _docPanel = new DocPanel();
private JScrollPane _scrollPane;
private int _rectHeight = 50;
private int count = 0;

public class DocPanel extends JPanel implements KeyListener {
public void keyReleased(KeyEvent ke) { System.err.println(count + ") Key Released"); count++; }
public void keyTyped(KeyEvent ke) { System.err.println(count + ") Key Typed"); count++; }
public void keyPressed(KeyEvent ke) { System.err.println(count + ") Key Pressed"); count++; }
public DocPanel() {
setOpaque(true);
setBackground(Color.white);
grabFocus();
this.addKeyListener(this);
}


public void paintComponent(Graphics g) {
grabFocus();
super.paintComponent(g);
}


} /* end class DocPanel */

public boolean isManagingFocus() { return true; }

public void paintComponent(Graphics g) {
super.paintComponent(g);
}

public TwoProblems() {
_scrollPane = new JScrollPane(_docPanel);
setLayout(new BoxLayout(this, BoxLayout.X_AXIS));
add(_scrollPane);
}

public static void main(String[] args) {
JFrame win = new JFrame();

TwoProblems st = new TwoProblems();
win.getContentPane().add(st);
win.setSize(700, 400);
win.show();

}
}




Visit the Apple Store online or at retail locations.
1-800-MY-APPLE

Contact Apple | Terms of Use | Privacy Policy

Copyright © 2011 Apple Inc. All rights reserved.