Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

CTRL Down/Up ????

Posted on 1998-07-22
7
Medium Priority
?
522 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
  • 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
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.

 

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 150 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

Technology Partners: 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…
Creating an auto free TStringList The TStringList is a basic and frequently used object in Delphi. On many occasions, you may want to create a temporary list, process some items in the list and be done with the list. In such cases, you have to…
This video shows how to quickly and easily deploy an email signature for all users in Office 365 and prevent it from being added to replies and forwards. (the resulting signature is applied on the server level in Exchange Online) The email signat…
Screencast - Getting to Know the Pipeline
Suggested Courses

886 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