Solved

Is there a KeyPreview equivalent in Java?

Posted on 2014-10-23
4
241 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
Comment Utility
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
Comment Utility
You are a God among men.
0
 
LVL 35

Expert Comment

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

Author Comment

by:Javin007
Comment Utility
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

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
endX challenge 2 48
JAVA part two 5 39
bitbucket vs gitbucket 3 29
backtracking recursion  code 19 39
Java contains several comparison operators (e.g., <, <=, >, >=, ==, !=) that allow you to compare primitive values. However, these operators cannot be used to compare the contents of objects. Interface Comparable is used to allow objects of a cl…
In this post we will learn how to connect and configure Android Device (Smartphone etc.) with Android Studio. After that we will run a simple Hello World Program.
This tutorial covers a step-by-step guide to install VisualVM launcher in eclipse.
This theoretical tutorial explains exceptions, reasons for exceptions, different categories of exception and exception hierarchy.

743 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

Need Help in Real-Time?

Connect with top rated Experts

8 Experts available now in Live!

Get 1:1 Help Now