home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 27 Fixes20 / 27-Fixes20.zip / arnea103.zip / arnedll.cpp < prev    next >
Text File  |  1994-06-01  |  6KB  |  195 lines

  1. /*******************************************************
  2.    ARNEDLL.CPP - Source code to a DLL for implementing
  3.    foreign keyboard support in "OS/2 Performance beta I" 
  4.           
  5.    Compiled using IBM C/Set++ 2.1 (Probably works with other 
  6.                                   OS/2 C++ compilers)
  7.  
  8.    This code may be modified to suit a specific keyboard as 
  9.    long as all comments are included in unmodified form.
  10.  
  11.  
  12.    Do you have MICROLEARN SWITCHER - a freeware utility from us?
  13.    Available from ftp - filename of current version: MLSW101.ZIP
  14.  
  15.    (c) Jari Williamsson, MicroLearn Nordic, 1994
  16.    - programmer for MicroLearn Game Pack, Volume 1 & 2 for OS/2
  17.  
  18.  
  19.    If you have questions, please contact: jari@microlearn.se
  20.  
  21. *******************************************************/
  22.  
  23. #define INCL_PM
  24. #define INCL_DOSMODULEMGR
  25. #define INCL_DOSMEMMGR
  26. #include <os2.h>
  27. #include <string.h>
  28. #include <stdio.h>
  29.  
  30. #define DLL_NAME "ARNEDLL" // File name of this DLL
  31.  
  32. #define ARR_SIZE 33 // Size of the array below
  33.  
  34. BOOL UseArneAnka; // Use/don't use flag
  35.  
  36. struct {
  37.    BOOL bAltGraf;     // Use Alt key or not
  38.    UCHAR ucScanCode;  // Scan code for key (only used with Alt key)
  39.    char chFrom;       /* Transform from this US char - used
  40.                          when bAltFraf is FALSE */
  41.    char chTo;         // Transform to this foreign char
  42. } usCharArray[ARR_SIZE] = {
  43.    /* To following table is for Swedish keyboard - modify to suit 
  44.       your keyboard: */
  45.    TRUE,  0x03, '2', '@',
  46.    TRUE,  0x04, '3', '£',
  47.    TRUE,  0x05, '4', '$',
  48.    TRUE,  0x08, '7', '{',
  49.    TRUE,  0x09, '8', '[',
  50.    TRUE,  0x0a, '9', ']',
  51.    TRUE,  0x0b, '0', '}',
  52.    TRUE,  0x0c, '-', '\\',
  53.    TRUE, 27, ']', '~',
  54.    FALSE, 0, '@', '"',
  55.    FALSE, 0, '~', '½',
  56.    FALSE, 0, '`', '⌡',
  57.    FALSE, 0, '$', '╧',
  58.    FALSE, 0, '^', '&',
  59.    FALSE, 0, '&', '/',
  60.    FALSE, 0, '*', '(',
  61.    FALSE, 0, '(', ')',
  62.    FALSE, 0, ')', '=',
  63.    FALSE, 0, '-', '+',
  64.    FALSE, 0, '_', '?',
  65.    FALSE, 0, '\\', 39,   // 39 = '
  66.    FALSE, 0, '|', '*',
  67.    FALSE, 0, '[', 'å',
  68.    FALSE, 0, '{', 'Å',
  69.    FALSE, 0, ';', 'ö',
  70.    FALSE, 0, ':', 'Ö',
  71.    FALSE, 0, 39,  'ä',   // 39 = '
  72.    FALSE, 0, '"', 'Ä',
  73.    FALSE, 0, '<', ';',
  74.    FALSE, 0, '>', ':',
  75.    FALSE, 0, '/', '-',
  76.    FALSE, 0, '?', '_',
  77.    FALSE, 0, '}', '^'
  78. };
  79.  
  80. void CharProc(PQMSG pqmMsg)
  81. // Entry for WM_CHAR filter messages:
  82. {
  83.    USHORT fsKeyFlags = (USHORT) SHORT1FROMMP(pqmMsg->mp1); // Keyboard flags
  84.    USHORT usVirtualKey = (USHORT) SHORT2FROMMP(pqmMsg->mp2); 
  85.                                           // Virtual key - if exist
  86.  
  87.    BYTE bArray[256]; // Keyboard state array
  88.    /* Check current keyboard state - also possible to check
  89.       by using the KC_xxxxxx flags: */
  90.    WinSetKeyboardStateTable(HWND_DESKTOP, bArray, FALSE);  
  91.    BOOL bAltGraf = (bArray[VK_ALT] & 0x80);
  92.    BOOL bShift = (bArray[VK_SHIFT] & 0x80);
  93.    BOOL bControl = (bArray[VK_CTRL] & 0x80);
  94.  
  95.    UCHAR chScanCode = CHAR4FROMMP(pqmMsg->mp1); // Get scancode
  96.  
  97.    if ((chScanCode == 0x3e)  && (bControl) && (!(fsKeyFlags & KC_KEYUP))) {
  98.         // Ctrl-F4 to toggle between using/not using Arne Anka:
  99.         DosBeep(1000, 100);
  100.         UseArneAnka = !UseArneAnka;
  101.         return;
  102.    } /* endif */
  103.    if (!UseArneAnka) return;
  104.  
  105.    // Return if the keyboard message is from a OS/2 or DOS window:
  106.    char szClassName[100];
  107.    WinQueryClassName(pqmMsg->hwnd, 100, (PCH) szClassName);
  108.    szClassName[100] = '\0';
  109.    if (strcmp(szClassName, "Shield") == 0) {
  110.       return; 
  111.    } /* endif */
  112.  
  113.    if (fsKeyFlags & KC_VIRTUALKEY) return; // Don't handle virtual keys
  114.  
  115.    if (chScanCode == 86) {
  116.       // Special key for Swedish keyboard - not present on US keyboards:
  117.       UCHAR uch;
  118.       if (bAltGraf) {
  119.          uch = '|';
  120.       } else if (bShift) {
  121.          uch = '>';
  122.       } else {
  123.          uch = '<';
  124.       }
  125.       fsKeyFlags |= KC_CHAR;
  126.       fsKeyFlags &= ~(KC_ALT | KC_SHIFT);
  127.       pqmMsg->mp1 = MPFROM2SHORT(fsKeyFlags,
  128.                               SHORT2FROMMP(pqmMsg->mp1));
  129.       pqmMsg->mp2 = MPFROM2SHORT(uch,
  130.                               SHORT2FROMMP(pqmMsg->mp2));
  131.       return;
  132.    } /* endif */
  133.  
  134.    if (chScanCode > 0x40) return; // Don't handle numeric keypad etc.
  135.  
  136.    USHORT fsCharacter = (USHORT) SHORT1FROMMP(pqmMsg->mp2);
  137.    for (int i = 0; i < ARR_SIZE; i++) {
  138.       if (usCharArray[i].bAltGraf) {
  139.          // Check for scancode + Alt key:
  140.          if (!bAltGraf) continue;
  141.          if (usCharArray[i].ucScanCode == chScanCode) {
  142.             fsKeyFlags |= KC_CHAR;
  143.             fsKeyFlags &= ~KC_ALT;
  144.             pqmMsg->mp1 = MPFROM2SHORT(
  145.                               fsKeyFlags,
  146.                               SHORT2FROMMP(pqmMsg->mp1));
  147.             pqmMsg->mp2 = MPFROM2SHORT(usCharArray[i].chTo,
  148.                               SHORT2FROMMP(pqmMsg->mp2));
  149.             return;
  150.          } // if
  151.  
  152.       } else {
  153.          // Check for US char and transform to foreign char:
  154.          if (bAltGraf) continue;
  155.          if (fsCharacter == usCharArray[i].chFrom) {
  156.             pqmMsg->mp2 = MPFROM2SHORT(usCharArray[i].chTo,
  157.                               SHORT2FROMMP(pqmMsg->mp2));
  158.             return;
  159.          } // if
  160.       }
  161.    } // for
  162. }
  163.  
  164. BOOL EXPENTRY MLSwitchHook(HAB habAnchor, PQMSG pqmMsg, ULONG fs)
  165. // Hook entry:
  166. {
  167.    switch (pqmMsg->msg) {
  168.       case WM_CHAR:
  169.          CharProc(pqmMsg);
  170.          break;
  171.    } // switch
  172.  
  173.    return FALSE;
  174. }
  175.  
  176.  
  177. BOOL EXPENTRY ArneAInit(HAB hab)
  178. // Init av DLL:
  179. {
  180.    HMODULE hmModule;
  181.    if (DosQueryModuleHandle(DLL_NAME, &hmModule)) {
  182.       return FALSE; // Failed to get handle
  183.    } // if
  184.  
  185.    // Set system hook:
  186.    WinSetHook(hab,
  187.               NULLHANDLE,
  188.               HK_INPUT,
  189.               (PFN)MLSwitchHook,
  190.               hmModule);
  191.  
  192.    UseArneAnka = TRUE; // Use Arne Anka by default
  193.    return TRUE;
  194. }
  195.