Java delete key compare - java

Hi I ve got one problem:
I write a little calculator in Java Swing. I ve got buttons bind with KeyBindings
and then in actionPerformed I check which key was pressed, it's something like that:
if(event.getActionCommand().equals("\b")) {
}
My question is: is there any way to compare delete button, like backspace button is compared above?

You can use KeyEvent constants to check whether delete key was pressed, e.g.:
if (event.getExtendedKeyCode() == KeyEvent.VK_DELETE) {
//do something
}
Here's javadoc for KeyEvent class.

Related

How do I run a function on a specific key press in JavaFX?

I have a program in javafx that is running and I want to call a function inside that program when a specific key is pressed on the keyboard (for example, the "a" key). I tried using an event handler on my scene but KEY_PRESSED seems to go off when any key is pressed, unless I am using it wrong. KEY_TYPED seems like it might suit my needs, but I've only found examples of that one in relation to text boxes, which is not what I'm looking for. Does anyone know how to do this, or have a good resource I can consult for something like this
Just check the code of the key that was pressed:
scene.setOnKeyPressed(e -> {
if (e.getCode() == KeyCode.A) {
System.out.println("The 'A' key was pressed");
}
});
Use an event filter and whatever keyevent you need, here I use ANY:
scene.addEventFilter(KeyEvent.ANY, keyEvent -> {
System.out.println(keyEvent);
});

Processing/Java - Key pressed/Key released

I'm building an application on Processing using NyARToolkit, but my question is not directly about NyArToolkit but about a key released() method.
The thing is, I show a card and then I can do a few things if I press different keys. I press "X" and it shows one thing, I press "Y" and it shows another thing. The problem is, it shows the info from the last key pressed all the time.
If I change my AR card, it will immediatly show the info from the last key pressed. I would like to do something to release the key, something like: just show while i'm pressing the key, or have an "ESC" to stop showing everything.
I've been reading about the keyreleased() method but I didn't figured it yet out to put it to work.
By the way my method is like this:
if(key == "c") then
else if(key =="d") then...
How about a boolean keeping track wether you've pushed something
private boolean buttonIsPressed = false;
and inside your keyPressed
buttonIsPressed = true;
and inside your keyReleased
buttonIsPressed = false;
This way you can keep track wether it's pressed or not, using a String you can also keep track which button is being held by initializing it in your keyPressed and making it null in your keyReleased.
If I missunderstood your question let me know.
EDIT:
Reading your edit, you're making quite a big mistake there, make sure to use .equals to compare strings so if(key.equals("c"))
Again, your question is not quite clear for me, so if I'm wrong, excuse me.

libGDX scene2d TextFieldListener does not receive DELETE key on Android

I cannot get the android "delete" key to register in my TextField (scene2d ui element in libgdx) listener. Here is my code to define the text field:
nameTextfield = new TextField("", skin);
nameTextfield.setMessageText("Some Text");
uiStage.addActor(nameTextfield);
I tried this listener just to decode the keycode for the DELETE key:
nameTextfield.setTextFieldListener(new TextFieldListener() {
public void keyTyped (TextField textField, char key) {
textField.setText(String.valueOf(Integer.valueOf(key)));
}
});
Although it gives code for almost for all buttons, it doesn't even react on DELETE button.
I tested this on a Nexus 7.
From the TextField.java source it looks like the "DELETE" (and "BACKSPACE", and "TAB" and a couple other keys ) are handled specially by the TextField. These keys are never forwarded to any listener.
The built-in handler should do "the right thing" (trimming characters off the string contents).
Is delete not behaving correctly for your case in some way that led you to try to decode it?
Well the DELETE button should be implemented differently.
I suggest trying to verify if the pressed key is the DELETE button. If it is, you just do textField.getText(), trim the last letter off it, and set the new text with setText.
I'm sure there's a much more elegant way to do this, but it's the only workaround I can think of. After all, DELETE isn't really a char which you can throw inside setText. Is it? :/
LATER EDIT:
Print the key variable inside your listener, put a breakpoint there, and see what value is assigned to it.
Then also print (or check the javadoc) KeyEvent.KEYCODE_DEL (documentation here) to see what value this one takes.
the best way to solve this is the following:
You will need to listen to inputs from another listner, for example the same screen.
MainMenuScreen implements Screen, InputProcessor
then you will need to create a multiplexer to let inputs been listen from both the stage and the listner.
multiplexer = new InputMultiplexer();
then add the two listners:
multiplexer.addProcessor(this);
multiplexer.addProcessor(stage);
Now you will have to simply delete the field from here:
#Override
public boolean keyDown(int keycode) {
Gdx.app.log("Debug:", "keydown : "+keycode);
//DO SMOTHING LIKE
// if(keycode==...) deleteTextField();
return true;
}
Let me know if you have questions about this solution.
Worked great for me.
This issue has been solved by the latest nightly version of libgdx, the issue is known and discussed in the following link:
nexus button

keycode for ctrl

For multiple selection in a Jtree,I am using multiple selection mode in it.It works.But I want to know when i am making multiple selection exactly in this tree to do this i wrote a very simple keycontroller class that implements KeyListener, but i wanna check whether CTRL is pressed or not to do so i am using this code but it seems to be not working :
kc.getKeyCode() == KeyEvent.CTRL_DOWN_MASK ;
what is the keyCode for ctrl ? Or am i doing something wrong ?
As CTRL is a key mask, there is no character for the CTRL key alone.
However, according to KeyEvent documentation, there is always a vaild key code that is sent when either a key is pressed or released. in that case, it should be KeyEvent.CHAR_UNDEFINED and getModifiersEx() should return true for the CTRL key. Notice that, for it to work, you have to register a KeyListener (specially handle for both keyPressed() and keyReleased()).
The key code for Ctrl is KeyCode.VK_CONTROL. In order to find if Ctrl is held you can do this:
if ((event.getModifiers() & ActionEvent.CTRL_MASK) ==ActionEvent.CTRL_MASK) {
System.out.println("CTRL KEY PRESSED");
}
Which is using the java.awt.event.ActionEventinstead of the java.awt.event.KeyEvent. So the code for Ctrl in ActionEvent is CTRL_MASK.
Hope this helps.

All keys to a list, for example VK_A..Z for user to select

I am making a program that allows user to custom keyboard shortcuts, for this i need the available keys to be displayed, what is the best way to achieve this in java swing?
KeyEvent.class.getDeclaredFields()
I am intrested in dynamic example of below,
keysLST.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "CTRL", "ALT", "SHIFT" }));
one way i know is to get all from the keyevent class but i am not sure how to integrate it to the list. any help would be appreciated.
Thanks
Instead of picking from a list, add a KeyListener to any component (a JTextField works) and record the key code as the user presses the key. You can also record modifiers (ctrl,alt,shift) this way.
public void keyPressed(KeyEvent e)
{
int keyTheUserJustPressed = e.getKeyCode();
// then use for ctrl/alt/shift
e.getModifiersEx();
// or use
e.isAltDown();
e.isShiftDown();
}

Categories

Resources