home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 11 Util / 11-Util.zip / keybset.zip / slhook.c < prev    next >
Text File  |  1995-03-19  |  3KB  |  105 lines

  1. /***************************************************************************\
  2. * slhook.c - this is the DLL part of the ShiftLock project    950319
  3. *
  4. * Copyright (c) 1995 Martin Lafaix. All Rights Reserved.
  5. \***************************************************************************/
  6.  
  7. #define INCL_WINHOOKS
  8. #define INCL_WININPUT
  9. #define INCL_WINMESSAGEMGR
  10. #define INCL_DOSMODULEMGR
  11.  
  12. #include <os2.h>
  13.  
  14. HAB hab;
  15. HMODULE hm;
  16. BYTE KeyState[257];
  17.  
  18. #define DLL_NAME  "SLHOOK"
  19.  
  20. static VOID _setcaps(BOOL bSet)
  21. /*
  22.  * This function sets the CAPSLOCK state.
  23.  *
  24.  * Input:  bSet - new CAPSLOCK state.
  25.  * Returns:  nothing.
  26.  */
  27. {
  28.   WinSetKeyboardStateTable(HWND_DESKTOP, &KeyState[1], FALSE);
  29.  
  30.   if(bSet)
  31.     KeyState[VK_CAPSLOCK+1] |= 0x01;
  32.   else
  33.     KeyState[VK_CAPSLOCK+1] &= ~0x01;
  34.  
  35.   WinSetKeyboardStateTable(HWND_DESKTOP, &KeyState[1], TRUE);
  36. }
  37.  
  38. BOOL EXPENTRY SlHook(HAB hab, PQMSG pQmsg, ULONG fs)
  39. /*
  40.  * This is the input hook that is used to detect VK_CAPSLOCK and VK_SHIFT
  41.  * usage.
  42.  *
  43.  * We only have to handle the up-to-down transition.
  44.  *
  45.  * Input:  hab - handle of the anchor block of the application to
  46.  *               receive this message
  47.  *         pQmsg - pointer to the QMSG structure to be passed to the
  48.  *                 application via WinGetMsg/WinPeekMsg
  49.  *         fs - specifies whether or not the message will be removed
  50.  *              from the queue.
  51.  * Returns:  FALSE allways.
  52.  */
  53. {
  54.   if(pQmsg->msg == WM_CHAR && (SHORT1FROMMP(pQmsg->mp1) & (KC_KEYUP|KC_PREVDOWN)) == 0)
  55.     {
  56.     USHORT usvk = SHORT2FROMMP(pQmsg->mp2);
  57.     USHORT ussc = HIBYTE(SHORT2FROMMP(pQmsg->mp1));
  58.  
  59.     if((SHORT1FROMMP(pQmsg->mp1) & KC_SCANCODE) == KC_SCANCODE &&
  60.        (ussc == 0x2A /* L-shift */ || ussc == 0x36 /* R-shift*/ || ussc == 0x3A /* CapsLock */))
  61.       _setcaps(ussc == 0x3A);
  62.     else
  63.     if((SHORT1FROMMP(pQmsg->mp1) & KC_VIRTUALKEY) == KC_VIRTUALKEY &&
  64.        (usvk == VK_SHIFT || usvk == VK_CAPSLOCK))
  65.       _setcaps(usvk == VK_CAPSLOCK);
  66.     }
  67.  
  68.   return FALSE;
  69. }
  70.  
  71. BOOL EXPENTRY SLINIT(HAB habCaller)
  72. /*
  73.  * This function initializes the DLL and sets the input hook.
  74.  *
  75.  * Input:  habCaller - handle of the caller anchor block
  76.  * Returns:  TRUE if successful, FALSE otherwise
  77.  */
  78. {
  79.   hab = habCaller;
  80.  
  81.   if(DosQueryModuleHandle(DLL_NAME,&hm))
  82.     return FALSE;
  83.  
  84.   WinSetHook(hab, NULLHANDLE, HK_INPUT, (PFN)SlHook, hm);
  85.  
  86.   return TRUE;
  87. }
  88.  
  89. BOOL EXPENTRY SLTERM(VOID)
  90. /*
  91.  * This function releases the input hook and broadcasts a WM_NULL message
  92.  * to all top level windows so that they will release the DLL.  If we don't
  93.  * do this, the DLL will remain locked and we'll have to reboot in order to
  94.  * recompile.
  95.  *
  96.  * Returns:  TRUE always
  97.  */
  98. {
  99.   WinReleaseHook(hab, NULLHANDLE, HK_INPUT, (PFN)SlHook, hm);
  100.  
  101.   WinBroadcastMsg(HWND_DESKTOP,WM_NULL,0,0,BMSG_FRAMEONLY|BMSG_POST);
  102.  
  103.   return TRUE;
  104. }
  105.