-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcursesInput.h
More file actions
42 lines (36 loc) · 1.1 KB
/
cursesInput.h
File metadata and controls
42 lines (36 loc) · 1.1 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
#ifndef CURSES_INPUT_H
#define CURSES_INPUT_H
#include "input.h"
#include <ncurses.h>
class CursesInput: public Input{
public:
CursesInput(){
nodelay(stdscr, TRUE);
keypad(stdscr, TRUE);
}
void waitForAnyKey() override {
nodelay(stdscr, false);
getch();
nodelay(stdscr, false);
}
Action readAction() override {
int ch;
Action last = Action::NONE;
// Read all pending keys
while((ch = getch()) != ERR){
switch(ch){
case 'w': last = Action::UP; break;
case 's': last = Action::DOWN; break;
case 'a': last = Action::LEFT; break;
case 'd': last = Action::RIGHT; break;
case 'q': last = Action::INVALID; break;
case KEY_UP: last = Action::UP; break;
case KEY_DOWN: last = Action::DOWN; break;
case KEY_LEFT: last = Action::LEFT; break;
case KEY_RIGHT: last = Action::RIGHT; break;
}
}
return last; // NONE if nothing pressed
}
};
#endif