home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 10 Tools / 10-Tools.zip / cenvi23.zip / NUMLOCK.CMD < prev    next >
OS/2 REXX Batch file  |  1995-03-31  |  5KB  |  128 lines

  1. EXTPROC CEnvi2
  2. /****************************************************************************
  3.  *** NumLock.cmd - Turn ON NUMLOCK for this session and for all sessions. ***
  4.  *** ver.1         This program, or a shadow of it, can be included in    ***
  5.  ***               your OS/2 startup folder if you want NumLock on as     ***
  6.  ***               a default when you turn on your computer.              ***
  7.  ***                                                                      ***
  8.  ***               If you run with a second parameter, then that is the   ***
  9.  ***               wait interval and this program runs forever setting    ***
  10.  ***               the numlock key at that interval.                      ***
  11.  ****************************************************************************/
  12.  
  13.  
  14. main(argc,argv)
  15. {
  16.    if ( argc != 1  &&  0 == (WaitInterval = atoi(argv[1])) ) {
  17.       ShowInstructions();
  18.    } else {
  19.       SetLocalKeyboardStatus();
  20.       SetGlobalKeyboardTable();
  21.       if ( 1 == argc ) {
  22.          puts("Numlock ON.  Enter NUMLOCK /? for more instructions.");
  23.       } else {
  24.          // kill the resident if it's already running
  25.          system("kill \"%s\"",WindowTitle);
  26.          // install the resident version of this program
  27.          system("start \"%s\" /N /B /WIN /MIN CEnvi2 #include '%s,,,/*RESIDENT*/' Forever(%d)",
  28.                 WindowTitle,argv[0],WaitInterval);
  29.       }
  30.    }
  31. }
  32.  
  33. ShowInstructions()
  34. {
  35.    printf("\a\n");
  36.    printf("NumLock.cmd - Set the NumLock key ON for OS/2 sessions and for the WPS.\n");
  37.    printf("\n");
  38.    printf("SYNTAX: NumLock [Interval]\n");
  39.    printf("\n");
  40.    printf("Where:   Interval - Optional period to suspend, without taking processor\n");
  41.    printf("                    time, before setting the NUMLOCK key again. If this\n");
  42.    printf("                    is not supplied or is 0 then will only set NUMLOCK\n");
  43.    printf("                    once and then exit. Time is in milliseconds. You may\n");
  44.    printf("                    want to set this process looping every second, or so,\n");
  45.    printf("                    if you need to keep NUMLOCK reset after a full-screen\n");
  46.    printf("                    session has undone it.\n");
  47.    printf("\n");
  48.    printf("Example: NumLock         - sets NumLock only once\n");
  49.    printf("         NumLock 1000    - sets NumLock once every second\n");
  50.    printf("\n");
  51. }
  52.  
  53. #define  NUMLOCK_BIT    0x20
  54.  
  55. SetLocalKeyboardStatus()  // set NUMLOCK on for local session
  56. {
  57.    // Open $Kbd device
  58.    #define ORD_DOS32OPEN   273
  59.    DynamicLink("doscalls",ORD_DOS32OPEN,BIT32,CDECL,
  60.                "\\DEV\\KBD$",FileHandle,ActionTaken,0,
  61.                0,0x01,0x40,0);
  62.  
  63.    // call DosDevIOCTL to get kbd state
  64.    #define ORD_DOS32DEVIOCTL     284
  65.    #define KEYBOARD_CATEGORY     4
  66.    #define KBD_GETSHIFTSTATE     0x0073
  67.    #define DATA_SIZE             3
  68.    BLObSize(Data,DATA_SIZE);
  69.    DynamicLink("doscalls",ORD_DOS32DEVIOCTL,BIT32,CDECL,
  70.                FileHandle,KEYBOARD_CATEGORY,KBD_GETSHIFTSTATE,
  71.                NULL,0,NULL,Data,DATA_SIZE,NULL);
  72.    ShiftState = BLObGet(Data,0,UWORD16);
  73.  
  74.    // call DosDevIOCTL to set kbd state
  75.    ShiftState |= NUMLOCK_BIT;
  76.    #define KBD_SETSHIFTSTATE     0x0053
  77.    BLObPut(Parms,ShiftState,UWORD16);
  78.    DynamicLink("doscalls",ORD_DOS32DEVIOCTL,BIT32,CDECL,
  79.                FileHandle,KEYBOARD_CATEGORY,KBD_SETSHIFTSTATE,
  80.                Parms,DATA_SIZE,NULL,NULL,0,NULL);
  81.  
  82.    // set LED's to show the state of the keyboard
  83.    #define KBD_SETLEDS  0x005A
  84.    leds = (ShiftState & 0x70) >> 4;
  85.    BLObPut(Parms,0,leds,UWORD16);
  86.    DynamicLink("doscalls",ORD_DOS32DEVIOCTL,BIT32,CDECL,
  87.                FileHandle,KEYBOARD_CATEGORY,KBD_SETLEDS,
  88.                Parms,2,NULL,NULL,0,NULL);
  89.  
  90.    // close device
  91.    #define ORD_DOS32CLOSE  257
  92.    DynamicLink("doscalls",ORD_DOS32CLOSE,BIT32,CDECL,FileHandle);
  93. }
  94.  
  95.  
  96. /*RESIDENT*/
  97. // All code below this is part of the resident program
  98.  
  99. WindowTitle = "NumLock Forever";
  100.  
  101. Forever(WaitInterval)
  102. {
  103.    // hide this window
  104.    system("WinSet \"%s\" HIDE",WindowTitle);
  105.    // check at every interval and set keyboard if it is not already set
  106.    for(;;) {
  107.       SetGlobalKeyboardTable();
  108.       suspend(WaitInterval);
  109.    }
  110. }
  111.  
  112. SetGlobalKeyboardTable() // set VK_NUMLOCK bit if not already set
  113. {
  114.    #define HWND_DESKTOP                     1
  115.    #define VK_NUMLOCK     0x1D
  116.    #define ORD_WIN32SETKEYBOARDSTATETABLE   921
  117.    KeyTable[255] = '\0'; // pre-allocate a 256 byte buffer
  118.    if ( DynamicLink("PMWIN",ORD_WIN32SETKEYBOARDSTATETABLE,BIT32,CDECL,
  119.                     HWND_DESKTOP,KeyTable,FALSE) ) {
  120.       if ( !(KeyTable[VK_NUMLOCK] & 1) ) {
  121.          KeyTable[VK_NUMLOCK] |= 1;
  122.          DynamicLink("PMWIN",ORD_WIN32SETKEYBOARDSTATETABLE,BIT32,CDECL,
  123.                      HWND_DESKTOP,KeyTable,TRUE);
  124.       }
  125.    }
  126. }
  127.  
  128.