home *** CD-ROM | disk | FTP | other *** search
/ Liren Large Software Subsidy 7 / 07.iso / c / c222 / 1.ddi / SOURCE / IBMLIB / KB_HANDL.C < prev    next >
Encoding:
C/C++ Source or Header  |  1990-07-10  |  3.7 KB  |  215 lines

  1. /*********************
  2.  *
  3.  *  kb_handl.c - keyboard handler functions.
  4.  *
  5.  *  Purpose: Functions for getting keyboard input from the IBM PC.
  6.  *
  7.  *  Blackstar C Function Library
  8.  *  (c) Copyright 1985,1989 Sterling Castle Software
  9.  *
  10.  *******/
  11.  
  12. #include "blackstr.h"
  13. #include "kb_head.h"
  14. #include "kb_defs.h"
  15. #include "ut_head.h"
  16. #include "primitiv.h"
  17.  
  18. extern void exit(int status);
  19.  
  20.  
  21. /********
  22.  *
  23.  *   kb_getc() - raw keyboard input
  24.  *
  25.  **/
  26.  
  27. int kb_getc(void)
  28. {
  29.     int i;
  30.     char *ptr;
  31.  
  32.     i = kb_getc_();             /* get it from driver       */
  33.     if(!(i & 0x00ff)) {         /* extended keys            */
  34.     ptr = keystbl_;         /* key scan xlate table     */
  35.     i/= 256;                /* ah has character         */
  36.     while(*ptr) {           /* table is null terminated */
  37.         if(*ptr==i)
  38.         return(0x00ff&(short int)(*(++ptr)));
  39.         else
  40.         ptr += 2;       /* next pair                */
  41.     }
  42.     }
  43.     return(i & 0x00ff);         /* mask off hi byte         */
  44. }
  45.  
  46.  
  47. /********
  48.  *
  49.  *   kb_getch() - get character w break & echo
  50.  *
  51.  **/
  52.  
  53. int kb_getch(void)
  54. {
  55.     int i;
  56.  
  57.     i = kb_getc();
  58.     if((breakf_) && (kb_isbrk(i)))
  59.     sy_abort();
  60.     if ((echof_) && (!kb_isfun(i)))
  61.     sc_putch(i);
  62.     return(i);
  63. }
  64.  
  65.  
  66. /********
  67.  *
  68.  *   kb_gets(str) - get string from keyboard
  69.  *
  70.  **/
  71.  
  72. char *kb_gets(char *str)
  73. {
  74.     int j;
  75.     char *ptr;
  76.  
  77.     ptr = str;
  78.     while( ((j=kb_getch())!=NEWLINE) && (j!=CR) )
  79.     *str++ = j;
  80.     *str = NUL;
  81.     return(ptr);
  82. }
  83.  
  84.  
  85. /********
  86.  *
  87.  *   kb_init(ftbl,xtbl,btbl) - initialize keyboard (0,0,0) for default
  88.  *
  89.  **/
  90.  
  91. void kb_init(char *ftbl, char *xtbl, char *btbl)
  92. {
  93.     keystbl_ = keystbl1_;           /* scan code table */
  94.  
  95.     /* function key table */
  96.     (ftbl)? (keyftbl_ = ftbl) : (keyftbl_ = keyftbl1_);
  97.  
  98.     /* translation table */
  99.     (xtbl)? (keyxtbl_ = xtbl) : (keyxtbl_ = keyxtbl1_);
  100.  
  101.     /* break table */
  102.     (btbl)? (keybrktbl_= btbl) : (keybrktbl_= keybrktbl1_);
  103.  
  104.     kb_clr();                       /* clear it           */
  105.     chinf_ = FALSE;                 /* clear char in flag */
  106.     echof_ = TRUE;                  /* turn echo on       */
  107.     insmodef_=FALSE;                /* insert off         */
  108.     breakf_ = TRUE;                 /* break on           */
  109. }
  110.  
  111.  
  112. /********
  113.  *
  114.  *   kb_getchar() - get character from keyboard with break ctrl-c
  115.  *
  116.  **/
  117.  
  118. int kb_getchar(void)
  119. {
  120.     int i;
  121.  
  122.     i = kb_getc();              /* get character */
  123.     if (i==CTRL_C)
  124.     exit(0);                /* just exit     */
  125.     if(echof_)
  126.     sc_putch(i);
  127.     return(i);
  128. }
  129.  
  130.  
  131. /********
  132.  *
  133.  *   kb_ungetc(c) - return previously gotten character
  134.  *
  135.  **/
  136.  
  137. int kb_ungetc(char c)
  138. {
  139.     return(kb_ungetc_(c));
  140. }
  141.  
  142.  
  143. /********
  144.  *
  145.  *   kb_inkey() - get key from keyboard if one available
  146.  *
  147.  **/
  148.  
  149. int kb_inkey(void)
  150. {
  151.     return (kb_hit()? kb_getc() : FALSE);
  152. }
  153.  
  154.  
  155. /********
  156.  *
  157.  *   kb_hit() - see if key was struck on keyboard
  158.  *
  159.  **/
  160.  
  161. int kb_hit(void)
  162. {
  163.     return(kb_hit_());
  164. }
  165.  
  166.  
  167. /********
  168.  *
  169.  *   kb_pause() - pause until keyboard hit
  170.  *
  171.  **/
  172.  
  173. void kb_pause(void)
  174. {
  175.     while(!kb_hit());
  176.     kb_clr();
  177. }
  178.  
  179.  
  180. /********
  181.  *
  182.  *   kb_tpause(time) - pause until keyboard hit or 100's secs passed
  183.  *
  184.  **/
  185.  
  186. void kb_tpause(int time)
  187. {
  188.     struct TIME stime;
  189.     int t1,t2;
  190.  
  191.     ut_times(&stime);       /* get current time */
  192.     t1 = t2 = 100*stime.sec+stime.hsec;
  193.  
  194.     while((t2-t1<time) && (!kb_hit())) {
  195.     ut_times(&stime);
  196.     t2 = 100*stime.sec+stime.hsec;
  197.     if(t2<t1)
  198.         t2 += 6000;     /* add a minute */
  199.     }
  200.     kb_clr();
  201. }
  202.  
  203.  
  204. /********
  205.  *
  206.  *   kb_clr() - clear the keyboard
  207.  *
  208.  **/
  209.  
  210. void kb_clr(void)
  211. {
  212.     while(kb_hit())
  213.     kb_getc();
  214. }
  215.