Solved

How access key-states?

Posted on 1998-05-07
5
207 Views
Last Modified: 2010-04-06
Hi!

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

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!

Thanks!

//Daniel, Sweden
0
Comment
Question by:Dippen
  • 3
  • 2
5 Comments
 

Accepted Solution

by:
westphal earned 50 total points
ID: 1342815
use the windows function

GetKeyState(nVirtKey : integer)

where nVirtKey is one of the following value:
VK_LSHIFT      
VK_RSHIFT
VK_LCONTROL      
VK_RCONTROL
VK_LMENU      
VK_RMENU

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.
0
 

Expert Comment

by:westphal
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);
begin
 if GetKeyState(vk_LControl) and $80 = $80 then
   MessageDlg('left control key is down',mtInformation,[mbOk],0)
 else
   MessageDlg('left control key is up',mtInformation,[mbOk],0);
end;

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

0
 

Author Comment

by:Dippen
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?

0
 

Expert Comment

by:westphal
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)

0
 

Author Comment

by:Dippen
ID: 1342819
OK. Thanks for your help!
0

Featured Post

What Is Threat Intelligence?

Threat intelligence is often discussed, but rarely understood. Starting with a precise definition, along with clear business goals, is essential.

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…
Introduction The parallel port is a very commonly known port, it was widely used to connect a printer to the PC, if you look at the back of your computer, for those who don't have newer computers, there will be a port with 25 pins and a small print…
In this tutorial you'll learn about bandwidth monitoring with flows and packet sniffing with our network monitoring solution PRTG Network Monitor (https://www.paessler.com/prtg). If you're interested in additional methods for monitoring bandwidt…
This video demonstrates how to create an example email signature rule for a department in a company using CodeTwo Exchange Rules. The signature will be inserted beneath users' latest emails in conversations and will be displayed in users' Sent Items…

743 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

11 Experts available now in Live!

Get 1:1 Help Now