Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

check for shift/control

Posted on 2001-07-29
10
Medium Priority
?
188 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
[X]
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
  • 3
  • 2
  • +1
10 Comments
 
LVL 5

Expert Comment

by:alanwhincup
ID: 6332527
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
ID: 6332971
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
ID: 6333233
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
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 
LVL 1

Author Comment

by:delphiexpert
ID: 6337174
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
ID: 6339769
Okay for me.
0
 
LVL 5

Expert Comment

by:alanwhincup
ID: 6339815
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
ID: 6341898
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
ID: 6342195
hehe, if no one wants the points ... :-))
0
 
LVL 5

Accepted Solution

by:
alanwhincup earned 308 total points
ID: 6345481
Ok...I'll take them, Cheers Madshi... :-)
0
 
LVL 1

Author Comment

by:delphiexpert
ID: 6350738
There you go! :)
0

Featured Post

Industry Leaders: 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

The uses clause is one of those things that just tends to grow and grow. Most of the time this is in the main form, as it's from this form that all others are called. If you have a big application (including many forms), the uses clause in the in…
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…
In this video, Percona Solution Engineer Rick Golba discuss how (and why) you implement high availability in a database environment. To discuss how Percona Consulting can help with your design and architecture needs for your database and infrastr…
Want to learn how to record your desktop screen without having to use an outside camera. Click on this video and learn how to use the cool google extension called "Screencastify"! Step 1: Open a new google tab Step 2: Go to the left hand upper corn…
Suggested Courses

598 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