WVrock
WVrock

Reputation: 1765

How to detect backspace in a keyTypedEvent

I'm using Netbeans' bean form to create my GUI.
I've added a keyTyped event to a JTextArea and I want to detect if the typed key is a Backspace.

I'm using keyTyped event for other reasons so I cannot just use the keyPressed event.

This is the generated code (and my if check):

private void langArea1KeyTyped(java.awt.event.KeyEvent evt) {   

    if(evt.getChar()== backspace) //how can I make this check?   
}     

evt.getKeyCode() always returns 0 independent of the typed key.
evt.getKeyChar() simply deletes the last character printed when printed so I can't use System.out.print(evt.getKeyChar()) to detect its value and check for it.

How can I detect if the typed key is Backspace (or Delete)?

Upvotes: 2

Views: 14002

Answers (3)

Francisco P.
Francisco P.

Reputation: 1

I think is possible to check BACKSPACE in "KeyTyped" event. You can compare the hexadecimal value of the char against the hexadecimal value of the constant KeyEvent.VK_BACK_SPACE.

Example (only return true if the key typed is Backspace, otherwise return false and consumes the event):

public static boolean CheckBackspace(KeyEvent e) {
    if (!((Integer.toHexString(e.getKeyChar()).equals(Integer.toHexString(KeyEvent.VK_BACK_SPACE))))) {
        e.consume();
        return false;
    }
    return true;
}

Upvotes: 0

raymelfrancisco
raymelfrancisco

Reputation: 871

backspace and delete have the following constants:

VK_BACK_SPACE

VK_DELETE

Constants are listed here: Class KeyEvent

From docs.oracle.com

WARNING: Aside from those keys that are defined by the Java language (VK_ENTER, VK_BACK_SPACE, and VK_TAB), do not rely on the values of the VK_ constants. Sun reserves the right to change these values as needed to accomodate a wider range of keyboards in the future.

Aside from that, I suggest you use keyPressed() or keyReleased() since keyTyped() is only used for keys that produce characters. Keys backspace and delete do not produce characters.

Additional:

getKeyChar() works well with KEY_TYPED, as mentioned here:

The getKeyChar method always returns a valid Unicode character or CHAR_UNDEFINED. Character input is reported by KEY_TYPED events: KEY_PRESSED and KEY_RELEASED events are not necessarily associated with character input.

Therefore, the result of the getKeyChar method is guaranteed to be meaningful only for KEY_TYPED events.

You can take a look at these links:

The keyTyped() event is only used for keys that produce character input.

"Key typed" events are higher-level and generally do not depend on the platform or keyboard layout.

Upvotes: 2

Mr. Polywhirl
Mr. Polywhirl

Reputation: 48723

You cannot use keyTyped, you need to use either keyPressed or keyReleased to determine if backspace character has been typed. The keyTyped event only picks up non-escape characters (only characters that can be displayed).

The backspace character can be checked with KeyEvent.VK_BACK_SPACE.

Try adding this listener to the keyPressed event:

import java.awt.event.KeyEvent;

private void langArea1KeyPressed(KeyEvent evt) {   
    if (evt.getKeyCode() == KeyEvent.VK_BACK_SPACE) {
        // Do something...
    }
}

Additional Reading

You can check out the official Java tutorial on How to Write a Key Listener.

Upvotes: 2

Related Questions