home *** CD-ROM | disk | FTP | other *** search
/ The C Users' Group Library 1994 August / wc-cdrom-cusersgrouplibrary-1994-08.iso / vol_300 / 328_01 / demohkey.c < prev    next >
C/C++ Source or Header  |  1991-02-28  |  3KB  |  153 lines

  1. /*DEMOHKEY.C
  2.  *     this program demonstrates how to install 'hotkey handlers'
  3.  *
  4.  *    a hotkey handler is a function that is executed when a specific key 
  5.  *    ...is pressed. The function takes no arguments, returns no values.
  6.  *
  7.  *        
  8.  */
  9.  
  10.  
  11. #include <stdlib.h>
  12. #include <ctype.h>
  13.  
  14. #include "wtwg.h"
  15.  
  16.  
  17. /* dummy function to echo any key pressed in whicever window is open
  18.  */
  19. void echo_key (void);
  20.  
  21.  
  22. /* Functions which will be set up as hotkeys called by F2 and F3
  23.  * NOTE what happens if you press F2 or F3 when the hotkey function is active.
  24.  *    ...reentrant calls to the hotkey handler are disallowed,
  25.  *  ...and the 'hotkey' (F2 or F3) is passed thru
  26.  *  ... to the func that called wgetc().
  27.  */
  28. void F2func (void);
  29. void F2func (void)
  30.     {
  31.     char *save_help;
  32.  
  33.     save_help = whelp_ptr;
  34.     whelp_ptr = "F2 key";
  35.  
  36.     wopen ( 10, 14, 45, 5, YELLOW, DOUBLE_BORDER, YELLOW, WSAVE2RAM);
  37.     wtitle ("F2 hotkey");
  38.     wgoto ( 0, 3 );
  39.     wputs ( " Now in the F2 HOTKEY FUNCTION...\n"
  40.             " KEYS will now be echoed in this window\n"
  41.             " until you press ESCAPE to return to main."  );
  42.     
  43.     echo_key();
  44.  
  45.     wclose ();
  46.     whelp_ptr = save_help;
  47.  
  48.  
  49.     return;        /* F2func */
  50.     }
  51.     
  52. void F3func (void);
  53. void F3func (void)
  54.     {
  55.     char *save_help;
  56.  
  57.     save_help = whelp_ptr;
  58.     whelp_ptr = "F3 key";
  59.  
  60.     wopen ( 20, 16, 45, 6, LIGHTGRAY<<4,SPECKLE_BORDER,LIGHTGRAY<<4,WSAVE2RAM);
  61.     wtitle ("F3 hotkey");
  62.     wgoto ( 0, 3 );
  63.     wputs ( " Now in the F3 HOTKEY FUNCTION...\n"
  64.             " KEYS will now be echoed in this window\n"
  65.             " until you press ESCAPE to return to main."  );
  66.     
  67.     echo_key();
  68.  
  69.     wclose ();
  70.     whelp_ptr = save_help;
  71.     return;        /* F3func */
  72.     }
  73.     
  74.     
  75. void echo_key (void)
  76.     {
  77.     int key;
  78.     int count = 0;
  79.  
  80.     while ( ESCAPE != ( key= wgetc() ) )
  81.         {
  82.         wgoto ( 0, w0-> winymax );        /* bottom line */
  83.         wscroll ();
  84.         if ( isascii (key) && isprint (key) )
  85.             {
  86.             wputc (key);
  87.             wprintf ("    %i", ++count );
  88.             }
  89.         else
  90.             {
  91.             if ( key == FKEY(2) )
  92.                 {
  93.                 wputs ("F2 HOTKEY is already active\nIGNORED");
  94.                 }
  95.             else
  96.             if ( key == FKEY(2) )
  97.                 {
  98.                 wputs ("F3 HOTKEY is already active\nIGNORED");
  99.                 }
  100.             else
  101.                 {
  102.                 wputs ("Please only enter printable keys...");
  103.                 }
  104.             }
  105.         }
  106.     
  107.     return;        /* echo_key */        
  108.  
  109.     }    
  110.     
  111.     
  112. main()
  113.     {
  114.     winit ( 'T' );
  115.     
  116.     whelp_install ( "demohkey" );        /* F1 */
  117.     whelp_ptr = "demohkey";
  118.     
  119.     whotkey_install ( FKEY(2), F2func );    /* associate FKEY(2) with F2func */
  120.     whotkey_install ( FKEY(3), F3func );
  121.  
  122.  
  123.  
  124.     
  125.     wclear ();
  126.     
  127.     wputs ( "This is a simple echo program. Keys will be echoed in a window.\n"
  128.             "press ESCAPE to quit, press F1 for help, press F2/F3 as hotkeys\n"
  129.           );
  130.     #ifdef __TURBOC__
  131.         /*  demonstrate use of the SysRq key
  132.          *        TurboC only. Hold ALT and press the SysRq (PrintScreen) key
  133.          *
  134.          */
  135.         wSysRq_install ( wSysRq_ask );
  136.         wputs ( "Or press ALT-SysRq for keyboard interrupt demo.\n" 
  137.                 "If keyboard locks, press ALT-SysRq again" );
  138.     #endif /* __TURBOC__ */
  139.     
  140.     wopen ( 4,5, 45, 11,  
  141.         LIGHTGRAY+(RED<<4),  SINGLE_BORDER,  LIGHTGRAY+(RED<<4), 0 );
  142.     wtitle ( "MAIN PROGRAM WINDOW" );
  143.     
  144.     echo_key ();
  145.     
  146.     wclose ();
  147.     wclear ();        /* clears fullscreen */
  148.     
  149.     wputs ("That\'s all folks");
  150.     
  151.     return (0);        /* main */
  152.     
  153.     }