[Webinar] Streamline your web hosting managementRegister Today

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 191
  • Last Modified:

check for shift/control

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
delphiexpert
Asked:
delphiexpert
  • 3
  • 3
  • 2
  • +1
1 Solution
 
alanwhincupCommented:
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
 
kretzschmarCommented:
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
 
MadshiCommented:
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
2018 Annual Membership Survey

Here at Experts Exchange, we strive to give members the best experience. Help us improve the site by taking this survey today! (Bonus: Be entered to win a great tech prize for participating!)

 
delphiexpertAuthor Commented:
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
 
MadshiCommented:
Okay for me.
0
 
alanwhincupCommented:
Cheers, but you can give Madshi the points if you want to seeing he gave the final answer... Choice is up to you.
0
 
MadshiCommented:
But Alan was first, I just gave some comments. So I think Alan has earned the points more...   :-)
0
 
kretzschmarCommented:
hehe, if no one wants the points ... :-))
0
 
alanwhincupCommented:
Ok...I'll take them, Cheers Madshi... :-)
0
 
delphiexpertAuthor Commented:
There you go! :)
0

Featured Post

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

  • 3
  • 3
  • 2
  • +1
Tackle projects and never again get stuck behind a technical roadblock.
Join Now