Jim Lawless' Blog


Safe Scripting with Scroll Lock and Caps Lock

Originally published on: Sat, 09 May 2009 16:38:50 +0000

I often throw caution to the wind and change items in my startup scripts ... changes that could ( and do ) cause issues during my initialization process. I have many recovery options available, but I still use an old technique that allows me to alter the behavior of my scripts by using the Caps Lock or Scroll Lock keys.

Ages ago in the MS-DOS days, I wrote a little program that would check the state of the shift-keys ... the SHIFT, ALT, CTRL ...etc. keys. I don't believe that the task was terribly complex, but I may be remembering incorrectly.

If memory serves, I had simply cobbled together a little .COM file in DEBUG to check for the shift state of the Caps Lock key by looking at a flag in the BIOS Data Area at segment 40h. After performing the AND to determine if the key was on ... I would exit via INT 21h function 4C, passing back a zero if the key was off or a one if the key was on. This permitted me to call the .COM utility in a batch file so that I could then check the ERRORLEVEL flag for a one.

At the time I wrote this utility, part of my startup was on a page on a server, so I could not easily recover if the startup script managed to hang. I opted to check the Caps Lock key and would then bypass all of my custom startup commands if present.

This technique saved my bacon exactly once, but the time saved in being able to recover so quickly made the time I had spent writing the utility worthwhile.

I've created a Windows counterpart to the utility that now calls GetKeyState() to check for either Scroll Lock or Caps Lock ( depending on whether you've specified "-scroll" or "-caps" on the command-line. )

checklock.c


// License: MIT / X11
// Copyright (c) 2009 by James K. Lawless
// jimbo@radiks.net http://www.radiks.net/~jimbo
// http://www.mailsend-online.com
//
// Permission is hereby granted, free of charge, to any person
// obtaining a copy of this software and associated documentation
// files (the "Software"), to deal in the Software without
// restriction, including without limitation the rights to use,
// copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the
// Software is furnished to do so, subject to the following
// conditions:
//
// The above copyright notice and this permission notice shall be
// included in all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
// EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
// OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
// NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
// HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
// WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
// FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
// OTHER DEALINGS IN THE SOFTWARE.

// check the state of the Scroll Lock or Caps Lock keys. Return a
// 1 if the requested key is active/on.

#include <windows.h>
#include <stdio.h>
#include <string.h>

#pragma comment(lib,"user32.lib")

int main(int argc,char **argv) {
   int i,key;
   if(argc<2) {
      fprintf(stderr,"Syntax:\n\t%s -scroll\nor\n\t%s -caps\n",
         argv[0],argv[0]);
      return 1;
   }
   if(!stricmp(argv[1],"-scroll"))
      key=VK_SCROLL;
   else
      key=VK_CAPITAL;
   return (GetKeyState(key)&1);
}

Here's a sample batch file that shows how to use the utilty:


@echo off
checklock -scroll
if errorlevel 1 goto scrollhandler

checklock -caps
if errorlevel 1 goto capshandler

echo Neither of the keys are on
goto end


:capshandler
echo Caps Lock is on.
goto end


:scrollhandler
echo Scroll Lock is on.
goto end


:end

The compiled EXE for checklock can be found here with the source code:

http://www.mailsend-online.com/wp/checklock.zip

Unless otherwise noted, all code and text entries are Copyright ©2009 by James K. Lawless.

del_icio_us Save to del.icio.us
stumbleupon Save to StumbleUpon
digg Digg it
reddit Save to Reddit
facebook Share on Facebook
twitter Share on Twitter
aolfav More bookmarks



Previous post: RSS feed processing with AWK
Next post:Throwaway Software: HangUp


Search this Blog (and site)

Search this Site with PicoSearch


Subscribe to this Blog

 Subscribe!


Contact Me

Email: jimbo@radiks.net


Follow me on Twitter

http://twitter.com/lawlessGuy


Recent Posts

Mad Schemes : Learning Lisp via SICP

Auto Save Clipboard Images Redux

Extending SpiderMonkey JavaScript on Windows

Rhino JavaScript to EXE with launch4j

Compiling Rhino JavaScript to Java

Directory Traversal in Rhino JavaScript

Taking Shape

We've Moved!


Popular Posts

A Command-Line MP3 Player for Windows

Auto Save Images from the Clipboard

Java in a Windows EXE with launch4j

An Interview with Tom Zimmer: Forth System Developer

Setting Windows Console Text Colors in C


Random Posts

We've Moved!

Hiding Batch File Console Windows

Java in a Windows EXE with launch4j

Removing IE Popups in C

Obfuscated C

WSH2EXE - The Final Chapter

CP/M Days

Windows Text to Speech in WSH JavaScript

A Simple Parser for a Small Command Line Interface

Jim Butterfield : The Commodore Guru


Full List of Posts

http://www.mailsend-online.com/bloglist.htm


Blogroll

MicroISV on a Shoestring
DadHacker
The Bottom Feeder
Writin' That Code!
The Recursive ISV
The Thomsen Blog
Prototypically Speaking
The Reinvigorated Programmer