Rob van der Woude's Scripting Pages
Powered by GeSHi

Source code for keylocks.bat

(view source code of keylocks.bat as plain text)

  1. @GOTO :Batch
  2.  
  3. D 0:417 L 1
  4. Q
  5.  
  6. :Batch
  7. @ECHO OFF
  8. :: Check the command line arguments
  9. IF NOT "%OS%"=="Windows_NT" GOTO Syntax
  10. IF NOT "%~2"=="" GOTO Syntax
  11. IF NOT "%~1"=="" IF /I NOT "%~1"=="C" IF /I NOT "%~1"=="N" IF /I NOT "%~1"=="S" GOTO Syntax
  12.  
  13.  
  14. :: Run DEBUG with the temporary script and capture the result
  15. FOR /F "skip=4 tokens=2" %%A IN ('DEBUG ^< "%~f0" 2^>NUL') DO SET /A KeyLocks = 0x%%A
  16.  
  17. :: Calculate the status of the individual keys
  18. SET /A CapsLock   = "(%KeyLocks% & 0x40) / 0x40"
  19. SET /A NumLock    = "(%KeyLocks% & 0x20) / 0x20"
  20. SET /A ScrollLock = "(%KeyLocks% & 0x10) / 0x10"
  21.  
  22. :: Display the requested result(s)
  23. IF /I NOT "%~1"=="N" IF /I NOT "%~1"=="S" SET CapsLock
  24. IF /I NOT "%~1"=="C" IF /I NOT "%~1"=="S" SET NumLock
  25. IF /I NOT "%~1"=="C" IF /I NOT "%~1"=="N" SET ScrollLock
  26.  
  27. :: Default return code is the combined status
  28. SET /A KeyLocks = "%KeyLocks% & 0x70"
  29. :: If a single key lock was requested on the command
  30. :: line, the return code will be that key's status
  31. IF /I "%~1"=="C" SET KeyLocks=%CapsLock%
  32. IF /I "%~1"=="N" SET KeyLocks=%NumLock%
  33. IF /I "%~1"=="S" SET KeyLocks=%ScrollLock%
  34.  
  35. :: Return the requested key lock's status as "errorlevel"
  36. EXIT /B %KeyLocks%
  37.  
  38.  
  39. :Syntax
  40. ECHO KeyLocks.bat,  Version 1.20 for Windows NT 4 / 2000 / XP
  41. ECHO Return the status of the CapsLock, NumLock and ScrollLock keys
  42. ECHO.
  43. IF NOT "%OS%"=="Windows_NT" ECHO Usage:  KEYLOCKS  [ C ³ N ³ S ]
  44. IF     "%OS%"=="Windows_NT" ECHO Usage:  KEYLOCKS  [ C ^| N ^| S ]
  45. ECHO.
  46. ECHO Where:  C is for CapsLock status, N for NumLock, and S for ScrollLock.
  47. ECHO         By default the status for all three is displayed.
  48. ECHO.
  49. ECHO Notes:  The status of either the requested or all key lock(s) is displayed
  50. ECHO         on screen, and each is stored in the environment variables CapsLock,
  51. ECHO         NumLock and ScrollLock. The value of the environment variable
  52. ECHO         KeyLocks, which will also be returned as an "errorlevel", will
  53. ECHO         either be the status of the requested key lock (0=OFF, 1=ON) or
  54. ECHO         a combination of the three (C=64, N=32, S=16).
  55. ECHO         This batch file uses DEBUG to read the keyboard status.
  56. ECHO.
  57. ECHO Written by Rob van der Woude
  58. ECHO http://www.robvanderwoude.com
  59.  

page last modified: 2024-04-16; loaded in 0.0062 seconds