How access key-states?

Posted on 1998-05-07
Last Modified: 2010-04-06

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
Question by:Dippen
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 2

Accepted Solution

westphal earned 50 total points
ID: 1342815
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.

Expert Comment

ID: 1342816
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);


Author Comment

ID: 1342817
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?


Expert Comment

ID: 1342818
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)


Author Comment

ID: 1342819
OK. Thanks for your help!

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

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…
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…
In this video we outline the Physical Segments view of NetCrunch network monitor. By following this brief how-to video, you will be able to learn how NetCrunch visualizes your network, how granular is the information collected, as well as where to f…
Michael from AdRem Software outlines event notifications and Automatic Corrective Actions in network monitoring. Automatic Corrective Actions are scripts, which can automatically run upon discovery of a certain undesirable condition in your network.…
Suggested Courses
Course of the Month8 days, 8 hours left to enroll

617 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