Solved

check for shift/control

Posted on 2001-07-29
10
178 Views
Last Modified: 2012-05-07
How do I check if the shift or control key is down?
Currently, I set a variable under onkeydown & unset it under onkeyup; I'd rather check it programmatically at the time the event is called (basically, I want people to be able to ctrl-click or shift-click on buttons/tlistview headers)
0
Comment
Question by:delphiexpert
  • 3
  • 3
  • 2
  • +1
10 Comments
 
LVL 5

Expert Comment

by:alanwhincup
Comment Utility
Try this:

function HighOrderBitSet(W : Word) : Boolean;
const
  HighOrderBit = 15;
type
  BitSet = set of 0..15;
begin
  HighOrderBitSet := (HighOrderBit in BitSet(W));
end;

procedure TForm1.Button1Click(Sender: TObject);
begin
  if HighOrderBitSet(Word(GetKeyState(VK_CONTROL))) = True then
    Label1.Caption := 'Shift is Down'
  else
    Label1.Caption := 'Shift is UP';
  if HighOrderBitSet(Word(GetKeyState(VK_SHIFT))) = True then
    Label2.Caption := 'CTRL is Down'
  else
    Label2.Caption := 'CTRL is UP';
end;

Other Virtual Keys you may want include:

VK_LSHIFT
VK_RCONTROL

Cheers,

Alan

0
 
LVL 27

Expert Comment

by:kretzschmar
Comment Utility
use then onMouseUp or OnMouseDown-events,
there is a ShiftState provided like

if ssControl in Shift then ... //ControlKey pressed
or
if ssShift in Shift then ... //Shift pressed
0
 
LVL 20

Expert Comment

by:Madshi
Comment Utility
Instead of this:

if HighOrderBitSet(Word(GetKeyState(VK_CONTROL))) = True then

You can directly write:

if HighOrderBitSet(Word(GetKeyState(VK_CONTROL))) then

Furthermore to make it shorter you can remove the "HighOrderBitSet" function and directly write:

if GetKeyState(VK_CONTROL) and $8000 <> 0 then

Then the label captions for control and shift are not swapped in Alan's example code.

Regards, Madshi.
0
 
LVL 1

Author Comment

by:delphiexpert
Comment Utility
Well, thanks Alan & madshi.. both your answers are fine; tho madshi's is a bit more compact...

guess I should give alan the points?
0
 
LVL 20

Expert Comment

by:Madshi
Comment Utility
Okay for me.
0
Do You Know the 4 Main Threat Actor Types?

Do you know the main threat actor types? Most attackers fall into one of four categories, each with their own favored tactics, techniques, and procedures.

 
LVL 5

Expert Comment

by:alanwhincup
Comment Utility
Cheers, but you can give Madshi the points if you want to seeing he gave the final answer... Choice is up to you.
0
 
LVL 20

Expert Comment

by:Madshi
Comment Utility
But Alan was first, I just gave some comments. So I think Alan has earned the points more...   :-)
0
 
LVL 27

Expert Comment

by:kretzschmar
Comment Utility
hehe, if no one wants the points ... :-))
0
 
LVL 5

Accepted Solution

by:
alanwhincup earned 77 total points
Comment Utility
Ok...I'll take them, Cheers Madshi... :-)
0
 
LVL 1

Author Comment

by:delphiexpert
Comment Utility
There you go! :)
0

Featured Post

What Security Threats Are You Missing?

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

Join & Write a Comment

Objective: - This article will help user in how to convert their numeric value become words. How to use 1. You can copy this code in your Unit as function 2. than you can perform your function by type this code The Code   (CODE) The Im…
In my programming career I have only very rarely run into situations where operator overloading would be of any use in my work.  Normally those situations involved math with either overly large numbers (hundreds of thousands of digits or accuracy re…
Get a first impression of how PRTG looks and learn how it works.   This video is a short introduction to PRTG, as an initial overview or as a quick start for new PRTG users.
This video explains how to create simple products associated to Magento configurable product and offers fast way of their generation with Store Manager for Magento tool.

762 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

15 Experts available now in Live!

Get 1:1 Help Now