home *** CD-ROM | disk | FTP | other *** search
/ Fish 'n' More 2 / fishmore-publicdomainlibraryvol.ii1991xetec.iso / fish / keyboard / input / test.c < prev   
C/C++ Source or Header  |  1991-01-15  |  5KB  |  199 lines

  1. /**************************************
  2. *  TEST.C  08/04/90
  3. *  Written by Timm Martin
  4. *  This source code is public domain.
  5. ***************************************/
  6.  
  7. #include <exec/types.h>
  8. #include <functions.h>
  9. #include <intuition/intuition.h>
  10. #include <intuition/intuitionbase.h>
  11. #include <stdio.h>
  12. #include "input.h"
  13.  
  14. /********************
  15. *  SHARED VARIABLES
  16. *********************/
  17.  
  18. struct IntuitionBase *IntuitionBase = NULL;
  19. struct Window        *win           = NULL;
  20.  
  21. /************************
  22. *  NEW WINDOW STRUCTURE
  23. *************************/
  24.  
  25. struct NewWindow new_window =
  26. {
  27.   /* SHORT           LeftEdge    */ 0,
  28.   /* SHORT           TopEdge     */ 0,
  29.   /* SHORT           Width       */ 640,
  30.   /* SHORT           Height      */ 40,
  31.   /* UBYTE           DetailPen   */ 0,
  32.   /* UBYTE           BlockPen    */ 1,
  33.   /* ULONG           IDCMPFlags  */ CLOSEWINDOW|RAWKEY,
  34.   /* ULONG           Flags       */ ACTIVATE|NOCAREREFRESH|SIMPLE_REFRESH|WINDOWCLOSE|WINDOWDEPTH|WINDOWDRAG|WINDOWSIZING,
  35.   /* struct Gadget * FirstGadget */ NULL,
  36.   /* struct Image *  CheckMark   */ NULL,
  37.   /* UBYTE *         Title       */ (STRPTR)"Input Test Program",
  38.   /* struct Screen * Screen      */ NULL,
  39.   /* struct BitMap * BitMap      */ NULL,
  40.   /* SHORT           MinWidth    */ 230,
  41.   /* SHORT           MinHeight   */ 20,
  42.   /* USHORT          MaxWidth    */ 640,
  43.   /* USHORT          MaxHeight   */ 200,
  44.   /* USHORT          Type        */ WBENCHSCREEN,
  45. };
  46.  
  47. /*************
  48. *  FUNCTIONS
  49. **************/
  50.  
  51. void main( int argc, char *argv[] );
  52. void print_key( USHORT key );
  53. void program_end( char *error );
  54. void program_begin( void );
  55. void window_input( void );
  56.  
  57. /***********
  58. *  M A I N
  59. ************/
  60.  
  61. void main( int argc, char *argv[] )
  62. {
  63.   /* quit if not run from the CLI -- nowhere to send output */
  64.   if (argc)
  65.   {
  66.     program_begin();
  67.     window_input();
  68.   }
  69.   program_end( NULL );
  70. }
  71.  
  72. /*****************
  73. *  PROGRAM BEGIN
  74. ******************/
  75.  
  76. /*
  77. This procedure opens the Intuition library, the window, and the console
  78. device.  If any of these things fail, the program ends.
  79. */
  80.  
  81. void program_begin( void )
  82. {
  83.   if (!(IntuitionBase = (struct IntuitionBase *)
  84.                         OpenLibrary( "intuition.library", 0L )))
  85.     program_end( "intuition" );
  86.  
  87.   if (!(win = OpenWindow( &new_window )))
  88.     program_end( "window" );
  89.  
  90.   if (!console_open())
  91.     program_end( "console" );
  92. }
  93.  
  94. /***************
  95. *  PROGRAM END
  96. ****************/
  97.  
  98. /*
  99. This procedure closes the console device, the window, and the Intuition
  100. library.  It also prints an error message (if any) to the CLI.
  101. */
  102.  
  103. void program_end( char *error )
  104. {
  105.   if (error) printf( "FAILED: %s\n", error );
  106.  
  107.   console_close();
  108.  
  109.   if (win)           CloseWindow( win );
  110.   if (IntuitionBase) CloseLibrary( IntuitionBase );
  111. }
  112.  
  113. /****************
  114. *  WINDOW INPUT
  115. *****************/
  116.  
  117. /*
  118. This procedure monitors for window input.  It returns when the user clicks on
  119. the window close gadget.
  120. */
  121.  
  122. void window_input( void )
  123. {
  124.   BOOL finished;
  125.   struct IntuiMessage *imessage;
  126.   USHORT key;
  127.  
  128.   finished = FALSE;
  129.   while (!finished)
  130.   {
  131.     /* wait for input from the window */
  132.     Wait( 1L<<win->UserPort->mp_SigBit );
  133.  
  134.     /* for each input message */
  135.     while (imessage = (struct IntuiMessage *)GetMsg( win->UserPort ))
  136.     {
  137.       switch (imessage->Class)
  138.       {
  139.         case CLOSEWINDOW:
  140.           finished = TRUE;                  /* end the program */
  141.           break;
  142.         case RAWKEY:
  143.           if (key = input_key( imessage ))  /* if a key was pressed */
  144.             print_key( key );               /* print it */
  145.           break;
  146.       }
  147.       /* reply to the message to free its memory */
  148.       ReplyMsg( (struct Message *)imessage );
  149.     }
  150.   }
  151. }
  152.  
  153. /*************
  154. *  PRINT KEY
  155. **************/
  156.  
  157. /*
  158. This procedure prints an English description of the key that was pressed and
  159. any qualifiers.
  160. */
  161.  
  162. char *command_keys[] =
  163. {
  164.   NULL, "F1", "F2", "F3", "F4", "F5", "F6", "F7", "F8", "F9", "F10", "TAB",
  165.   "DELETE", "ESCAPE", "BACKSPACE", "HELP", "RETURN", "UP_ARROW",
  166.   "DOWN_ARROW", "LEFT_ARROW", "RIGHT_ARROW",
  167. };
  168.  
  169. void print_key( USHORT key )
  170. {
  171.   char c;
  172.  
  173.   /* check for qualifiers */
  174.   if (key & QUAL_LSHIFT)
  175.     printf( "LSHIFT " );
  176.   if (key & QUAL_RSHIFT)
  177.     printf( "RSHIFT " );
  178.   if (key & QUAL_CONTROL)
  179.     printf( "CONTROL " );
  180.   if (key & QUAL_LALT)
  181.     printf( "LALT " );
  182.   if (key & QUAL_RALT)
  183.     printf( "RALT " );
  184.   if (key & QUAL_LAMIGA)
  185.     printf( "LAMIGA " );
  186.   if (key & QUAL_RAMIGA)
  187.     printf( "RAMIGA " );
  188.  
  189.   /* which key was pressed? */
  190.   c = KEY_VALUE( key );
  191.  
  192.   /* if this was a command key */
  193.   if (KEY_COMMAND( key ))
  194.     printf( "%s\n", command_keys[c] );
  195.   /* else just normal ASCII character */
  196.   else
  197.     printf( "'%c'\n", c );
  198. }
  199.