CSC112 Spring 2016 Examples
/*1* File: keyStream.h2* Purpose: A small class which provides a nifty little tty aware input3* stream. This class only works with stdin.4* Also, it's probably a bad idea to touch cin if you're using this thing.5* You've been warned!6* Author: Robert Lowe7*/8#ifndef KEYSTREAM_H9#define KEYSTREAM_H1011#include <iostream>12#include <termios.h>13#include <cstdint>1415typedef std::uint32_t keycode;161718class KeyStream : public std::istream19{20public:21//constructor & Destructor22KeyStream();23~KeyStream();2425//macro modes26virtual void cookedMode();27virtual void rawMode();28virtual void cbreakMode();2930//specific status flags31virtual bool echo();32virtual void echo(bool flag);33virtual bool canonical();34virtual void canonical(bool flag);35virtual bool signal();36virtual void signal(bool flag);37virtual bool special();38virtual void special(bool flag);3940//extensions to the normal IO stuff41virtual keycode getKey();42virtual bool hasInput();4344private:45bool _echo;46bool _canonical;47bool _signal;48bool _special;49struct termios _originalTermios;5051void setTermiosFlags();52};5354extern KeyStream kin; //the default keyboard stream kin!5556/*57* Keycodes of special keys as reported by getKey58* ALT+key is detected by the ALT macro: ALT('a'), ALT('c') etc.59* The others are defined as constants60*/61#define ALT(k) (0x1B00 | (k))62#define UP 0x1B5B4163#define DOWN 0x1B5B4264#define RIGHT 0x1B5B4365#define LEFT 0x1B5B4466#define INSERT 0x1B5B327E67#define DELETE 0x1B5B337E68#define PAGEUP 0x1B5B357E69#define PAGEDOWN 0x1B5B367E70#define HOME 0x1B4F4871#define END 0x1B4F4672#define F1 0x1B4F5073#define F2 0x1B4F5174#define F3 0x1B4F5275#define F4 0x1B4F5376#define ESC 0x1B77#define ENTER 0x0A78#define CTRL_Q 0x1179#define CTRL_W 0x1780#define CTRL_E 0x0581#define CTRL_R 0x1282#define CTRL_T 0x1483#define CTRL_Y 0x1984#define CTRL_U 0x1585#define CTRL_I 0x0986#define CTRL_O 0x0F87#define CTRL_P 0x1088#define CTRL_LSQUARE 0x1B89#define CTRL_RSQUARE 0x1D90#define CTRL_A 0x0191#define CTRL_S 0x1392#define CTRL_D 0x0493#define CTRL_F 0x0694#define CTRL_G 0x0795#define CTRL_H 0x0896#define CTRL_J 0x0A97#define CTRL_K 0x0B98#define CTRL_L 0x0C99#define CTRL_SEMI 0x3B100#define CTRL_QUOTE 0x27101#define CTRL_ENTER 0x0A102#define CTRL_Z 0x1A103#define CTRL_X 0x18104#define CTRL_C 0x03105#define CTRL_V 0x16106#define CTRL_B 0x02107#define CTRL_N 0x0E108#define CTRL_M 0x0D109#define CTRL_COMMA 0x2C110#define CTRL_PERIOD 0x2E111#define CTRL_SLASH 0x1F112#endif113114115