Using the Keyboard "ContextMenu" button in Java - java

In the application I'm working on I'd like to listen for when the keyboard's context menu (right click) button is pressed.
Just to be clear, I'm talking about the button between Alt Gr and Ctrl on the right of the spacebar. I realise it is not on all keyboards (older, mac's etc), but I know that all of the keyboards which will be using this application will have the button.
I'd like to know if there is a simple KeyEvent or any other method for knowing when it has been pressed.
Thanks,
Dave

To echo #Thijs Wouters' answer, (a) this is always a great way to figure out what key codes are associated with what keys in Java, and (b), this key code for the context menu, 525, is 20D in hexadecimal, and is defined in Java (since 1.5) as
KeyEvent.VK_CONTEXT_MENU
for ease of code reading.

You can check the key code when a key is pressed. The key code of the context menu key is 525.
You can check this for yourself:
public void keyPressed(KeyEvent e) {
System.out.println(e.getKeyCode());
}

Related

Keycode event for BACKSPACE in JavaFx

I am doing a project in JavaFX, using Java 8 and Gluon scenebuilder. I want to detect when backspace is pressed inside a TextField. This is the code I am using:
public void keyPressed(KeyEvent kEvent) {
if (kEvent.getCode() == KeyCode.BACK_SPACE) {
System.out.println("Backspace pressed");
}
This code is inside the controller file, called FXMLDocumentController, which controls the GUI xml file FXMLDocument. You can see from the image below the function is called whenever a key is typed inside TextField. This works with all the letters/numbers, but not with backspace.
Gluon scene builder settings
Theoretically it should work, but it doesn't.
How can I manage typing of the backspace button?
Edit:
Notice that putting this exact function on the root of the elements, on the "Window itself" (which is the AnchorPane) works. The problem is with reading the pressing of the backspace inside the TextField. You can see in the image below where I've put the function:
On the window backspace's detecting works
You should use your keyPressed method in either on key pressed or on key released.
In the Docs, it states that:
No key typed events are generated for keys that don't generate Unicode
characters (e.g., action keys, modifier keys, etc.).
Backspace is consider an Action Key.
In the screenshot of your SceneBuilder I can see you are referencing the keyPressed(KeyEvent kEvent) controller method with On Key Typed not On Key Pressed.
For events of KeyEvent.KEY_TYPED the value of getCode() is always KeyCode.UNDEFINED.
Edit: I came back to this answer and reread your question. I want to clarify something but what I said above is still correct.
In your edit you mention the "exact" same setup works with the AnchorPane but when looking at your screenshot there are some differences. For your AnchorPane you have the controller method referenced for all three types: On Key Pressed, On Key Released, and On Key Typed.
This means that the method should be called up to 3 times for each key stroke (when the events reach the AnchorPane). Once when pressed, once for typed (if the key is capable of sending typed events - see the answer by Sedrick for clarification), and then once for released. Because of this, the method will work for the pressed and released events, but it won't work for the typed events.
In other words, the reason your code works for the AnchorPane but not the TextField is because you configured it correctly for the AnchorPane but not the TextField.

How to disable windows beep sound on delete key press

I have a java swing application that beeps everytime I delete a row in a jtable.
Anyone have any ideas how I can prevent this from happening or at least what is causing this?
From here: https://www.java.net/node/687490
On Windows, pressing the Alt key moves the keyboard focus to the
window menu in the top left corner. This focus is invisible, and it
even does not send a "focus lost" event. But if you press and release
Alt and then press the up or down arrow, the window menu will show up.
Luckily, it's possible to prevent this functionality of the Alt key:
addKeyListener( new KeyAdapter() {
public void keyPressed(KeyEvent e) {
System.out.println("keyPressed code=" + e.getKeyCode());
e.consume();
} } );
e.consume( ) prevents the event to be processed by usual rules. This
prevents the Alt key from moving the focus to the window menu, and
further alphanumeric keys continue to function as usual. You may want
to check the event code and consume only Alt keys, if something else
stops working.

Detecting if a key (CTRL) is currently pressed without a KeyEvent in Java

I need to be able to see if the CTRL key is currently being pressed during the processing of a mouse event. I have tried to use the KeyListener, but trying to use the mouse event and key event together is proving to be an issue because of focus issues.
What I am essentially trying to accomplish is selecting multiple objects using the CTRL key like in Windows.
It would be much easier if, while in my mouse event, I could just check the status of the CTRL key...
Can you do that in Java?
Thanks.
Use getModifiers() to detect the key pressed..
eg:
if ((event.getModifiers() & ActionEvent.CTRL_MASK) ==ActionEvent.CTRL_MASK) {
System.out.println("CTRL KEY PRESSED");
}
MouseEvent extends from InputEvent, and I think that you can still get the modifiers from this object via getModifiers() to see if a ctrl key has been pressed. I've not tested this yet though.

Keyboard shortcuts and action listeners

Wondering how to link a keyboard key to a JButton in netbeans, Downloaded KeyEventDemo program from oracle that lets you press a key and it tells you the keycode, Now im wondering how to implement that, working on a calculator project and I already have the ability to click on any button and have it either +,-,/,* or enter so now I am wondering how to link the 2 together, essentially I'm trying to figure out how to check which key is being pressed in a method, and then run a method depending on what they pressed! thanks for any info!
See How to Use Key Bindings. Basically you bind a KeyStroke to an Action. The tutorial also has a section on How to Use Actions.

Shortcut key for jButton without using alt key

In SWT you can give any button a shortcut key simply by adding & in front of the letter in the button label. For example, if my button label is &Play, I can activate the button by hitting letter p on the keyboard.
In Swing, you can add a shortcut key using the mnemonic property. However, you need to hit alt+p to activate the button. This is really most appropriate for menu shortcuts. I want to activate the button with a letter press and no alt modifier.
I've seen this post on how to do it, but it seems absurdly complicated. Is there an easier way to do this?
http://linuxjavaprogrammer.blogspot.com/2008/01/java-swing-jbutton-keyboard-shortcuts.html
Update: After #camickr suggestion, I ended up using this code. I couldn't find any clear and simple example online, so hopefully this will help people out.
// play is a jButton but can be any component in the window
play.getInputMap(play.WHEN_IN_FOCUSED_WINDOW).put(KeyStroke.getKeyStroke(KeyEvent.VK_P, 0), "play");
play.getActionMap().put("play", new AbstractAction() {
public void actionPerformed(ActionEvent e) {
playActionPerformed(e); // some function
}
});
Yes, Swing was designed to use Key Bindings. So instead of adding an ActionListener to the button you add an Action. Then that Action can be shared by the button or a menu item. You can also assign any number of KeyStrokes to invoke the Action by using the KeyBindings. The tutorial also has a section on Actions which explains why using an Action is beneficial.
JComponent has a registerKeyboardAction(...) method which basically does the InputMap/ActionMap bindings for you, but it also has to wrap the ActionListener in a wrapper Action so its preferrable for you to do you own bindings.
Further to camickr's answer, I am now using a little utility function like this:
public static void clickOnKey(
final AbstractButton button, String actionName, int key )
{
button.getInputMap( JButton.WHEN_IN_FOCUSED_WINDOW )
.put( KeyStroke.getKeyStroke( key, 0 ), actionName );
button.getActionMap().put( actionName, new AbstractAction()
{
#Override
public void actionPerformed( ActionEvent e )
{
button.doClick();
}
} );
}
Now to set the keyboard shortcut for a button I just do:
clickOnKey( playButton, "play", KeyEvent.VK_P );
I had a similar problem with a dynamically constructed (based on data input) form and just ended up attaching a keyListener action to the buttons. On form construction I parse the Component tree for the buttons and attach the listener. The listener than also parses the tree and matches the keypress with the appropriate button (via the text in the button), since I have no idea which one will have focus at any given time, and fires the button doClick... It's ugly, feels hackish, and has got to be a bit processor intensive, but it allows the flexibility I need for this particular dynamic form...

Categories

Resources