Source code for /engineering/SERIAL-TEST/KEY.CPPOriginal file KEY.CPP
   1 // --------------------------------------------------------------------------
   2 // --
   3 // -- FBUFFERS - FIFO buffers.
   4 // --
   5 // --
   6 // --------------------------------------------------------------------------
   7 
   8 // --------------------------------------------------------------------------
   9 // -- Includes
  10 // --------------------------------------------------------------------------
  11 #include <stdio.h>
  12 #include <stdlib.h>
  13 #include <conio.h>
  14 #include "defines.h"
  15 #include "key.h"
  16 
  17 // --------------------------------------------------------------------------
  18 // -- Definitions
  19 // --------------------------------------------------------------------------
  20 
  21 #define KEY_EXTENDED_KEY		0
  22 
  23 // --------------------------------------------------------------------------
  24 // -- Methods
  25 // --------------------------------------------------------------------------
  26 
  27 // --- Constructor ------------------
  28 KeyHandler::KeyHandler(void) {
  29 
  30    invalidateAll();
  31 
  32 }
  33 
  34 
  35 // --- Destructor -------------------
  36 // NONE
  37 
  38 
  39 
  40 // --- Register a single command to the table
  41 KeyError KeyHandler::registerCommand(KeyElement	  element,
  42 									 Boolean 	  extended ) {
  43 
  44    if (element.keyCode >= KEY_MAX_TRANSLATION) return KEY_OUT_OF_RANGE;
  45 
  46    if (extended) {
  47 	  extTranslation[element.keyCode] = element.theCommand;
  48 
  49    } else {
  50 	  baseTranslation[element.keyCode] = element.theCommand;
  51 
  52    }
  53 
  54    return KEY_NO_ERROR;
  55 }
  56 
  57 
  58 // --- Register a single command to the table, simple way
  59 KeyError KeyHandler::registerCommand(KeyCommand    theCommand,
  60 									 unsigned int  keyCode,
  61 									 Boolean 	   extended    ) {
  62 
  63    if (keyCode >= KEY_MAX_TRANSLATION) return KEY_OUT_OF_RANGE;
  64 
  65    if (extended) {
  66 	  extTranslation[keyCode] = theCommand;
  67 
  68    } else {
  69 	  baseTranslation[keyCode] = theCommand;
  70 
  71    }
  72 
  73    return KEY_NO_ERROR;
  74 }
  75 
  76 
  77 // -- Register an entire set of the table, as a key element
  78 KeyError KeyHandler::registerTable(KeyElement   *theTable,
  79 								   int			 number,
  80 								   Boolean       extended      ) {
  81 
  82    int        step   = 0;
  83    KeyElement *rover = theTable;
  84 
  85    for (step = 0; step < number; step++) {
  86 
  87 	  if (rover->keyCode>= KEY_MAX_TRANSLATION) return KEY_OUT_OF_RANGE;
  88 
  89 	  if (extended) {
  90 		 extTranslation[rover->keyCode]  = rover->theCommand;
  91 
  92 	  } else {
  93 		 baseTranslation[rover->keyCode] = rover->theCommand;
  94 
  95 	  }
  96 
  97 	  rover++;
  98 
  99    };
 100 
 101    return KEY_NO_ERROR;
 102 
 103 }
 104 
 105 
 106 // -- Unregister a specific command
 107 KeyError KeyHandler::unregisterCommand(unsigned int  keyCode,
 108 									   Boolean       extended ) {
 109 
 110    if (keyCode >= KEY_MAX_TRANSLATION) return KEY_OUT_OF_RANGE;
 111 
 112    if (extended) {
 113 	  extTranslation[keyCode]  = KEY_NO_COMMAND;
 114 
 115    } else {
 116 	  baseTranslation[keyCode] = KEY_NO_COMMAND;
 117 
 118    }
 119 
 120    return KEY_NO_ERROR;
 121 
 122 }
 123 
 124 
 125 // -- Dump the entire contents of both tables
 126 void KeyHandler::invalidateAll(void) {
 127 
 128    for (int step = 0; step < KEY_MAX_TRANSLATION; step++ ) {
 129 	  baseTranslation[step] = KEY_NO_COMMAND;
 130 	  extTranslation[step]  = KEY_NO_COMMAND;
 131    }
 132 }
 133 
 134 
 135 // -- Flush the keyboard ------------
 136 void KeyHandler::flush(void) {
 137 
 138    unsigned char  key;
 139 
 140    while( kbhit() ) {
 141 
 142 	  key = getch();
 143 	  if (key == KEY_EXTENDED_KEY) getch();
 144 
 145    }
 146 }
 147 
 148 
 149 // -- Poll, a NON-blocking char get
 150 Boolean  KeyHandler::poll(unsigned char &theChar,
 151 						  KeyCommand    &theCommand) {
 152 
 153    if (kbhit()) {
 154 
 155 	  theChar = getch();
 156 	  if (theChar == KEY_EXTENDED_KEY) {
 157 		 theChar    = getch();
 158 		 theCommand = extTranslation[theChar];
 159 
 160 	  } else {
 161 		 theCommand = baseTranslation[theChar];
 162 
 163 	  }
 164 
 165 	  return TRUE;
 166 
 167    } else {
 168 
 169 	  theCommand = KEY_NO_COMMAND;
 170 	  return FALSE;
 171    }
 172 
 173 
 174 }
 175 
 176 
 177 // -- get, a BLOCKING char get
 178 KeyCommand KeyHandler::get( unsigned char &theChar) {
 179 
 180    theChar = getch();
 181    if (theChar == KEY_EXTENDED_KEY) {
 182 	  theChar    = getch();
 183 	  return (extTranslation[theChar]);
 184 
 185    } else {
 186 	  return (baseTranslation[theChar]);
 187 
 188    }
 189 
 190 }
 191 
 192