Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
Download

CSC112 Spring 2016 Examples

2370 views
1
/*
2
* File: keyStream.h
3
* Purpose: A small class which provides a nifty little tty aware input
4
* stream. This class only works with stdin.
5
* Also, it's probably a bad idea to touch cin if you're using this thing.
6
* You've been warned!
7
* Author: Robert Lowe
8
*/
9
#ifndef KEYSTREAM_H
10
#define KEYSTREAM_H
11
12
#include <iostream>
13
#include <termios.h>
14
#include <cstdint>
15
16
typedef std::uint32_t keycode;
17
18
19
class KeyStream : public std::istream
20
{
21
public:
22
//constructor & Destructor
23
KeyStream();
24
~KeyStream();
25
26
//macro modes
27
virtual void cookedMode();
28
virtual void rawMode();
29
virtual void cbreakMode();
30
31
//specific status flags
32
virtual bool echo();
33
virtual void echo(bool flag);
34
virtual bool canonical();
35
virtual void canonical(bool flag);
36
virtual bool signal();
37
virtual void signal(bool flag);
38
virtual bool special();
39
virtual void special(bool flag);
40
41
//extensions to the normal IO stuff
42
virtual keycode getKey();
43
virtual bool hasInput();
44
45
private:
46
bool _echo;
47
bool _canonical;
48
bool _signal;
49
bool _special;
50
struct termios _originalTermios;
51
52
void setTermiosFlags();
53
};
54
55
extern KeyStream kin; //the default keyboard stream kin!
56
57
/*
58
* Keycodes of special keys as reported by getKey
59
* ALT+key is detected by the ALT macro: ALT('a'), ALT('c') etc.
60
* The others are defined as constants
61
*/
62
#define ALT(k) (0x1B00 | (k))
63
#define UP 0x1B5B41
64
#define DOWN 0x1B5B42
65
#define RIGHT 0x1B5B43
66
#define LEFT 0x1B5B44
67
#define INSERT 0x1B5B327E
68
#define DELETE 0x1B5B337E
69
#define PAGEUP 0x1B5B357E
70
#define PAGEDOWN 0x1B5B367E
71
#define HOME 0x1B4F48
72
#define END 0x1B4F46
73
#define F1 0x1B4F50
74
#define F2 0x1B4F51
75
#define F3 0x1B4F52
76
#define F4 0x1B4F53
77
#define ESC 0x1B
78
#define ENTER 0x0A
79
#define CTRL_Q 0x11
80
#define CTRL_W 0x17
81
#define CTRL_E 0x05
82
#define CTRL_R 0x12
83
#define CTRL_T 0x14
84
#define CTRL_Y 0x19
85
#define CTRL_U 0x15
86
#define CTRL_I 0x09
87
#define CTRL_O 0x0F
88
#define CTRL_P 0x10
89
#define CTRL_LSQUARE 0x1B
90
#define CTRL_RSQUARE 0x1D
91
#define CTRL_A 0x01
92
#define CTRL_S 0x13
93
#define CTRL_D 0x04
94
#define CTRL_F 0x06
95
#define CTRL_G 0x07
96
#define CTRL_H 0x08
97
#define CTRL_J 0x0A
98
#define CTRL_K 0x0B
99
#define CTRL_L 0x0C
100
#define CTRL_SEMI 0x3B
101
#define CTRL_QUOTE 0x27
102
#define CTRL_ENTER 0x0A
103
#define CTRL_Z 0x1A
104
#define CTRL_X 0x18
105
#define CTRL_C 0x03
106
#define CTRL_V 0x16
107
#define CTRL_B 0x02
108
#define CTRL_N 0x0E
109
#define CTRL_M 0x0D
110
#define CTRL_COMMA 0x2C
111
#define CTRL_PERIOD 0x2E
112
#define CTRL_SLASH 0x1F
113
#endif
114
115