Solved

CTRL Down/Up ????

Posted on 1998-07-22
7
519 Views
Last Modified: 2008-02-01
Hi

I would like to know if it is possible to aks Delphi/Windows for the current state of the Shift, Alt and Ctrl keys. I emagine something like:

function GetCurrentShiftState: TShiftState;

...and the use it like...

...
SS : TShiftState;
...
SS := GetCurrentShiftState;
...

Exactly what I want to do is on a given time to be able find out if the Ctrl-button is up og down.
Can anyone please help me solve my problem.

Thanx - Steff
0
Comment
Question by:steff
[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
  • 4
  • 3
7 Comments
 
LVL 8

Expert Comment

by:ZifNab
ID: 1358423
Hi steff,

You can get the keyboard state in a timer with, say, 100 ms interval. I j

       VAR KS : TKeyboardState;
       ...
       GetKeyboardState(KS);
       IF Odd(KS[VK_CAPITAL]) THEN {caps lock is down}
       ... VK_NUMLOCK
       ... VK_SCROLL
       ... VK_INSERT

Zif.
0
 
LVL 8

Expert Comment

by:ZifNab
ID: 1358424
i believe you should use VK_Control.
0
 

Author Comment

by:steff
ID: 1358425
Hi ZifNab

I'm not sure I understand what you mean - would you please explain alittle more.

Steff
0
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!

 

Author Comment

by:steff
ID: 1358426
Hi ZifNab

I'm not sure I understand what you mean - would you please explain alittle more.

Steff
0
 
LVL 8

Accepted Solution

by:
ZifNab earned 50 total points
ID: 1358427
Hi steff,

You can check yourself each time if ctrl is pushed :

If you use this code :

function CtrlDown : Boolean;
var  State : TKeyboardState;
begin
  GetKeyboardState(State);  
  Result := ((State[vk_Control] And 128) <> 0);
end;

function ShiftDown : Boolean;
var  State : TKeyboardState;
begin
  GetKeyboardState(State);  
  Result := ((State[vk_Shift] and 128) <> 0);
end;

function AltDown : Boolean;
var  State : TKeyboardState;
begin
  GetKeyboardState(State);  
  Result := ((State[vk_Menu] and 128) <> 0);
end;

----
you can also use this code (now you will be notified when the ctrl button is pressed):

First: set the form's KeyPreview := true;

Then:

procedure TForm1.FormKeyDown(Sender: TObject; var Key: Word;
Shift: TShiftState);
begin
if (ssCtrl in shift) then
ShowMessage('Ctrl is pressed');
end;

---

Kind regards, Zif.

0
 

Author Comment

by:steff
ID: 1358428
Hi Zif

Thanx alot for your answer. What I actualy got out of it was that I got to know the function GetKeyboardState and the type TKeyboardState - I didn't know them before.

I guess that a general problem for me is that I dont know enough about Windows-programming. Can you tell me how I can get a good .hlp file on windows-programming?

Am I right when I say that it is alittle strange that online help in Delphi says nothing about the functions that it ofers to windows - e.g. GetKeyboardState?
If it had just metioned it I would have found it in there - but the again, you would not have had this question at the following points  ;-)

BTW I think that your second idea...

procedure TForm1.FormKeyDown(Sender: TObject; var Key: Word;
Shift: TShiftState);
begin
if (ssCtrl in shift) then
ShowMessage('Ctrl is pressed');
end;

.would not have worked for me, since I can not be sure that the user pushes the CTRL-key when my application is active. What if the user pushes the CTRL key down, holds it down and then activates my application. If he now does the thing that makes me want to know if CTRL is down, I would (by this method) know that the CTRL-key actualy is down !?!?!?

Thanx Steff
0
 
LVL 8

Expert Comment

by:ZifNab
ID: 1358429
Yep, delphi manual has some lacks... sorry, don't know od a very good .hlp file. Everything I know of these functions is because I somewhere found them on the net.

About ctrl-key. I don't know if second idea works, haven't tried it the way you propose it.

Zif.
0

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

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 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…
In this brief tutorial Pawel from AdRem Software explains how you can quickly find out which services are running on your network, or what are the IP addresses of servers responsible for each service. Software used is freeware NetCrunch Tools (https…
Add bar graphs to Access queries using Unicode block characters. Graphs appear on every record in the color you want. Give life to numbers. Hopes this gives you ideas on visualizing your data in new ways ~ Create a calculated field in a query: …
Suggested Courses
Course of the Month8 days, 8 hours left to enroll

615 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