Jim Lawless' Blog


Safe Scripting with Scroll Lock and Caps Lock

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

Please note! If you're having difficulties compiling the C source code presented below, please see my post: Compiling C from the Command Line with Pelles C

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.



Views expressed in this blog are those of the author and do not necessary reflect those of the author's employer. Views expressed in the comments are those of the responding individual.

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


About Jim ...


Click **here**
to try out MailWrench;
a command-line SMTP /
SMTPS (Google Gmail)
mailer for Windows.


Follow me on Twitter

http://twitter.com/lawlessGuy


Recent Posts

A JavaScript REPL for Android Devices

MailSend is Free

My Blog Engine

The October 10th Bug

A Review of Kevin Mitnick's Book Ghost in the Wires

Spellbound by Web Programming

Backlinks to my Blog Posts

Play MP3 Files with Python on Windows


Random Posts

Envy

My Blog Engine

WSH JavaScript Includes

An Interview with the Author of the French Silk Assembler

Blog Posts by Category

Blogoversary

TAP : A Command Processor Library

An Interview with Game Developer James Hague

MailSend is Free

We've Moved!


Full List of Posts

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


Recent Posts from my Other Blog

Remembering Dr. San Guinary

Why Some Web Sites will go Dark on Jan 18th

SNL Superhero Skit

More Ruby Games

My Ruby Game Challenge Entry

Steal this Bookmarklet

Nerd Toys

Learn New Jargon, You Must

Spot the Wiebe

Tech Magazine Glory Days

Book Review : Paull Allen - Idea Man

A 90's Experiment in Online Systems - The U.S. West CommunityLink Service