Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Is there a KeyPreview equivalent in Java?

Posted on 2014-10-23
4
Medium Priority
?
329 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
[X]
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
  • 2
  • 2
4 Comments
 
LVL 36

Accepted Solution

by:
mccarl earned 2000 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 36

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

Build and deliver software with DevOps

A digital transformation requires faster time to market, shorter software development lifecycles, and the ability to adapt rapidly to changing customer demands. DevOps provides the solution.

Question has a verified solution.

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

Go is an acronym of golang, is a programming language developed Google in 2007. Go is a new language that is mostly in the C family, with significant input from Pascal/Modula/Oberon family. Hence Go arisen as low-level language with fast compilation…
Introduction This article is the second of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article covers the basic installation and configuration of the test automation tools used by…
Viewers will learn how to properly install Eclipse with the necessary JDK, and will take a look at an introductory Java program. Download Eclipse installation zip file: Extract files from zip file: Download and install JDK 8: Open Eclipse and …
How to fix incompatible JVM issue while installing Eclipse While installing Eclipse in windows, got one error like above and unable to proceed with the installation. This video describes how to successfully install Eclipse. How to solve incompa…
Suggested Courses

722 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