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* Revision: $Revision: 1.1 $9* Change Log10* $Log: keystream.h,v $11* Revision 1.1 2016/03/17 15:50:30 pngwen12* Initial revision13*14*/15#ifndef KEYSTREAM_H16#define KEYSTREAM_H1718#include <iostream>19#include <termios.h>20#include <cstdint>2122typedef std::uint32_t keycode;232425class KeyStream : public std::istream26{27public:28//constructor & Destructor29KeyStream();30~KeyStream();3132//macro modes33virtual void cookedMode();34virtual void rawMode();35virtual void cbreakMode();3637//specific status flags38virtual bool echo();39virtual void echo(bool flag);40virtual bool canonical();41virtual void canonical(bool flag);42virtual bool signal();43virtual void signal(bool flag);44virtual bool special();45virtual void special(bool flag);4647//extensions to the normal IO stuff48virtual keycode getKey();49virtual bool hasInput();5051private:52bool _echo;53bool _canonical;54bool _signal;55bool _special;56struct termios _originalTermios;5758void setTermiosFlags();59};6061extern KeyStream kin; //the default keyboard stream kin!6263/*64* Keycodes of special keys as reported by getKey65* ALT+key is detected by the ALT macro: ALT('a'), ALT('c') etc.66* The others are defined as constants67*/68#define ALT(k) (0x1B00 | (k))69#define UP 0x1B5B4170#define DOWN 0x1B5B4271#define RIGHT 0x1B5B4372#define LEFT 0x1B5B4473#define INSERT 0x1B5B327E74#define DELETE 0x1B5B337E75#define PAGEUP 0x1B5B357E76#define PAGEDOWN 0x1B5B367E77#define HOME 0x1B4F4878#define END 0x1B4F4679#define F1 0x1B4F5080#define F2 0x1B4F5181#define F3 0x1B4F5282#define F4 0x1B4F5383#define ESC 0x1B84#define ENTER 0x0A85#define CTRL_Q 0x1186#define CTRL_W 0x1787#define CTRL_E 0x0588#define CTRL_R 0x1289#define CTRL_T 0x1490#define CTRL_Y 0x1991#define CTRL_U 0x1592#define CTRL_I 0x0993#define CTRL_O 0x0F94#define CTRL_P 0x1095#define CTRL_LSQUARE 0x1B96#define CTRL_RSQUARE 0x1D97#define CTRL_A 0x0198#define CTRL_S 0x1399#define CTRL_D 0x04100#define CTRL_F 0x06101#define CTRL_G 0x07102#define CTRL_H 0x08103#define CTRL_J 0x0A104#define CTRL_K 0x0B105#define CTRL_L 0x0C106#define CTRL_SEMI 0x3B107#define CTRL_QUOTE 0x27108#define CTRL_ENTER 0x0A109#define CTRL_Z 0x1A110#define CTRL_X 0x18111#define CTRL_C 0x03112#define CTRL_V 0x16113#define CTRL_B 0x02114#define CTRL_N 0x0E115#define CTRL_M 0x0D116#define CTRL_COMMA 0x2C117#define CTRL_PERIOD 0x2E118#define CTRL_SLASH 0x1F119#endif120121122