How to change the keyboard state??

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

Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Turn this around :-)


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

    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];

WizKidAuthor Commented:
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)

Yes, just set the 0040:0097 adress instead of reading it...?
Cloud Class® Course: Amazon Web Services - Basic

Are you thinking about creating an Amazon Web Services account for your business? Not sure where to start? In this course you’ll get an overview of the history of AWS and take a tour of their user interface.

WizKidAuthor Commented:
Tried your code already but it doesn't work on Delphi 3.0 (32 bit). It just can't compile...

Any other way?

try this... (from UDDF)

procedure TMyForm.Button1Click(Sender: TObject);
    KeyState  :  TKeyboardState;
    if (KeyState[VK_NUMLOCK] = 0) then
      KeyState[VK_NUMLOCK] := 1
      KeyState[VK_NUMLOCK] := 0;

for caps lock substitute VK_CAPITAL for VK_NUMLOCK.

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
WizKidAuthor Commented:
It worked! Thanks Pal!
No problem, glad I could help.

Good luck with it!
c.u. Zif.
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.