Results 1 to 5 of 5
  1. #1
    ushigley is offline Member
    Join Date
    Aug 2008
    Posts
    1
    Rep Power
    0

    Question Reading Keyboard

    Easy question I realize, but for some reason nobody has made a legitimate tutorial for it...

    I just want to know how to check if a certain key (okay a couple different keys) has been pressed, and if it has, it calls a function. If it matters this is for JFrame, not console.

    Thanks in advance.

  2. #2
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    This may not be quite as easy as it seems. I suggest that you read the Sun tutorial on key binding and use this technique to trap keys in the JPanel that holds your application. Good luck!

  3. #3
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,305
    Rep Power
    25

    Default

    Look at keyListeners. These will capture keyboard input key events.

  4. #4
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    Look at keyListeners. These will capture keyboard input key events.
    I don't disagree directly as these will often work, but if you are using Swing and not AWT, key bindings are the preferred way to do this. For one thing, it's much easier to trap keys with key bindings vs key listeners if the component that holds the bindings / listeners loses focus but is still held in a component that has focus. Also as per the Sun experts:

    "An alternative to key bindings is using key listeners. Key listeners have their place as a low-level interface to keyboard input, but for responding to individual keys key bindings are more appropriate and tend to result in more easily maintained code. Key listeners are also difficult if the key binding is to be active when the component doesn't have focus. Some of the advantages of key bindings are they're somewhat self documenting, take the containment hierarchy into account, encourage reusable chunks of code (Action objects), and allow actions to be easily removed, customized, or shared. Also, they make it easy to change the key to which an action is bound. Another advantage of Actions is that they have an enabled state which provides an easy way to disable the action without having to track which component it is attached to. "

  5. #5
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,305
    Rep Power
    25

Similar Threads

  1. getting each character from keyboard
    By Sreejesh25 in forum New To Java
    Replies: 11
    Last Post: 01-25-2011, 01:08 PM
  2. getting each character from keyboard
    By Sreejesh25 in forum Advanced Java
    Replies: 6
    Last Post: 03-05-2010, 07:12 PM
  3. Need help with on-screen keyboard (first post)
    By tom_k in forum New To Java
    Replies: 7
    Last Post: 07-31-2008, 02:07 AM
  4. Assign a keyboard key to a JButton.
    By gszauer in forum AWT / Swing
    Replies: 1
    Last Post: 12-15-2007, 10:42 PM
  5. Help with keyboard events?
    By Bibendum in forum New To Java
    Replies: 2
    Last Post: 11-02-2007, 02:51 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •