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.
Related
I'm having trouble understanding the difference between the 2 methods in the SWT SelectionListener. The javadoc is as follows:
void
org.eclipse.swt.events.SelectionListener.widgetSelected(SelectionEvent
e)
Sent when selection occurs in the control.
For example, selection occurs in a List when the user selects an item
or items with the keyboard or mouse. On some platforms, the event
occurs when a mouse button or key is pressed. On others, it happens
when the mouse or key is released. The exact key or mouse gesture that
causes this event is platform specific.
void
org.eclipse.swt.events.SelectionListener.widgetDefaultSelected(SelectionEvent
e)
Sent when default selection occurs in the control.
For example, on some platforms default selection occurs in a List when
the user double-clicks an item or types return in a Text. On some
platforms, the event occurs when a mouse button or key is pressed. On
others, it happens when the mouse or key is released. The exact key or
mouse gesture that causes this event is platform specific.
It sounds to me like widgetSelected() is called when the user selects a widget in any way. The widgetDefaultSelected() is called when the user is finished interacting with the widget. For a Text widget, that would be pressing Enter; for a List, that would be double clicking on a list item; and for a Date, that would be pressing Enter.
Is this understanding correct?
Your general understanding is correct, though the term 'finished' might not be 100% accurate in all situations.
Widgets that send (default) selection events document the particular details in the JavaDoc of their respective addSelectionListener method. There you can read if and when widgetSelected and/or widgetDefaultSelected is sent.
If you look into the Link::addSelectionListener JavaDoc for example, you will see that widgetDefaultSelected() is never called.
I'm trying to make an application which uses the tab key to do something. I've implemented the KeyListener interface and I'm able to program all the other keys. But when I press the Tab key and in the keyPressed() method I put this:
System.out.println(ke.getKeyCode());
I get no output. The 'Tab' key is the only which gives no output. All the other keys have their relative keycode printed. Why is this?
Use setFocusTraversalKeysEnabled(false) on the control that you listen on (JTextField?).
EDIT:
The Tab key is usually used to switch input focus between UI controls, for easier keyboard input without the need for a mouse. When a UI control receives a key press event it first tries to "consume" it on its own, and if the key is a special key (Tab, Shift-Tab, etc.) then the control consumes the event and doesn't propagate it further. On the other hand, if the key is not a "special" key it propagates the event to attached listeners (if any). Calling setFocusTraversalKeysEnabled(false) disables this built-in behavior for the control (but in this case just for FocusTraversal, i.e. Tab and Shift+Tab), so that event gets propagated to listeners.
I have a Java Swing application in the NetBeans IDE.
I made a form and attached a KeyListener to my various controls as such:
jButton1.addKeyListener(new java.awt.event.KeyAdapter() {
public void keyTyped(java.awt.event.KeyEvent evt) {
keyTypedEvent(evt);
}
});
and keyTypedEvent is defined as such:
private void keyTypedEvent(java.awt.event.KeyEvent evt)
{
System.out.println(evt);
appendDisplay(String.valueOf(evt.getKeyChar()));
}
I added a println to the evt to see what happens and to verify that my keylistener does work.
When I build and run my application, I realized that the output always seems to have a keycode = 0
To verify this, I had changed my println to be evt.getKeyCode() and it is always returning 0.
I could be completely misinterpreting what KeyCode does, but I thought that it would coorespond with the values in Oracle's documentation here:
http://docs.oracle.com/javase/7/docs/api/constant-values.html#java.awt.event.KeyEvent.VK_ESCAPE
For instance, VK_ESCAPE has a value of 27.
The keyTyped() event is only used for keys that produce character input. If you want to know when any key is pressed or released, you need to implement keyPressed() or keyReleased().
From the KeyEvent API:
"Key typed" events are higher-level and generally do not depend on the
platform or keyboard layout. They are generated when a Unicode
character is entered, and are the preferred way to find out about
character input....
For key pressed and key released events, the getKeyCode method returns
the event's keyCode. For key typed events, the getKeyCode method
always returns VK_UNDEFINED.
all suggestion about KeyListener for JButton are wrong, meaning Button1.addKeyListener(new java.awt.event.KeyAdapter() {
these events are implemented and correctly in JButtons API, use SwingAction or add ActionListener for listening Mouse and Key Event from/to JButton
basically everything is described in Oracle tutorial about How to Use Buttons, Check Boxes, and Radio Buttons
It very depends on key that has been pressed. Probably you need KeyListener with keyPressed method override, because keyTyped not triggered on non-printable characters.
Look at the difference between keyTyped and keyPressed here:
KeyListener, keyPressed versus keyTyped
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.
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());
}