tapio / rlutil

C and C++ utilities for cross-platform console roguelike game creation.
http://tapio.github.com/rlutil/
229 stars 42 forks source link

keyboard artefacts ^[[B ^[[D ^[[C #54

Closed SafinaM closed 5 years ago

SafinaM commented 5 years ago

Hello! I wrote very simple app and use keyboard input. ` if (kbhit()) { ch = rlutil::getkey();

    if (ch == 'q')
        break;
    switch (ch) {
        case 'w':
        case rlutil::KEY_UP:
        case rlutil::KEY_SPACE:
            if (board.allowRotate(*figure)) {
                painter.drawFigure(*figure, false);
                figure->setNextPoints();
            }
            break;
        case 'a':
        case rlutil::KEY_LEFT:
            if (board.allowMove(Direction::Left, *figure)) {
                painter.drawFigure(*figure, false);
                figure->move(Direction::Left);
            }
            break;
        case 'd':
        case rlutil::KEY_RIGHT:
            if (board.allowMove(Direction::Right, *figure)) {
                painter.drawFigure(*figure, false);
                figure->move(Direction::Right);
            }
            break;
        case 's':
        case rlutil::KEY_DOWN:
            if (board.allowMove(Direction::Down, *figure)) {
                painter.drawFigure(*figure, false);
                figure->move(Direction::Down);
            }
            break;
        default:
            break;
    }
}

But in terminal I see such keyboard artefacts ^[[B ^[[D ^[[C. When I used ncurses library I could switch off them by noecho() method. But here I tried to clean screen by rlutil::cls(), tried to play with such flags. newt.c_lflag - is always is 0. newt = oldt; newt.c_lflag &= ~(ICANON | ECHO); tcsetattr(STDIN_FILENO, TCSANOW, &newt);` Also I tried to use std::cout.flush() after each key-pressing. Nothing helps. Maybe you can help me, where I did mistake. Please.

SafinaM commented 5 years ago

`void noecho() {

static struct termios oldt, newt;
int cnt = 0;
tcgetattr(STDIN_FILENO, &oldt);
newt = oldt;
newt.c_lflag    &= ~(ICANON | ECHO);
tcsetattr(STDIN_FILENO, TCSANOW, &newt);

} ` I have used such call befor cycle and erase all keyboard garbage. Ok and thanks)