Java - Key Events and Key Binds - java

Is it possible to bind a global key listener for a Java program?
Like windows key binds.
My objective is to when I am in a game, I could, for example, change my current music by pushing F2 or F3.

See the answer for Java System-Wide Keyboard Shortcut. If you want it to apply globally (and not just when the container is minimized), then this should do it. If you want it only for when your container is minimized, then you can take that solution and see if your container is minimized (Frame.getState()).

Related

Java AWT Robot - how to read item text / label?

I need to select appropriate option from the popup window. Unfortunately, it is not the popup displayed by the web page, so I cannot use Selenium framework for that purpose.
As I checked, it is possible to navigate to different option by pressing arrow keys. Thus keyPress and keyRelease​ from Java AWT Robot should work for me perfectly to select option and confirm the selection by pressing Enter key.
Unfortunately, I do not see a method to read currently selected item text. Without that I cannot find appropriate option. Is it possible to read item label using Java AWT Robot?
If the target application is another java application, then you should be able to get a reference to the component hierarchy and traverse it until you find the text field & label in question.
Assuming it's not a java application, then I don't believe it's possible to do this - at least directly. Robot just provides mouse / keyboard input, but you could use it combined with other classes in the toolkit to at least partially solve your problem.
Use the Robot methods keyPress & keyRelease to input CTRL-A. Then CTRL-C.
Once the text is in the clipboard, you could read it using Toolkit.getDefaultToolkit().getSystemClipboard().getData(DataFlavor.stringFlavor)
You might be able to use the same approach for a text label, assuming it is selectable.
This question is talking about using java.awt.Robot to alter text in another program (MS excel), but it might provide you some additional ideas for tackling the problem:
How can I select text in another application with java.awt.robot (example: shift+home)

JavaFX: How can I get the character the represents the platform specific SHORTCUT key code?

I am writing a JavaFX application and I have used accelerators to add shortcut keys to the application menus.
Now I am creating the application help and I want to describe the usage of the shortcut keys.
The recommendation for JavaFX accelerators is to use SHORTCUT instead of CONTROL (Windows) and COMMAND (Apple). This works fine and in the menus when running the application on different platforms show the right key combination.
For example, MenuItem Exit I have added the accelerator SHORTCUT_DOWN + X which is displayed as
Ctrl+X under Windows
and
⌘+X under Mac OS
Now I would like to get the explanations (Ctrl+X, ⌘+X) from the system in order to add it to the user help.
Is it possible to ask JavaFX for the presentation of the accelerator in the menu? Or get the presentation of SHORTCUT_DOWN used in the menus?
Thx in advance
Thorsten
According to the documentation, the getDisplayText() method
Returns a string representation of this KeyCombination that is suitable for display in a user interface (for example, beside a menu item).
So all you need is
String acceleratorAsString = menuItem.getAccelerator().getDisplayText();

Java KeyBindings not working after entering text in jTextField?

Basically, I have a java program that acts like a media player, PLAY PAUSE FAST FORWARD options etc.
Which also have keybindings.
However, in another JPanel I also have a JTextField that allows the user to enter some text to act as commentary.
If the user decides to write some text, then this JTextField is focused and they key bindings don't work anymore.
But if I did setFocusable(false) to all containers and child containers, the user won't be able to enter text into the JTextfield.
How do I solve this?
JComponent#getInputMap uses WHEN_FOCUSED by default.
Depending on your needs you can use WHEN_ANCESTOR_OF_FOCUSED_COMPONENT or WHEN_IN_FOCUSED_WINDOW to change the level of focus you component needs in order to respond to key input
See JComponent#getInputMap(int) and How to Use Key Bindings for more details

Capturing specific keystrokes in Java Swing

There are a lot of topics on how to capture keystrokes in Java Swing, but I'd like to ask about the best practice. For example, I have a window in which I wish to listen to a keystroke of either F1 or Command-P on a Mac (or CTRL-P on a PC).
Reading The official Javadoc for KeyEvent, it seems that it is a better practice to use Key Typed events rather than Key Pressed or Key Released events, because they are higher-level. This makes sense to me, and I've even found that in order to make sure the program is platform-agnostic, I have to specify a keystroke object thusly:
private KeyStroke ks = KeyStroke.getKeyStroke(KeyEvent.VK_P, Toolkit.getDefaultToolkit().getMenuShortcutKeyMask());
This should allow for capturing of either the Command accelerator key on a Mac, and the CTRL accelerator key on a PC. (I hope I'm using those terms correctly.) So now that I have a KeyStroke object, how do I go about checking it against a KeyEvent object in my KeyListener? And throwing a check for an F1 key event as well only complicates the matter further, though hopefully not too much.
Suggestions?
So now that I have a KeyStroke object, how do I go about checking it against a KeyEvent object in my KeyListener?
You don't use a KeyListener. Swing was designed to be used with Key Bindings.
Check out Key Bindings which contains a program to list the default bindings of each Swing component. It also give some example of how you might create your own ey Bindings. It also contains a link to the Swing tutorial on Key Bindings which explains the whole process in more detail

how to disable system keyboard handling in Java AWT Application?

How can i disable such keys and their combinations as, for example, Alt ; Alt + F4 and others in my Java AWT application?
E.g. my KeyboardListener should handle that keys as 'usual' keys and combinations without closing window or entering window menu.
One way is to create a program in "kiosk mode", something that requires more than Java to achieve (such as JNA or JNI). If you google this or search this site for this, you'll find out more about it. If I were using your code, though, I'd be very frustrated and perhaps angry, unless this were being run on a dedicated kiosk terminal.
Edit: Another option is as per this thread: java-full-screen-program-swing-tab-alt-f4:
window.setExtendedState(Frame.MAXIMIZED_BOTH); //maximise window
window.setUndecorated(true); //remove decorations e.g. x in top right
window.setAlwaysOnTop(true);
Edit 2: and this brute-force method: Remove the possibility of using Alt-F4 and Alt-TAB in Java GUI
Found this solution:
for Tab - use Frame.setFocusTraversalKeysEnabled(false);
for Alt - add keyEvent.consume(); at the end of each key
event handling code block
Then, to find out if Alt or Ctrl key is pressed - use keyEvent.isAltDown() and keyEvent.isControlDown() methods of keyPressed or keyReleased events.
Thanks, #Hovercraft , for your quick response!

Categories

Resources