home *** CD-ROM | disk | FTP | other *** search
/ Amiga Developer CD 1.2 / amidev_cd_12.iso / reference_library / devices / dev_examples / keyboard_events.c < prev    next >
Encoding:
C/C++ Source or Header  |  1992-08-20  |  2.8 KB  |  99 lines

  1. /* Shown below is an example keyboard.device read-event program:   */
  2. /*
  3.  * Keyboard_Events.c
  4.  *
  5.  * This example does not work very well in a system where
  6.  * input.device is active since input.device also actively calls for
  7.  * keyboard events via this call. For that reason, you will not get all of
  8.  * the keyboard events. Neither will the input device;no one will be happy.
  9.  *
  10.  * Compile with SAS 5.10  lc -b1 -cfistq -v -y -L
  11.  *
  12.  * Run from CLI only
  13.  */
  14.  
  15. #include <exec/types.h>
  16. #include <exec/io.h>
  17. #include <exec/ports.h>
  18. #include <exec/memory.h>
  19. #include <devices/inputevent.h>
  20. #include <devices/keyboard.h>
  21.  
  22. #include <clib/exec_protos.h>
  23. #include <clib/alib_protos.h>
  24.  
  25. #include <stdio.h>
  26.  
  27. #ifdef LATTICE
  28. int CXBRK(void) { return(0); }     /* Disable SAS CTRL/C handling */
  29. int chkabort(void) { return(0); }  /* really */
  30. #endif
  31.  
  32. VOID Display_Event(struct InputEvent *keyEvent)
  33. {
  34. printf("Got key event: KeyCode: %2x  Quailifiers: %4x\n",
  35.                keyEvent->ie_Code,
  36.                keyEvent->ie_Qualifier);
  37. }
  38.  
  39. VOID main(int argc, char *argv[])
  40. {
  41. struct IOStdReq   *keyRequest;
  42. struct MsgPort    *keyPort;
  43. struct InputEvent *keyEvent;
  44.        SHORT      loop;
  45.  
  46. if (keyPort=CreatePort(NULL,NULL))
  47.   {
  48.   if (keyRequest=
  49.           (struct IOStdReq *)CreateExtIO(keyPort,sizeof(struct IOStdReq)))
  50.       {
  51.       if (!OpenDevice("keyboard.device",NULL,
  52.                         (struct IORequest *)keyRequest,NULL))
  53.           {
  54.           if (keyEvent=AllocMem(sizeof(struct InputEvent),MEMF_PUBLIC))
  55.               {
  56.               for (loop=0;loop<4;loop++)
  57.                    {
  58.                    keyRequest->io_Command=KBD_READEVENT;
  59.                    keyRequest->io_Data=(APTR)keyEvent;
  60.  
  61.                    /*
  62.                     * We want 1 event, so we just set the
  63.                     * length field to the size, in bytes
  64.                     * of the event.  For multiple events,
  65.                     * set this to a multiple of that size.
  66.                     * The keyboard device NEVER fills partial
  67.                     * events...
  68.                     */
  69.  
  70.                    keyRequest->io_Length=sizeof(struct InputEvent);
  71.                    DoIO((struct IORequest *)keyRequest);
  72.  
  73.                        /* Check for CLI startup... */
  74.                    if (argc)
  75.                        Display_Event(keyEvent);
  76.                    }
  77.  
  78.               FreeMem(keyEvent,sizeof(struct InputEvent));
  79.               }
  80.           else
  81.               printf("Error:Could not allocate memory for InputEvent\n");
  82.  
  83.           CloseDevice((struct IORequest *)keyRequest);
  84.           }
  85.       else
  86.           printf("Error: Could not open keyboard.device\n");
  87.  
  88.       DeleteExtIO((struct IORequest *)keyRequest);
  89.       }
  90.   else
  91.       printf("Error: Could not create I/O request\n");
  92.  
  93.   DeletePort(keyPort);
  94.   }
  95.   else
  96.     printf("Error: Could not create message port\n");
  97. }
  98.  
  99.