What Is the Listener for Key Presses?

K

KevinSimonson

I'm familiar with how one writes code that is executed when the user
clicks or drags a mouse button. How does one write code that is
executed when the user presses a key on the keyboard? I'm not talking
about using the <Scanner> class to detect a string entered terminated
by the <return> key. I want my code to be executed when the user
presses a single key. I used to know how to do this, but I don't have
any existing code that does it any more, and I don't remember the
listener class/interface I need to use. Any pointers anyone can give
me on this would be greatly appreciated.

Kevin S
 
J

Joshua Cranmer

I'm familiar with how one writes code that is executed when the user
clicks or drags a mouse button. How does one write code that is
executed when the user presses a key on the keyboard? I'm not talking
about using the<Scanner> class to detect a string entered terminated
by the<return> key. I want my code to be executed when the user
presses a single key. I used to know how to do this, but I don't have
any existing code that does it any more, and I don't remember the
listener class/interface I need to use. Any pointers anyone can give
me on this would be greatly appreciated.

The standard listener is java.awt.KeyListener, but this is primarily
oriented towards GUI applications. If you want to listen for individual
keystrokes in a console-based applications, you will have to go with
some Java curses-based approach (such for "Java curses").
 
R

Roedy Green

I'm familiar with how one writes code that is executed when the user
clicks or drags a mouse button. How does one write code that is
executed when the user presses a key on the keyboard? I'm not talking
about using the <Scanner> class to detect a string entered terminated
by the <return> key. I want my code to be executed when the user
presses a single key. I used to know how to do this, but I don't have
any existing code that does it any more, and I don't remember the
listener class/interface I need to use. Any pointers anyone can give
me on this would be greatly appreciated.

Kevin S

for sample code, see http://mindprod.com/products1.html#KEYPLAYER
 

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

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,581
Members
45,056
Latest member
GlycogenSupporthealth

Latest Threads

Top