[Last Call] Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 1484
  • Last Modified:

Delphi - get status of Num Lock / Caps Lock / Scroll Lock lights (on or off)

This has been asked historically but I was wondering if there has been any progress on this issue in the intervening years.

What I need is a was of accurately displaying whether the Num/Caps/Scoll lock lights are on or off.

The reasons behind this is simple. I am writing a key stroke logger to be used with blind users. Rather than second guess what keystrokes they have pressed the logger displays what buttons they have actually pressed. However it is vital for their remote tutors to know whether the Num and Caps locks are on or off.

Can this be done reliably?

With thanks,

Ed
0
edhasted
Asked:
edhasted
  • 3
  • 2
1 Solution
 
mikelittlewoodCommented:
When your application loads you could run these procedures to check the state of the keys

procedure TForm1.FormCreate(Sender: TObject);
begin
  bNumLock := NumLock;
  bCapsLock := CapsLock;
  bScrollLock := ScrollLock;
end;

function LowOrderBitSet( Value: integer ): boolean;
begin
 Result := (Value and 1 > 0);
end;

function CapsLock : boolean;
begin
 if LowOrderBitSet(GetKeyState(VK_CAPITAL))then Result := True
 else Result := False;
end;

function NumLock : boolean;
begin
 if LowOrderBitSet(GetKeyState(VK_NUMLOCK))then Result := True
  else Result := False;
end;

function ScrollLock : boolean;
begin
 if LowOrderBitSet(GetKeyState(VK_SCROLL))then Result := True
 else Result := False;
end;

Then within the application write a key down function to capture changes in the key states.
Make sure KeyPreview property of mainform is set to True.

procedure TForm1.FormKeyDown(Sender: TObject; var Key: Word; Shift: TShiftState);
begin
 case key of
  VK_CAPITAL: bCapsLock := not bCapsLock;
  VK_NUMLOCK: bNumLock := not bNumLock;
  VK_SCROLL:  bScrollLock := not bScrollLock;
 end;
end;
0
 
mokuleCommented:
try this

var
  k: TKeyboardState;
begin
  GetKeyboardState(k);
  Edit1.Text := IntToStr(k[VK_NUMLOCK]);
  Edit2.Text := IntToStr(k[VK_CAPITAL]);
  Edit3.Text := IntToStr(k[VK_SCROLL]);
0
 
edhastedAuthor Commented:
Does LowOrderBitSet work in Delphi 5? If not is there a workaround - I'm getting an "Undeclared Idenfier" from the compiler.

Thanks,

Ed
0
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 
edhastedAuthor Commented:
Apologies - I copied over the code and left out the LowOrderBit function.

Ed
0
 
edhastedAuthor Commented:
Firstly many thanks to Mike Littlewood for his answer. I have set it up so three panels alter their colour from black to green depending on whether the lights are on or off. So far it works perfectly.

What I find strange reading the previous stabs at this problem was that the contributors felt that it could not be done.

This appears to crack the problem.

With many thanks,

Ed
0
 
mikelittlewoodCommented:
Glad I could help.
0

Featured Post

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

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

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