<meta name='google-adsense-platform-account' content='ca-host-pub-1556223355139109'/> <meta name='google-adsense-platform-domain' content='blogspot.com'/> <!-- data-ad-client=ca-pub-2799935330569724 --> <!-- --><style type="text/css">@import url(https://www.blogger.com/static/v1/v-css/navbar/3334278262-classic.css); div.b-mobile {display:none;} </style> </head><body><script type="text/javascript"> function setAttributeOnload(object, attribute, val) { if(window.addEventListener) { window.addEventListener('load', function(){ object[attribute] = val; }, false); } else { window.attachEvent('onload', function(){ object[attribute] = val; }); } } </script> <div id="navbar-iframe-container"></div> <script type="text/javascript" src="https://apis.google.com/js/platform.js"></script> <script type="text/javascript"> gapi.load("gapi.iframes:gapi.iframes.style.bubble", function() { if (gapi.iframes && gapi.iframes.getContext) { gapi.iframes.getContext().openChild({ url: 'https://www.blogger.com/navbar.g?targetBlogID\x3d24422183\x26blogName\x3dTech+at+the+speed+of+Warp\x26publishMode\x3dPUBLISH_MODE_BLOGSPOT\x26navbarType\x3dBLACK\x26layoutType\x3dCLASSIC\x26searchRoot\x3dhttps://nccwarp9.blogspot.com/search\x26blogLocale\x3den_US\x26v\x3d2\x26homepageUrl\x3dhttp://nccwarp9.blogspot.com/\x26vt\x3d2845469267604558330', where: document.getElementById("navbar-iframe-container"), id: "navbar-iframe" }); } }); </script>
0 comments | Wednesday, November 15, 2006

Ever since I first used WinAmp plugin to turn off/on my keyboard lights I wanted to reproduce that effect. finally some time ago I found out how its done and here's how.

The image on the left is from KeyLED 0.9.3 plugin for Samurize that I made.

My programing language of choice is Delphi from Borland since its first birth as Pascal for Windows and later as Delphi 1. Never needed to change so code will be for delphi but it is such a small part of code that it could be translated to any programing language with relative ease.

The trick with controlling your keyboard lights is a very old one. Computer communicates with keyboard on I/O ports 60h which is data port, and with port 64h which is Command/Status port of keyboard. By writing to port 60h we bypass the system and change the LED indicator for eg. Caps Lock to off even if Caps Lock is on but without changing the Caps Lock status. Caps Lock stays on even if LED is off. This enables us to play with keyboard lights without any worries.

Port 64h is used to detect if keyboard is ready. We do that by waiting that status of port 64h becomes 0. Then we prepare keyboard to change keyboard light by writing EDh to port 60h and then wait port 64h to once again become 0.

The waiting for 64h port to become 0 is a safety measure that can be bypassed but I would recommend against it. You would still be able to change the LED state but by waiting for ready state you remove some unwanted artifacts like missing keystrokes etc.

After we prepare the keyboard we can turn off and on Keyboard lights. By writing to port 60h combinations of

  1. Caps Lock = 4 or second bit = 1 (00000100)
  2. Num Lock = 2 or first bit = 1 (00000010)
  3. Scroll Lock = 1 or zero bit = 1 (00000001)

we can selectively turn on and off lights on the keyboard without changing caps lock or numlock status.

This is all just theory but how it is applied in reality? Well, Windows XP introduced restrictive methods for accessing hardware ports so we need a library that can to that for us. In the good old Dos there was Int 9 and direct access to ports but not anymore. Library that I use is Inpout32.dll. It is open source so if you really like it you could incorporate it directly into your applications removing the need for "one additional file to carry around".

First we need to create functions connecting to DLL by

function Out32(wAddr:word;bOut:byte):byte; stdcall; external 'inpout32.dll';
function Inp32(wAddr:word):byte; stdcall; external 'inpout32.dll';

With functions for port access defined we can now proceed with writing and waiting and writing and waiting by:

PROCEDURE Set_keyboard_lights(n,c,s : boolean);
var
bErr:byte;
val : byte;
BEGIN
val := 0;
IF s THEN inc(val);
IF n THEN inc(val,2);
IF c THEN inc(val,4);
repeat until (inp32($64) AND 2)=0;
bErr:=(Out32($60,$ED));
repeat until (inp32($64) AND 2)=0;
bErr:=(Out32($60,val));
END;

Procedure presented will take three parameters that can be either true of false; NumLock Status, CapsLock Status and NumLock Status.

This small snippet is all you need to control your Keyboard Lights. Have Fun!!

Labels: , ,

0 Comments:

Post a Comment

<< Home