Solved

Is there a KeyPreview equivalent in Java?

Posted on 2014-10-23
4
267 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

Live: Real-Time Solutions, Start Here

Receive instant 1:1 support from technology experts, using our real-time conversation and whiteboard interface. Your first 5 minutes are always free.

Question has a verified solution.

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

For beginner Java programmers or at least those new to the Eclipse IDE, the following tutorial will show some (four) ways in which you can import your Java projects to your Eclipse workbench. Introduction While learning Java can be done with…
By the end of 1980s, object oriented programming using languages like C++, Simula69 and ObjectPascal gained momentum. It looked like programmers finally found the perfect language. C++ successfully combined the object oriented principles of Simula w…
Viewers learn about the scanner class in this video and are introduced to receiving user input for their programs. Additionally, objects, conditional statements, and loops are used to help reinforce the concepts. Introduce Scanner class: Importing…
This tutorial will introduce the viewer to VisualVM for the Java platform application. This video explains an example program and covers the Overview, Monitor, and Heap Dump tabs.

776 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