home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 10 Tools / 10-Tools.zip / MJHKGEN.LZH / MJHKGENP.C < prev    next >
Text File  |  1991-05-05  |  7KB  |  155 lines

  1. /***************************************************************************/
  2. /*                                                                         */
  3. /* PROGRAM NAME: MJHKGENP - Generic System Wide Input Hook Procedure       */
  4. /* -------------                                                           */
  5. /*  Process input messages.  Start the Command To Execute if the           */
  6. /*  Key To Intercept is pressed.  Pass the Argument String to the          */
  7. /*  Command To Execute as parameters.                                      */
  8. /*                                                                         */
  9. /* IN ORDER TO BE ABLE TO READ ALL THE COMMENTS IN THIS                    */
  10. /* PROGRAM, READ THE SOURCE FILES INCLUDED SEPARATELY.                     */
  11. /*                                                                         */
  12. /* AUTHOR:  Michael R. Jones (Fidonet Net Mail address 1:202/204)          */
  13. /* -------                                                                 */
  14. /*                                                                         */
  15. /* LOG:  04/29/91 Program Creation                                         */
  16. /* ----                                                                    */
  17. /*                                                                         */
  18. /* RESTRICTIONS:  None.  This program is Public Domain.  Please note that  */
  19. /* -------------         this program is not complete.  It was written as  */
  20. /*                       a sample System Input Queue Hook for the IdleNews */
  21. /*                       Newsletter. It works but isn't production ready.  */
  22. /***************************************************************************/
  23.  
  24. #define INCL_DOS
  25. #define INCL_ERRORS
  26. #define INCL_PM
  27. #include <os2.h>
  28.  
  29. #include <stdio.h>
  30. #include <string.h>
  31.  
  32. int            _acrtused = 0;
  33. BYTE           byteKeyToIntercept = ' ';
  34.  
  35. BOOL EXPENTRY GenericHookProc( HAB, PQMSG, USHORT );
  36. BOOL EXPENTRY GenericHookStartProcess( HAB, PQMSG, USHORT );
  37.  
  38. BOOL EXPENTRY GenericHookProc( hab, pqmsg, usRemove )
  39. HAB            hab;
  40. PQMSG          pqmsg;
  41. USHORT         usRemove;
  42.   {
  43.   PBYTE            pbyte;
  44.   SEL              selSharedMJHKGEN;
  45.  
  46.   if( byteKeyToIntercept == ' ' )        /* Only get the Key To Intercept  */
  47.     {                                    /* once.                          */
  48.     DosGetShrSeg( "\\SHAREMEM\\MJHKGEN",
  49.                   &selSharedMJHKGEN );
  50.     pbyte = MAKEP(selSharedMJHKGEN, 1);
  51.     byteKeyToIntercept = *pbyte;
  52.     DosFreeSeg( selSharedMJHKGEN );
  53.     }
  54.  
  55.   if( pqmsg->msg != WM_CHAR )          /* Only care about WM_CHAR messages */
  56.     goto end_function;
  57.  
  58.   if( SHORT1FROMMP(pqmsg->mp1)         /* Ignore KEYUP, process down       */
  59.         & KC_KEYUP )
  60.     goto end_function;
  61.  
  62.   switch( SHORT2FROMMP(pqmsg->mp2) )   /* Check if the key pressed matches */
  63.     {                                  /* the Key To Intercept.            */
  64.     case VK_F1:
  65.       if( byteKeyToIntercept == '1' )  /* Note that this construct is quite*/
  66.         goto start_process;            /* crude.  A more generic, efficient*/
  67.       break;                           /* routine should be used to see if */
  68.     case VK_F2:                        /* the key pressed matches the Key  */
  69.       if( byteKeyToIntercept == '2' )  /* To Intercept.  It would be better*/
  70.         goto start_process;            /* to translate the Key To Intercept*/
  71.       break;                           /* to the VK_ equivalent in the     */
  72.     case VK_F3:                        /* MJHKGEN.C program so that this   */
  73.       if( byteKeyToIntercept == '3' )  /* comparision could be done in one */
  74.         goto start_process;            /* line of code.  Efficiency is very*/
  75.       break;                           /* important inside this function   */
  76.     case VK_F4:                        /* because it will most likely get  */
  77.       if( byteKeyToIntercept == '4' )  /* executed for every input message */
  78.         goto start_process;            /* for all applications.  For that  */
  79.       break;                           /* matter, these hooks would be best*/
  80.     case VK_F5:                        /* implemented for production use in*/
  81.       if( byteKeyToIntercept == '5' )  /* assembler.  C language is best   */
  82.         goto start_process;            /* for prototyping since debugging  */
  83.       break;                           /* is easier.  Debugging is a major */
  84.     case VK_F6:                        /* concern since you have to reboot */
  85.       if( byteKeyToIntercept == '6' )  /* or kill PM entirely to free this */
  86.         goto start_process;            /* DLL in order to replace it.      */
  87.       break;
  88.     case VK_F7:
  89.       if( byteKeyToIntercept == '7' )
  90.         goto start_process;
  91.       break;
  92.     case VK_F8:
  93.       if( byteKeyToIntercept == '8' )
  94.         goto start_process;
  95.       break;
  96.     case VK_F9:
  97.       if( byteKeyToIntercept == '9' )
  98.         goto start_process;
  99.       break;
  100.     }
  101.  
  102.   goto end_function;
  103.  
  104.   start_process:
  105.    GenericHookStartProcess( hab, pqmsg, usRemove );
  106.  
  107.   end_function:
  108.    return( FALSE ); /* Returning FALSE allows other hooks and the recipient*/
  109.                     /* window to receive this message unless another hook  */
  110.                     /* later in the chain returns TRUE which halts the     */
  111.                     /* passing of the message down the line.  Returning    */
  112.                     /* TRUE is one way of disabling system or application  */
  113.                     /* functions.                                          */
  114.   }
  115.  
  116.  
  117. /***************************************************************************\
  118. | Start the Command To Execute running as an asynchronous process.          |
  119. \***************************************************************************/
  120.  
  121. BOOL EXPENTRY GenericHookStartProcess( hab, pqmsg, usRemove )
  122. HAB            hab;
  123. PQMSG          pqmsg;
  124. USHORT         usRemove;
  125.   {
  126.  
  127.   PSZ              pszArgumentString;
  128.   PSZ              pszCommandToExecute;
  129.   RESULTCODES      ResultCodes;
  130.   SEL              selSharedMJHKGEN;
  131.  
  132.   DosGetShrSeg( "\\SHAREMEM\\MJHKGEN",  /* Get access to arguments.  Access*/
  133.                 &selSharedMJHKGEN );    /* should really be semaphored.    */
  134.  
  135.   pszCommandToExecute
  136.     = MAKEP(selSharedMJHKGEN,
  137.             strlen( MAKEP(selSharedMJHKGEN,0) ) + 1);
  138.  
  139.   pszArgumentString
  140.     = pszCommandToExecute
  141.        + strlen( pszCommandToExecute ) + 1;
  142.  
  143.   DosExecPgm( NULL,               /* This should really be DosStartSession */
  144.               0,                  /* which can start executables which are */
  145.               EXEC_ASYNC,         /* of a different executable type than   */
  146.               pszArgumentString,  /* the calling session.  DosExecPgm will */
  147.               (PSZ)NULL,          /* only successfully start PM programs.  */
  148.               &ResultCodes,
  149.               pszCommandToExecute );
  150.  
  151.   DosFreeSeg( selSharedMJHKGEN ); /* Keep shared memory freed for MJHKGEN.C*/
  152.  
  153.   return( TRUE );
  154.   }
  155.