Is There a Keyboard Event Listener?

K

kvnsmnsn

I've written Java programs that interface with my mouse using the
<MouseListener> and <MouseMotionListener> interfaces and the
<MouseEvent> class. Is there a way to write a listener for keyboard
entries, so that pressing a key on the keyboard causes an event that I
can write a listener for? Any information on this would be greatly
appreciated.

---Kevin Simonson

"You'll never get to heaven, or even to LA,
if you don't believe there's a way."
from _Why Not_
 
A

Abrasive Sponge

I've written Java programs that interface with my mouse using the
<MouseListener> and <MouseMotionListener> interfaces and the
<MouseEvent> class. Is there a way to write a listener for keyboard
entries, so that pressing a key on the keyboard causes an event that I
can write a listener for? Any information on this would be greatly
appreciated.

---Kevin Simonson

"You'll never get to heaven, or even to LA,
if you don't believe there's a way."
from _Why Not_

KeyListener
 
K

kvnsmnsn

Thanks for the input from Abrasive Sponge and Hal Rosser. I used
<KeyListener> in my code as shown below. Then I ran the application,
which brought up a <JFrame>, I clicked in the <JFrame>, and typed an
'a', but nothing happened. Anyone know why? What I want is for my
<IdKeys> panel to recognize that I've typed in a character and write
to the panel what the character is. Any pointers on this would be
greatly appreciated. If it's relevant, my Java version is 1.4.2_05.

---Kevin Simonson

"You'll never get to heaven, or even to LA,
if you don't believe there's a way."
from _Why Not_

####################################################################

import javax.swing.JFrame;
import javax.swing.JPanel;
import java.awt.Graphics;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.event.KeyListener;
import java.awt.event.KeyEvent;

public class IdKeys extends JPanel
{
boolean keyEntered = false;
int keyId;

public IdKeys ()
{
setPreferredSize( new Dimension( 800, 800));
addKeyListener( new IdListener());
}

protected void paintComponent ( Graphics page)
{
page.setColor( Color.black);
page.fillRect( 0, 0, 800, 800);
page.setColor( Color.green);
if (keyEntered)
{ if (keyId == KeyEvent.VK_A)
{ page.drawString( "VK_A", 400, 400);
}
else if (keyId == KeyEvent.VK_B)
{ page.drawString( "VK_B", 400, 400);
}
else
{ page.drawString( "other value (" + keyId + ")", 400, 400);
}
keyEntered = false;
}
else
{ page.drawString( "No key entered.", 400, 400);
}
}

private class IdListener implements KeyListener
{
public void keyPressed ( KeyEvent kyEvnt)
{
keyId = kyEvnt.getKeyCode();
keyEntered = true;
repaint();
}

public void keyReleased ( KeyEvent kyEvnt) {}
public void keyTyped ( KeyEvent kyEvnt) {}
}

public static void main ( String[] arguments)
{
JFrame idFrame = new JFrame( "id program");
IdKeys idPanel = new IdKeys();

idFrame.setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE);
idFrame.getContentPane().add( idPanel);
idFrame.pack();
idFrame.setVisible( true);
}
}
 
H

Hal Rosser

Thanks for the input from Abrasive Sponge and Hal Rosser. I used
<KeyListener> in my code as shown below. Then I ran the application,
which brought up a <JFrame>, I clicked in the <JFrame>, and typed an
'a', but nothing happened. Anyone know why? What I want is for my
<IdKeys> panel to recognize that I've typed in a character and write
to the panel what the character is. Any pointers on this would be
greatly appreciated. If it's relevant, my Java version is 1.4.2_05.

---Kevin Simonson

"You'll never get to heaven, or even to LA,
if you don't believe there's a way."
from _Why Not_

####################################################################

import javax.swing.JFrame;
import javax.swing.JPanel;
import java.awt.Graphics;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.event.KeyListener;
import java.awt.event.KeyEvent;

public class IdKeys extends JPanel
{
boolean keyEntered = false;
int keyId;

public IdKeys ()
{
setPreferredSize( new Dimension( 800, 800));
addKeyListener( new IdListener());
}

protected void paintComponent ( Graphics page)
{
page.setColor( Color.black);
page.fillRect( 0, 0, 800, 800);
page.setColor( Color.green);
if (keyEntered)
{ if (keyId == KeyEvent.VK_A)
{ page.drawString( "VK_A", 400, 400);
}
else if (keyId == KeyEvent.VK_B)
{ page.drawString( "VK_B", 400, 400);
}
else
{ page.drawString( "other value (" + keyId + ")", 400, 400);
}
keyEntered = false;
}
else
{ page.drawString( "No key entered.", 400, 400);
}
}

private class IdListener implements KeyListener
{
public void keyPressed ( KeyEvent kyEvnt)
{
keyId = kyEvnt.getKeyCode();
keyEntered = true;
repaint();
}

public void keyReleased ( KeyEvent kyEvnt) {}
public void keyTyped ( KeyEvent kyEvnt) {}
}

public static void main ( String[] arguments)
{
JFrame idFrame = new JFrame( "id program");
IdKeys idPanel = new IdKeys();

idFrame.setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE);
idFrame.getContentPane().add( idPanel);
idFrame.pack();
idFrame.setVisible( true);
}
}

In your constructor, try
addKeyListener(this);
instead of
addKeyListener( new IdListener());
 
K

kvnsmnsn

Hal Rosser wrote:

=In your constructor, try
=addKeyListener(this);
=instead of
=addKeyListener( new IdListener());

I wrote a new version of the previous Java application that took
care of everything in one class, so I could do what you suggested, re-
sulting in the following code. I still got the same results.
"No key entered." was printed to the center of the screen (since
<paintComponent> is called once to begin with) and that was all. Typ-
ing the 'a' key made no difference.

Does anybody have any idea what I would have to do to get my Java
application to recognize that I've typed a character and output some-
thing saying I've typed that character?

---Kevin Simonson

"You'll never get to heaven, or even to LA,
if you don't believe there's a way."
from _Why Not_

####################################################################

import javax.swing.JFrame;
import javax.swing.JPanel;
import java.awt.Graphics;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.event.KeyListener;
import java.awt.event.KeyEvent;

public class Rosser extends JPanel implements KeyListener
{
boolean keyEntered = false;
int keyId;

public Rosser ()
{
setPreferredSize( new Dimension( 800, 800));
addKeyListener( this);
}

protected void paintComponent ( Graphics page)
{
page.setColor( Color.black);
page.fillRect( 0, 0, 800, 800);
page.setColor( Color.green);
if (keyEntered)
{ if (keyId == KeyEvent.VK_A)
{ page.drawString( "VK_A", 400, 400);
}
else if (keyId == KeyEvent.VK_B)
{ page.drawString( "VK_B", 400, 400);
}
else
{ page.drawString( "other value (" + keyId + ")", 400, 400);
}
keyEntered = false;
}
else
{ page.drawString( "No key entered.", 400, 400);
}
}

public void keyPressed ( KeyEvent kyEvnt)
{
keyId = kyEvnt.getKeyCode();
keyEntered = true;
repaint();
}

public void keyReleased ( KeyEvent kyEvnt) {}
public void keyTyped ( KeyEvent kyEvnt) {}

public static void main ( String[] arguments)
{
JFrame idFrame = new JFrame( "id program");
Rosser idPanel = new Rosser();

idFrame.setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE);
idFrame.getContentPane().add( idPanel);
idFrame.pack();
idFrame.setVisible( true);
}
}
 
C

Chris Uppal

protected void paintComponent ( Graphics page)
{ [...]
keyEntered = false;

Why are you doing this ? It means that you discard the record of the keypress
whenever the system asks you to repaint (any part of) the window.

-- chris
 
S

sanjay manohar

Two possible problems
1) your JPanel mightn't have the focus. Your panel should be allowed to
get focus when clicked on, or else contain a component that is.
2) as chris said, you need to record which key was last pressed,
because the paintComponent() method may be called more than once (even
though you only call repaint once). This might immediately remove what
you had painted.
 
K

kvnsmnsn

sanjay manohar wrote:

=Two possible problems
=1) your JPanel mightn't have the focus. Your panel should be allowed
to
=get focus when clicked on, or else contain a component that is.

How do I give my <JPanel> the focus, or allow it to get focus when
clicked on?

=2) as chris said, you need to record which key was last pressed,
=because the paintComponent() method may be called more than once (even
=though you only call repaint once). This might immediately remove what
=you had painted.

Okay, I changed my code so it should remember every key that's
pressed, and prints them all everytime <paintComponent> is called. I
also added some code so that if the mouse is clicked, that event is
remembered too. It works; if I click somewhere on my <JPanel> the ap-
plication prints out where I clicked on it; but I still type 'a' or
'b' and get nothing.

---Kevin Simonson

"You'll never get to heaven, or even to LA,
if you don't believe there's a way."
from _Why Not_

####################################################################

import javax.swing.JFrame;
import javax.swing.JPanel;
import java.awt.Graphics;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Point;
import java.awt.event.KeyListener;
import java.awt.event.KeyEvent;
import java.awt.event.MouseListener;
import java.awt.event.MouseEvent;

public class Rosser extends JPanel implements KeyListener,
MouseListener
{
int[] keysEntered = new int[ 100];
int numberOfKeys = 0;

public Rosser ()
{
setPreferredSize( new Dimension( 800, 800));
addKeyListener( this);
addMouseListener( this);
}

protected void paintComponent ( Graphics page)
{
int enteredKey;

page.setColor( Color.black);
page.fillRect( 0, 0, 800, 800);
page.setColor( Color.green);
page.drawString( "Entered keys:", 400, 200);
page.setColor( Color.cyan);
for (enteredKey = 0; enteredKey < numberOfKeys; enteredKey++)
{ if (keysEntered[ enteredKey] == KeyEvent.VK_A)
{ page.drawString( "VK_A", 400, 215 + 15 * enteredKey);
}
else if (keysEntered[ enteredKey] == KeyEvent.VK_B)
{ page.drawString( "VK_B", 400, 215 + 15 * enteredKey);
}
else if (keysEntered[ enteredKey] == KeyEvent.VK_C)
{ page.drawString( "VK_C", 400, 215 + 15 * enteredKey);
}
else
{ page.drawString
( "other value (" + keysEntered[ enteredKey] + ")"
, 400, 215 + 15 * enteredKey);
}
}
page.setColor( Color.green);
page.drawString( "End of entered keys.", 400, 215 + 15 *
numberOfKeys);
}

public void keyPressed ( KeyEvent kyEvnt)
{
keysEntered[ numberOfKeys++] = kyEvnt.getKeyCode();
repaint();
}

public void mousePressed ( MouseEvent msEvnt)
{
Point pressed = msEvnt.getPoint();

keysEntered[ numberOfKeys++] = 10000 * pressed.x + pressed.y;
repaint();
}

public void keyReleased ( KeyEvent kyEvnt) {}
public void keyTyped ( KeyEvent kyEvnt) {}

public void mouseClicked ( MouseEvent msEvnt) {}
public void mouseEntered ( MouseEvent msEvnt) {}
public void mouseExited ( MouseEvent msEvnt) {}
public void mouseReleased ( MouseEvent msEvnt) {}

public static void main ( String[] arguments)
{
JFrame idFrame = new JFrame( "id program");
Rosser idPanel = new Rosser();

idFrame.setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE);
idFrame.getContentPane().add( idPanel);
idFrame.pack();
idFrame.setVisible( true);
}
}
 
K

kvnsmnsn

Chris Uppal wrote:

=> How do I give my <JPanel> the focus, or allow it to get focus when
=> clicked on?
=
=You'll need at least to add:
=
= public boolean isFocusable() { return true; }
=
=to your component. There are other issues you should probably think
about;
=see the tutorial for background. Specifically:
=
=http://java.sun.com/docs/books/tutorial/uiswing/events/keylistener.html

Thanks! This worked just fine. And thanks to everybody else who has
helped me with this problem.

---Kevin Simonson

"You'll never get to heaven, or even to LA,
if you don't believe there's a way."
from _Why Not_
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,995
Messages
2,570,225
Members
46,815
Latest member
treekmostly22

Latest Threads

Top