How access key-states?


Here's an easy one... How can I check whether Ctrl, Alt or L/R Shift is pressed in an IF-statement. Like:

if (bla bla bla) then begin
  CtrlPressed := True;

In good, old DOS (may she rest in peace) there was a particular address where some bits indicated the key-states, but hey!... it's the 90ties now!


//Daniel, Sweden
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.

use the windows function

GetKeyState(nVirtKey : integer)

where nVirtKey is one of the following value:

the return value is
if the high-order bit is 1, the key is down; otherwise, it is up. If the low-order bit is 1, the key is toggled. A key, such as the CAPS LOCK key, is toggled if it is turned on. The key is off and untoggled if the low-order bit is 0. A toggle key's indicator light (if any) on the keyboard will be on when the key is toggled, and off when the key is untoggled.

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
here is an example:
if you have a form with 2 buttons you can test the GetKeyState with the following code:

procedure TForm1.Button1Click(Sender: TObject);
 if GetKeyState(vk_LControl) and $80 = $80 then
   MessageDlg('left control key is down',mtInformation,[mbOk],0)
   MessageDlg('left control key is up',mtInformation,[mbOk],0);

procedure TForm1.Button2Click(Sender: TObject);
 if GetKeyState(vk_NumLock) and $1 = $1 then
   MessageDlg('NumLock is toggled',mtInformation,[mbOk],0)
   MessageDlg('NumLock is untoggled',mtInformation,[mbOk],0);

DippenAuthor Commented:
Hi, westphal!

I can't get your first procedure to work using the VK_LCONTROL constant, but if I change it to VK_CONTROL it works for both Ctrl keys. Is it because I use Win95? And if it IS because of Win95, is there any workaround in case I want to distinguish between Left and right Shift or Ctrl?

you are right, win95 does'nt know the difference between right and left :) but you can use in win95 a direct access to bios or harware (old way in dos program's)

DippenAuthor Commented:
OK. Thanks for your help!
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.