Results 1 to 9 of 9
  1. #1
    Lil_Aziz1's Avatar
    Lil_Aziz1 is offline Senior Member
    Join Date
    Dec 2009
    Location
    United States
    Posts
    343
    Rep Power
    5

    Default When would one use the KeyStroke class?

    I just finished reading about implementing key listeners and it talked about using KeyEvent to differentiate between the keys on a keyboard. As I started reading about the focus subsystem, the class KeyStroke came up. I read the API for it ( KeyStroke (Java 2 Platform SE v1.4.2) ) and it's parent AWTKeyStroke ( AWTKeyStroke (Java 2 Platform SE v1.4.2) ). The KeyStroke class is just full of factory methods that don't seem beneficial at all to me and the AWTKeyStroke makes no sense to me. :S I guess what I'm asking is, what is the KeyStroke class? When/Why should one use it? Thanks in advance!

    Best Regards,

    Lil_Aziz1
    Last edited by Lil_Aziz1; 06-17-2010 at 05:24 PM.
    "Experience is what you get when you don't get what you want" (Dan Stanford)
    "Rise and rise again until lambs become lions" (Robin Hood)

  2. #2
    collin389 is offline Senior Member
    Join Date
    Nov 2009
    Posts
    235
    Rep Power
    5

    Default

    "KeyStrokes are used to define high-level (semantic) action events. Instead of trapping every keystroke and throwing away the ones you are not interested in, those keystrokes you care about automatically initiate actions on the Components with which they are registered." From the API. So I think an example would be KeyStroke.getKeyStroke((char)27); (27 s the char for esc) and then when the user presses esc it will do something. that way you don't have to say:
    Java Code:
    while(c != (char)27)
    {
    scanner.nextChar() = c;
    }
    This would be the "trapping every keystroke and throwing away the ones you are not interested in" part. Anyway, thats how I read it, but I've never used it so someone might come up with a better answer.

    Edit: So I was looking at the getKeyStroke method and it seems like there is one: public static KeyStroke getKeyStroke(Character keyChar, int modifiers) Where modifiers can be ctrl, alt, shift or meta. This would allow someone to wait for the user to press Ctrl+x for example. There is another thread here where someone wants to know when the user presses ctrl+x and This might be a way. However, I've never used this method and am not completely sure how it works. Also. I found this tutorial which uses KeyStroke: http://java.sun.com/docs/books/tutor...eybinding.html
    Last edited by collin389; 06-17-2010 at 12:15 AM.

  3. #3
    camickr is offline Senior Member
    Join Date
    Jul 2009
    Posts
    1,236
    Rep Power
    7

    Default

    Yes, a KeyStroke is used with Key Bindings. You should generally use Key Bindings instead of KeyListeners.

    Old AWT applications used KeyListeners because Key Bindings didn't exist. But know all Swing components use Key Bindings. Check out my quick intro to Key Bindings Java Tips Weblog

  4. #4
    Lil_Aziz1's Avatar
    Lil_Aziz1 is offline Senior Member
    Join Date
    Dec 2009
    Location
    United States
    Posts
    343
    Rep Power
    5

    Default

    Oh alright. So KeyListener listens for every key pressed while using Key Binding listens for only the keys you specifically want to listen to. Is that correct? Thanks both of you!
    Last edited by Lil_Aziz1; 06-17-2010 at 05:20 PM.
    "Experience is what you get when you don't get what you want" (Dan Stanford)
    "Rise and rise again until lambs become lions" (Robin Hood)

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

    Default

    As far as I know, key listeners will listen for almost all key presses (they don't work for shift, control, etc ... again AFAIK), but only if the component that's doing the listening has focus. Key Binding will perform actions in response to specific key presses and this can be set up so that it only works for a focused component or if the component is held in a window that has focus, but there are restrictions on its use as well, for instance if you try to bind a key that's already bound.

    Caveat: I've used key bindings but am no expert, so corrections are most welcome.

  6. #6
    Lil_Aziz1's Avatar
    Lil_Aziz1 is offline Senior Member
    Join Date
    Dec 2009
    Location
    United States
    Posts
    343
    Rep Power
    5

    Default

    Ooo so Key Binding is just an efficient version of KeyListener
    "Experience is what you get when you don't get what you want" (Dan Stanford)
    "Rise and rise again until lambs become lions" (Robin Hood)

  7. #7
    camickr is offline Senior Member
    Join Date
    Jul 2009
    Posts
    1,236
    Rep Power
    7

    Default

    they don't work for shift, control, etc ... again AFAIK
    Yes KeyEvents work for those keys. You can listen for those keys individually or you can listen for them in combination with other keys (ie. Control+F1).

  8. #8
    camickr is offline Senior Member
    Join Date
    Jul 2009
    Posts
    1,236
    Rep Power
    7

    Default

    so Key Binding is just an efficient version of KeyListener
    It has nothing to do with efficiency (in fact Key Bindings probably cause more overhead).

    Its about using better OO design principles. Key Bindings simply allow you to map KeyStrokes to an Action. It allows you to create Actions that can potentially be used by many different compnents. This is exactly how all the text components work. All the cut, copy, paste Actions can be reused. It allows you to override or replace a default Action easily.


    There is no need for big if/else statements. Any time you see code like this you are not using OO techniques.

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

    Default

    Quote Originally Posted by camickr View Post
    Yes KeyEvents work for those keys. You can listen for those keys individually or you can listen for them in combination with other keys (ie. Control+F1).
    Thanks for correcting my mistake and educating us (as usual)!

Similar Threads

  1. Replies: 4
    Last Post: 11-26-2009, 10:40 AM
  2. How to make virtual keystroke output?
    By Lynx09 in forum Suggestions & Feedback
    Replies: 2
    Last Post: 03-31-2009, 01:33 AM
  3. More than one KeyStroke (Shortcut) for a JMenuItem
    By hannehomuth in forum Advanced Java
    Replies: 0
    Last Post: 07-25-2008, 04:35 PM
  4. how to read single Keystroke without using return
    By oomanamemni in forum New To Java
    Replies: 1
    Last Post: 02-14-2008, 08:21 PM
  5. Replies: 1
    Last Post: 07-29-2007, 11:53 PM

Posting Permissions

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