Solved

How to change the keyboard state??

Posted on 1997-12-13
7
172 Views
Last Modified: 2010-04-04
I need the code to change the NumLock Key state by clicking on a button of my application

Have tried using the following code but it does not seem to work
keybd_event(VK_NUMLOCK, 0, 0, 0);
keybd_event(VK_NUMLOCK, 0, KEYEVENTF_KEYUP, 0);

The above code works when substituted with VK_CAPITAL or VK_INSERT but not VK_NUMLOCK. Anyone knows why?

By the way, I took the code from another person's answer
(Ermac I think...) and he had the same problem as me








0
Comment
Question by:WizKid
  • 3
  • 2
  • 2
7 Comments
 
LVL 1

Expert Comment

by:miv
ID: 1353518
Turn this around :-)

------------------------------------

FUNCTION GetKeybStatus : TKeybStatusSet;
VAR
  KeybAbs: Byte absolute $0040:$0097;

  BEGIN
    Result:= [];
    IF (KeybAbs and 4)=4 THEN
      Result:= [ksCapsLock];
    IF (KeybAbs and 2)=2 THEN
      Result:= Result+[ksNumLock];
    IF (KeybAbs and 1)=1 THEN
      Result:= Result+[ksScrollLock];
  END;


0
 

Author Comment

by:WizKid
ID: 1353519
I think you got me wrong....
The code that you gave me is only used for detecting the status of the keyboard but what I wanted was to set the them (Eg. Toggle the Num Lock key)


0
 
LVL 1

Expert Comment

by:miv
ID: 1353520
Yes, just set the 0040:0097 adress instead of reading it...?
0
Free Tool: Postgres Monitoring System

A PHP and Perl based system to collect and display usage statistics from PostgreSQL databases.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 

Author Comment

by:WizKid
ID: 1353521
Tried your code already but it doesn't work on Delphi 3.0 (32 bit). It just can't compile...

Any other way?
0
 
LVL 8

Accepted Solution

by:
ZifNab earned 50 total points
ID: 1353522

try this... (from UDDF)

procedure TMyForm.Button1Click(Sender: TObject);
  Var
    KeyState  :  TKeyboardState;
  begin
    GetKeyboardState(KeyState);
    if (KeyState[VK_NUMLOCK] = 0) then
      KeyState[VK_NUMLOCK] := 1
    else
      KeyState[VK_NUMLOCK] := 0;
    SetKeyboardState(KeyState);
  end;

for caps lock substitute VK_CAPITAL for VK_NUMLOCK.
0
 

Author Comment

by:WizKid
ID: 1353523
It worked! Thanks Pal!
0
 
LVL 8

Expert Comment

by:ZifNab
ID: 1353524
No problem, glad I could help.

Good luck with it!
c.u. Zif.
0

Featured Post

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Creating an auto free TStringList The TStringList is a basic and frequently used object in Delphi. On many occasions, you may want to create a temporary list, process some items in the list and be done with the list. In such cases, you have to…
Have you ever had your Delphi form/application just hanging while waiting for data to load? This is the article to read if you want to learn some things about adding threads for data loading in the background. First, I'll setup a general applica…
In a recent question (https://www.experts-exchange.com/questions/29004105/Run-AutoHotkey-script-directly-from-Notepad.html) here at Experts Exchange, a member asked how to run an AutoHotkey script (.AHK) directly from Notepad++ (aka NPP). This video…

830 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question