• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 595
  • Last Modified:

Keyboard Lights

I wonder how i can get the "Keyboard Ligths"
(NUM LOCK/CAPS LOCK/SCREEN LOCK) to all be turned on...
0
TeChNiCh
Asked:
TeChNiCh
1 Solution
 
DragonSlayerCommented:
procedure ToggleKey(VirtKey: Integer; SetOn: Boolean);
var
  IsToggledOn: Boolean;
begin
  IsToggledOn := (GetKeyState(VirtKey) and 1) = 1;
  if IsToggledOn <> SetOn then
  begin
    keybd_event(VirtKey, 0, 0, 0);
    keybd_event(VirtKey, 0, KEYEVENTF_KEYUP, 0);
  end;
end;


The above procedure will check if a key is toggled on/off.

To set CAPS LOCK to ON:

ToggleKey(VK_CAPITAL, True);
and to set it to OFF again:
ToggleKey(VK_CAPITAL, False);

NUM LOCK's virtual key is VK_NUMLOCK
SCROLL LOCK's virtual key is VK_SCROLL



DragonSlayer.
0
 
sundayboysCommented:
function IsNumLockOn: boolean;
var KeyState: TKeyboardState;
begin
GetKeyboardState(KeyState);
Result := (KeyState[VK_NUMLOCK] and 1) = 1;
end;

procedure NumLockOn;
var KeyState: TKeyboardState;
begin
GetKeyboardState(KeyState);
KeyState[VK_NUMLOCK] := $01;
SetKeyboardState(KeyState);
end;

procedure NumLockOff;
var KeyState: TKeyboardState;
begin
GetKeyboardState(KeyState);
KeyState[VK_NUMLOCK] := $00;
SetKeyboardState(KeyState);
end;

// these tips can use with other keys ie. VK_CAPITAL, VK_SCROLL

0
 
DragonSlayerCommented:
SundayBoys,

From the Win32 API help file:

Because the SetKeyboardState function alters the input state of the calling thread and not the global input state of the system, an application cannot use SetKeyboardState to set the NUM LOCK, CAPS LOCK, or SCROLL LOCK indicator lights on the keyboard.


DragonSlayer.
0
Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

 
sundayboysCommented:
DragpmSlayer?F
  try my code,it can change NUMLOCK lights.my OS is win98se.
  Don't  always trust Microsoft!
0
 
DragonSlayerCommented:
:)
0
 
sundayboysCommented:
:-)
0
 
robert_marquardtCommented:
I think the code does not only sets the numlock light but also the numlock state!
The problem is that Windows now has a virtual system keyboard and you cannot fiddle with the keyboard controller anymore. It may be that your oly keyboard is USB which is not controlled by the keyboard controller.
0
 
MadshiCommented:
We needed to control the LEDs (including the state) in win9x in our firm, too. We worked hard on that and finally found a method which works more or less reliable. But it works only in connection with our little keyboard driver.

We also tried the SetKeyboardState stuff, of course, but somehow it made problems. I don't remember exactly, what the problem was. I think, it was not reliable, on some keyboards it did work, on others it did not.

Regards, Madshi.
0
 
Dennis9Commented:
hi.
Something like this?


procedure TurnAllOn;
var
  keys: TKeyboardState;
begin
  GetKeyboardState( keys );
  keys[VK_NUMLOCK] := 1;  {Num Lock}
  keys[VK_CAPITAL] := 1;  {Caps Lock}
  keys[VK_SCROLL]  := 1;  {Scroll Lock}
  SetKeyboardState(keys);
end;

procedure TurnAllOff;
var
  keys: TKeyboardState;
begin
  GetKeyboardState( keys );
  keys[VK_NUMLOCK] := 0;  {Num Lock}
  keys[VK_CAPITAL] := 0;  {Caps Lock}
  keys[VK_SCROLL]  := 0;  {Scroll Lock}
  SetKeyboardState(keys);
end;


Dennis
0
 
SpideyModCommented:
Administrative Action - Force Accepted

SpideyMod
Community Support Moderator @Experts Exchange
0

Featured Post

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now