How to detect if backspace is pressed? - java

mainNotes.setOnKeyListener(new View.OnKeyListener() {
public boolean onKey(View v, int keyCode, KeyEvent event) {
if ((keyCode != KeyEvent.KEYCODE_SLASH) && (titleMod = true) && (keyCode != 46) && (keyCode != KeyEvent.KEYCODE_SLASH) && (keyCode != KeyEvent.KEYCODE_ENTER)) {
Toast.makeText(MainActivity.this, "not a slash", Toast.LENGTH_SHORT).show();
titleEnd += 1;
slashCount = 1;
} else if ((keyCode == KeyEvent.KEYCODE_DEL) && (titleMod = true)) {
Toast.makeText(MainActivity.this, "deleted", Toast.LENGTH_SHORT).show();
titleEnd -= 1;}
The code does not recognize that delete is pressed. It works for all other keypresses like slash, other text and so on. It doesn't even register that delete is pressed?

KeyEvent.KEYCODE_DEL corresponds to your backspace key. Standard Android keyboards don't actually have a delete key at all, so you need to be pressing backspace when testing on emulator via keyboard.
To make sure that soft key events are detected, avoid using the KeyListener interface because it's unlikely to be recognized by a software keyboard or emulator. From the official documentation:
Key presses on soft input methods are not required to trigger the
methods in this listener, and are in fact discouraged to do so. The
default android keyboard will not trigger these for any key to any
application targetting Jelly Bean or later, and will only deliver it
for some key presses to applications targetting Ice Cream Sandwich or
earlier.
Instead, try using KeyboardView.OnKeyboardActionListener.
Sample code may be found here.

Related

Detect when a user cancels typing

i would like to know if its possible to detect whenever a user presses the back button when in the EditText field. I've tried using the KeyEvent.BACK and the KeyEvent.CANCELED flags in my setOnKeyListener like so:
textView.setOnKeyListener(new View.OnKeyListener() {
#Override
public boolean onKey(View v, int keyCode, KeyEvent event) {
if (keyCode == KeyEvent.KEYCODE_ENTER && event.getAction() == KeyEvent.ACTION_DOWN || keyCode == KeyEvent.FLAG_CANCELED || keyCode == KeyEvent.BACK) {
Log.e("TextView", String.valueOf(thiz.getText()));
}
return false;
}
});
but this didn't work. If somebody could help me i would appreciate that :)
As per my understanding if you want to do something("I would like to log the text of the edittext whenever an user closes the keyboard") when keyboard is hidden, you can use ViewTreeObserver like this example
As I understand that you want, for some reason, to log text from EditText when the user closes the virtual keyboard. If yes, check this question, possibly this is what you searching for. Especially you may take look at this answer

JavaFX KeyCode for constant MINUS

I'm using Eclipse. I developed simple JavaFX calculator app, everything is working but keyevent for numpad MINUS and PLUS. Which is funny because every other numpad key is ok. Am I missing something?
public void onKeyPress(KeyEvent event) {
KeyCode keyCode = event.getCode();
Just a piece of my code:
case PLUS:
if (operation.getOperand() == 'x') {
operation.add(Double.parseDouble("" + display.getText()), display);
decimal.setDisable(false);
} else {
result.fire();
operation.add(Double.parseDouble("" + display.getText()), display);
decimal.setDisable(false);
}
break;
Just now i thought of debugging keycodes themselves while running the app. Added a simple console output of a keycode that is currently pressed, found out that constant for numpad plus and minus in not PLUS and MINUS but ADD and SUBTRACT.
Thanks to all who took the time to answer me.

Recognising Escape Key

I'm currently working on a practice program that works with various keyboard inputs so I can understand how they work in Android apps with Java. But I am struggling to get the ESC key to be recognised. The code below doesn't work. Has anyone ever managed to get the ESC key to work in an app? Is it actually possible?
editText.setOnEditorActionListener(new OnEditorActionListener() {
#Override
public boolean onEditorAction(TextView textView, int i, KeyEvent keyEvent) {
boolean handled = false;
if (keyEvent.getKeyCode() == KeyEvent.KEYCODE_ESCAPE) {
textView.setText("Escapekey pressed");
handled = true;
}
return handled;
}
});
To summarize, I want the program to recognise the ESC key.
It seems that Android Emulator will not forward this key to the application, just like e.g. the function keys. Receiving these key events on a real device from a real keyboard works fine for me.

Capturing keyboard combinations globally on the entire application to show hidden JDialog in Java

I'm going to make a hidden dialog in my application that get visible with a keyboard combination (e.g. Five sequent Ctrl+Shift+i).
How Can I capture keyboard combination strokes globally on the entire application?
Thanks
FullScreenTest is an example that shows how to use Action and Key Bindings in this context. You could substitute KeyEvent.VK_I and the relevant KeyStroke modifiers. Your action listener can keep count of how often it's been triggered.
I solved it by defining a DispatcherListener:
class DispatcherListener implements KeyEventDispatcher{
private int level=0;
public boolean dispatchKeyEvent(KeyEvent e) {
if(e.getID() == KeyEvent.KEY_RELEASED){
if(e.isControlDown() && e.isShiftDown()){
if(this.level==0 && e.getKeyCode()==KeyEvent.VK_S){
level++;
}else if(this.level==1 && e.getKeyCode()==KeyEvent.VK_H){
level++;
}else if(this.level==2 && e.getKeyCode()==KeyEvent.VK_O){
level++;
}else if(this.level==3 && e.getKeyCode()==KeyEvent.VK_W){
level=0;
this.showHiddenWindow((JFrame)SwingUtilities.getRoot(e.getComponent()));
}else{
level=0;
}
//System.out.println( "level: " + level );
}
}
return false;
}
and used it as this:
KeyboardFocusManager manager = KeyboardFocusManager.getCurrentKeyboardFocusManager();
manager.addKeyEventDispatcher( new DispatcherListener());
Thank you all

Need help with pressing multiple keys in java

I'm trying to code a game similar to Mario in Java and am having problems with making the dude jump while he is running. What the below code ends up doing is, as the user is holding down right and presses the jump button, Mario basically stops running and jumps, so its like the user has let go of right and pressed jump.
public void keyPressed(KeyEvent e) {
// here I keep track of what buttons the user pressed
int keyCode = e.getKeyCode();
if(keyCode == 37)
pressedKeys[0] = true;
else if(keyCode == 39)
pressedKeys[1] = true;
else if(keyCode == 68)
pressedKeys[2] = true;
// after I see what the user has pressed an action is carried out
Thread t = new Thread(this);
t.start();
}
public void performAction()
{
// depending on what the user has pressed a certain action is performed
if(pressedKeys[2]==true)
{
// changes the coordinates of the character itself
// done in another thread so the background can continue
// moving as the user holds down a direction
Thread t = new Thread(mcControl);
t.start();
}
if(pressedKeys[0]==true)
{
changeSprite();
bg.moveImageForward();
}
if(pressedKeys[1]==true)
{
changeSprite();
bg.moveImageBackward();
}
}
public void run() {
performAction();
}
if(keyCode == 37)
Not related to your problem, but NEVER use code like that. Most people don't know what that magic number means.
The better way to do this is to use:
if(keyCode == KeyEvent.VK_???)
Now your code is self documenting.
You want to take a look at How to Write a Key Listener. The KeyEvent object has several methods for learning what modifier keys and mouse buttons were pressed at the time the event was generated.

Categories

Resources