What Is the Listener for Key Presses?

Discussion in 'Java' started by KevinSimonson, Nov 29, 2010.

  1. 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
    KevinSimonson, Nov 29, 2010
    #1
    1. Advertising

  2. On 11/29/2010 10:22 AM, KevinSimonson wrote:
    > 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").

    --
    Beware of bugs in the above code; I have only proved it correct, not
    tried it. -- Donald E. Knuth
    Joshua Cranmer, Nov 29, 2010
    #2
    1. Advertising

  3. KevinSimonson

    Roedy Green Guest

    On Mon, 29 Nov 2010 07:22:53 -0800 (PST), KevinSimonson
    <> wrote, quoted or indirectly quoted someone who
    said :

    >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
    --
    Roedy Green Canadian Mind Products
    http://mindprod.com

    In programming, and documenting programs, keep vocabulary consistent and precisely defined! Variation in vocabulary to relieve the tedium is for novels.
    Roedy Green, Dec 2, 2010
    #3
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Detecting key presses

    , Jun 18, 2006, in forum: Python
    Replies:
    4
    Views:
    471
  2. Tim Daneliuk

    tkinter Ignoring Certain Key Presses?

    Tim Daneliuk, Jan 10, 2007, in forum: Python
    Replies:
    0
    Views:
    294
    Tim Daneliuk
    Jan 10, 2007
  3. Tim Mcd

    Ruby-opengl and key presses?

    Tim Mcd, Jun 4, 2009, in forum: Ruby
    Replies:
    0
    Views:
    107
    Tim Mcd
    Jun 4, 2009
  4. Richard Fairbanks
    Replies:
    0
    Views:
    99
    Richard Fairbanks
    May 3, 2010
  5. Replies:
    1
    Views:
    122
Loading...

Share This Page