KeyEventListener recognising UP and Delete key simultaneously in Java [closed]

StackOverflow https://stackoverflow.com/questions/17318015

  •  01-06-2022
  •  | 
  •  

سؤال

I am writing a command line application in Java. Rather than using System.console(), I am writing a custom console, that will run within a JFrame. This is primarily to circumvent the fact that System.console() returns null from within an IDE like eclipse.

I only require the console to have very basic features:

  • Backspace and Delete keys must remove the character behind or in front of the Caret
  • Up and Down keys must cycle through previously typed commands.

I have achieved this by using a KeyEventListener, and using a switch statement to select the appropriate action for the special keys (Backspace, Delete, Up, Down, Left, Right etc.) and otherwise printing the pressed key to the console.

Everything works, except when the Delete key is pressed, both the Delete and Up statements within the switch statement are executed. This implies that pressing the delete key fires both a Delete event, and then an Up event separately. When pressing the Delete key, the character immediately in front of the Caret is deleted, and then the previously entered command is displayed.

public void keyPressed(KeyEvent e) {
        switch (e.getKeyCode()){
            case KeyEvent.VK_ENTER: 
                String line = getLine();
                previousCommands.add(line.substring(1));
                output.append("\n" + line);
                commandPosition = 0;
                setFinished(true);
                break;

            case KeyEvent.VK_BACK_SPACE:
                if(input.getCaretPosition() <= 1){
                    break;
                }
                int caretPosition = input.getCaretPosition();
                String text = input.getText().
                        substring(0, input.getCaretPosition() - 1) +
                        input.getText().
                        substring(input.getCaretPosition());
                input.setText(text);
                input.setCaretPosition(caretPosition -1);
                break;

            case KeyEvent.VK_DELETE:
                if(input.getCaretPosition() == input.getText().length()){
                    break;
                }
                int otherCaretPosition = input.getCaretPosition();
                String otherText = input.getText().
                        substring(0, otherCaretPosition) +
                        input.getText().
                        substring(otherCaretPosition + 1);
                input.setText(otherText);
                input.setCaretPosition(otherCaretPosition);
                            break;

            case KeyEvent.VK_UP:
                if(previousCommand() != null){
                    setLine(">" + previousCommand());
                }
                break;

            case KeyEvent.VK_DOWN:
                if(nextCommand() != null){
                    setLine(">" + nextCommand());
                }
                break;

            case KeyEvent.VK_LEFT:
                if(input.getCaretPosition() <= 1){
                    input.setCaretPosition(1);
                    break;
                }
                input.setCaretPosition(input.getCaretPosition() - 1);
                break;

            case KeyEvent.VK_RIGHT:
                if(input.getCaretPosition() == input.getText().length()){
                    input.setCaretPosition(input.getText().length());
                    break;
                }
                input.setCaretPosition(input.getCaretPosition() + 1);
                break;

            default:
                break;
        }

    }

I have omitted the remainder of the class as I don't think it is necessary. I am intending to move away from the KeyEventListener and using KeyBindings instead, and I expect that this will solve the problem. However I still would like to know what might cause this.

EDIT: There was a mistake in the code. It works now.

هل كانت مفيدة؟

المحلول

You skipped the break before line case KeyEvent.VK_UP:

مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top