views:

129

answers:

2

I have a single pc with multiple keyboards, all the capslock lights turn on/off synchronously. (so if one user/keyboard turns on the capslock, everybody types in caps)

I was thinking about saving each keyboard's capslock status in flags but I just realized that the lights won't correspond for each user/keyboard's capslock status.
I just want to know if the capslock light can be turned on/off independently. I'm already planning on disabling the capslock (since I don't really like that key), but in case the client wants to use it I can either find a way to do it or tell them it's impossible.

+2  A: 

try

http://www.codeguru.com/Cpp/W-P/system/keyboard/article.php/c2825

a bit involved, but doable :)

Keith Nicholas
whoa. That's a lot of work. Thanks for answering. Does this work on XP? I'm gonna try to make this in delphi and modify it from there.
Dian
yeah, but you will be ninja :)
Keith Nicholas
Which part qualifies for "ninja" status? Translating the C code to Delphi, or adapting the code to work in Windows XP? If the former, then you need to rethink your rating system because that code's pretty straightforward. If the latter, then could you describe some of the hurdles Dian and others would face to make the code work in later Windows versions?
Rob Kennedy
ninja is the functionality not so much the code :)
Keith Nicholas
A: 

you can Programmatically Get and Set the State of the CapsLock Keys using Keybd_Event function
try this:

var
    KeyState: TKeyboardState;
 begin
    GetKeyboardState(KeyState) ;
   if (KeyState[VK_CAPITAL] = 0) then
    begin  //simulate key down
      Keybd_Event(VK_CAPITAL, 1, KEYEVENTF_EXTENDEDKEY or 0, 0) ;
      Keybd_Event(VK_CAPITAL, 1, KEYEVENTF_EXTENDEDKEY or KEYEVENTF_KEYUP, 0) ;
    end
    else
    begin  //simulate key up
      Keybd_Event(VK_CAPITAL, 0, KEYEVENTF_EXTENDEDKEY or 0, 0) ;
      Keybd_Event(VK_CAPITAL, 0, KEYEVENTF_EXTENDEDKEY or KEYEVENTF_KEYUP, 0) ;
    end;

if you REALLY want to disable a key(which i dont recommend) you can use this library called BlockKeys(i found BlockKeys library at http://www.experts-exchange.com/Programming/Languages/Pascal/Delphi/Q_21504230.html ):

library BlockKeys;

uses
  Windows,
  Messages;

{$R *.RES}

var
hKeyHook: THandle = 0;
Hooked: Boolean = False;

function HookFunc(Code, VirtualKey, KeyStroke: Integer): Integer; stdcall;
begin
if  VirtualKey in [VK_TAB, VK_CONTROL, VK_MENU, VK_ESCAPE, VK_F1] then
  Result := 32
  else
  Result := CallNextHookEx(hKeyHook, Code, VirtualKey, KeyStroke);
end;


function StartHook: Boolean; export;
begin
Result := False;
if Hooked then
  begin
  Result := True;
  Exit;
  end;

hKeyHook := SetWindowsHookEx(WH_KEYBOARD, HookFunc, hInstance, 0);
if hKeyHook <> 0 then
  begin
  Result := True;
  Hooked := True;
  end;
end;


function StopHook: Boolean; export;
begin
if Hooked then
  Result := UnhookWindowsHookEx(hKeyHook)
  else
  Result := True;
if Result then Hooked := False;
end;

exports
  StartHook,
  StopHook;

begin

end.

hope this helps

Omair Iqbal
This isn't controlling the state of the lights. It's manipulating the OS caps-lock state by pretending to press a key. It's ignoring the issue that there are multiple keyboards, each with its own set of lights. You could have caps lock set on one keyboard while the other is normal.
Rob Kennedy
@rob thanks for pointing out,should i remove my answer?
Omair Iqbal