Проверка на квадратные символы (KeyEvent) - PullRequest
0 голосов
/ 20 сентября 2019

Я пытаюсь добавить базовую пишущую машинку в свой проект, используя класс keyListener для обнаружения нажатий клавиш.Это работает по большей части, за исключением того, что некоторые клавиши (ctrl, shift, ect ...) заставят его печатать квадратный символ.Я бы хотел, чтобы эти квадратные символы были проигнорированы.Вот мой класс KeyListener:

public class Keyboard implements KeyListener{
KeyAction action; //an enum with 3 values: rtrn, del, and type
char key;

@Override
public void keyPressed(KeyEvent arg0) {}

@Override
public void keyReleased(KeyEvent e) {
    action = KeyAction.type; //tells my main class to type the key char on the screen
    key = e.getKeyChar();
    if (key == '\u0000') { //I read that the square symbols are /u0000 , but this test is not working
        action = null; //tells my main class to do nothing
        System.out.println("Bad character detected");
    }
    if (e.getKeyCode() == KeyEvent.VK_ENTER) action = KeyAction.rtrn; //tells my main class to indent on the screen
    if (e.getKeyCode() == KeyEvent.VK_BACK_SPACE) action = KeyAction.del; //tells my main class to delete the last character from the screen
}

@Override
public void keyTyped(KeyEvent e) {}

}

...