Results 1 to 7 of 7
  1. #1
    ChazZeromus is offline Member
    Join Date
    Sep 2008
    Posts
    13
    Rep Power
    0

    Default How to simply get if a key is pushed?

    I used the keyboard listener but it only takes in keystrokes in the usual linear intervals. You know, like when you're typing. Is there any simple way to just grab whether a key is held down?

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

    Default

    In what context? Swing? AWT? SWT? None of the above?

  3. #3
    ChazZeromus is offline Member
    Join Date
    Sep 2008
    Posts
    13
    Rep Power
    0

    Default

    Well I came up with a solution using swing, I used a key listener on my main JApplet class but it doesn't detect any keystrokes until i click on the window of the app. I always have to click on the already focused window. And it turns out that my previous solution wasn't using a listener but a dispatcher, so that explained the keystroke-like registering.

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

    Default

    Have you looked at using key bindings? These can work even if the component that has the binding doesn't have focus but is held by the window with focus.

  5. #5
    ChazZeromus is offline Member
    Join Date
    Sep 2008
    Posts
    13
    Rep Power
    0

    Default

    Not at all, how would I do that using my JApplet or Jframe class?

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

    Default

    Here's an example program that shows use of key bindings. In this program pressing one of the arrow keys together with alt will start a Swing Timer that will keep firing (appending a String in a JTextArea) until the alt-arrow key has been released.

    I'm no expert in key binding, so please take this program as only a suggestion for ideas, not as gospel. Also, since for unknown reasons the forum doesn't allow posting of code with key binding in it, I created a Jar file with source code and then changed the extension to zip and uploaded it. Please let me know if I can answer any questions about this.

    Best of luck
    Attached Files Attached Files

  7. #7
    ChazZeromus is offline Member
    Join Date
    Sep 2008
    Posts
    13
    Rep Power
    0

    Default

    Well I already came up with a sugerpacket solution by creating a keylistener and adding that single listener to JApplet and JFrame. It works, well I'd say.

    As for the example, how exactly do you perform the example? I looked at the code and it looks like a timer's suppose to trigger or something causing some text to appear in one of the edit controls? I'm not sure.

Similar Threads

  1. It simply won't sort
    By xf021209 in forum New To Java
    Replies: 7
    Last Post: 04-25-2009, 03:53 PM
  2. input to an integer (simply)
    By chitwood in forum Advanced Java
    Replies: 3
    Last Post: 03-18-2009, 06:34 AM
  3. Replies: 6
    Last Post: 10-17-2008, 06:11 PM
  4. Error With Simply Displaying Picture
    By JDCAce in forum Java Applets
    Replies: 4
    Last Post: 10-07-2008, 08:09 PM
  5. Simply output the result to a text file.
    By silvia in forum New To Java
    Replies: 1
    Last Post: 08-07-2007, 05:48 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
  •