Solved

Setting the keyboard state

Posted on 1997-12-12
3
170 Views
Last Modified: 2010-04-04
I need to enable the Num Lock, Caps Lock and Insert/Overwrite Key by just clicking on a button

I have tried using the SetKeyBoardState function but found that it does not do this
Is there any way to do this??

Please Help....
Thanks in Advance
0
Comment
Question by:ermac
  • 2
3 Comments
 
LVL 1

Accepted Solution

by:
zac earned 70 total points
ID: 1353445
SetKeyBoardState only changes the state of the keyboard for the thread from which it was called, consequently you cannot change the global keyboard state using this function.

There is a fudge however, simply send the keystrokes themselves to windows the code I use is below:

  keybd_event(VK_NUMLOCK, 0, 0, 0);
  keybd_event(VK_NUMLOCK, 0, KEYEVENTF_KEYUP, 0);

You need both calls, one for the press event and one for the key up event.  This code will toggle the NUMLOCK status of the keyboard.

If you need to set a particular state, then I suggest you look at the keyboard state using GetKeyState and then make the changes where necessary.


I hope this helps.


PS Virtual key codes you will need:

NUMLOCK  -  VK_NUMLOCK
CAPSLOCK -  VK_CAPITAL
Insert           -  VK_INSERT

However I believe the insert/ovewrite toggle is implemented in each application and therefore you might have to give the application input focus before sending the commands.
0
 

Author Comment

by:ermac
ID: 1353446
Great! It works except for the Num Lock key...

I've even tried it using a brand new project
Both the Caps Lock and the Insert/Overwrite Key works perfectly but not the Num Lock key
Why??

Increased Points to 75


0
 
LVL 1

Expert Comment

by:zac
ID: 1353447
They code fragment I supplied was code that I tested ie with the NUMLOCK key - it worked!  However I use windows NT for all my development.
0

Featured Post

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

This article explains how to create forms/units independent of other forms/units object names in a delphi project. Have you ever created a form for user input in a Delphi project and then had the need to have that same form in a other Delphi proj…
Hello everybody This Article will show you how to validate number with TEdit control, What's the TEdit control? TEdit is a standard Windows edit control on a form, it allows to user to write, read and copy/paste single line of text. Usua…
Sending a Secure fax is easy with eFax Corporate (http://www.enterprise.efax.com). First, Just open a new email message.  In the To field, type your recipient's fax number @efaxsend.com. You can even send a secure international fax — just include t…
Polish reports in Access so they look terrific. Take yourself to another level. Equations, Back Color, Alternate Back Color. Write easy VBA Code. Tighten space to use less pages. Launch report from a menu, considering criteria only when it is filled…

708 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

Need Help in Real-Time?

Connect with top rated Experts

16 Experts available now in Live!

Get 1:1 Help Now