Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people, just like you, are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
Solved

Is there a KeyPreview equivalent in Java?

Posted on 2014-10-23
4
284 Views
Last Modified: 2014-10-26
In other languages (.NET, Delphi, etc.) there's a KeyPreview option where you can flag it so that you can capture KeyPress events regardless of which component they take place in.  I can't seem to find an equivalent in Java.

I know that there's a swing KeyStroke object you can use to "bind" individual "actions" to a specific method, but this is definitely not what I'm looking for.  

So far, the only options I can see is either:
    1.) Overlaying an invisible object over the entire window, then manually handling all key events, focus, etc. in all objects (horrible).  
    2.) Add a static custom KeyInputListener to every single component in the application.  (Almost as horrible as 1, but not quite.)  
    3.) Create a KeyStroke "bind" for every conceivable keypress on every conceivable keyboard type.  Impossible.

Hoping for something much cleaner.
0
Comment
Question by:Javin007
  • 2
  • 2
4 Comments
 
LVL 35

Accepted Solution

by:
mccarl earned 500 total points
ID: 40400776
Put the following code somewhere in your Frame initialization (or somewhere else appropriate)...

        KeyboardFocusManager.getCurrentKeyboardFocusManager().addKeyEventDispatcher(new KeyEventDispatcher() {
            @Override
            public boolean dispatchKeyEvent(KeyEvent e) {
                System.out.println("ID: " + e.getID() + ", KeyCode: " + e.getKeyCode());
                return false;
            }
        });

Open in new window

0
 
LVL 4

Author Comment

by:Javin007
ID: 40402330
You are a God among men.
0
 
LVL 35

Expert Comment

by:mccarl
ID: 40403445
Thank you for that complement! ;) Glad to help
0
 
LVL 4

Author Comment

by:Javin007
ID: 40405609
No, seriously, every Java question I've had, you've got the right answer.  I just hope you're getting compensated appropriately in your god-ish java programming job.
0

Featured Post

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
session migration servlets 2 42
Notify sent to other threads in Java 9 33
Java basic valueOf question 1 29
passing enum to a method 4 20
Java had always been an easily readable and understandable language.  Some relatively recent changes in the language seem to be changing this pretty fast, and anyone that had not seen any Java code for the last 5 years will possibly have issues unde…
Introduction Java can be integrated with native programs using an interface called JNI(Java Native Interface). Native programs are programs which can directly run on the processor. JNI is simply a naming and calling convention so that the JVM (Java…
Viewers learn about the third conditional statement “else if” and use it in an example program. Then additional information about conditional statements is provided, covering the topic thoroughly. Viewers learn about the third conditional statement …
Viewers will learn about the regular for loop in Java and how to use it. Definition: Break the for loop down into 3 parts: Syntax when using for loops: Example using a for loop:

861 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question