Adding a key listener problem
Hey, im struggling trying to figure out how to get it to work. I have followed a number of examples online and added the as far as i can figure out all that i need to. But when i press a key nothing happens. Any advice would be helpful. PS. Im using eclipse which is why the stubs are kicking around....
Code:
public class testCanvas extends JPanel implements ,KeyListener
{
testCanvas()
{
this.addKeyListener(listener);
}
KeyListener listener = new KeyListener()
{
@Override
public void keyPressed(KeyEvent e) {
// TODO Auto-generated method stub
dumpInfo("Pressed", e);
}
@Override
public void keyReleased(KeyEvent e) {
// TODO Auto-generated method stub
dumpInfo("Pressed", e);
}
@Override
public void keyTyped(KeyEvent e) {
// TODO Auto-generated method stub
dumpInfo("Pressed", e);
}
private void dumpInfo(String s, KeyEvent e)
{
System.out.println(s);
int code = e.getKeyCode();
System.out.println("\tCode: " + KeyEvent.getKeyText(code));
System.out.println("\tChar: " + e.getKeyChar());
int mods = e.getModifiersEx();
System.out.println("\tMods: "
+ KeyEvent.getModifiersExText(mods));
System.out.println("\tLocation: "
+ location(e.getKeyLocation()));
System.out.println("\tAction? " + e.isActionKey());
}
private String location(int location)
{
switch (location) {
case KeyEvent.KEY_LOCATION_LEFT:
return "Left";
case KeyEvent.KEY_LOCATION_RIGHT:
return "Right";
case KeyEvent.KEY_LOCATION_NUMPAD:
return "NumPad";
case KeyEvent.KEY_LOCATION_STANDARD:
return "Standard";
case KeyEvent.KEY_LOCATION_UNKNOWN:
default:
return "Unknown";
}
}
};
}