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