Solved

CTRL Down/Up ????

Posted on 1998-07-22
7
511 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
Comment Utility
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
Comment Utility
i believe you should use VK_Control.
0
 

Author Comment

by:steff
Comment Utility
Hi ZifNab

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

Steff
0
Threat Intelligence Starter Resources

Integrating threat intelligence can be challenging, and not all companies are ready. These resources can help you build awareness and prepare for defense.

 

Author Comment

by:steff
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
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

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

Suggested Solutions

Title # Comments Views Activity
delphi custom sort exception 6 109
How to fill array with TArray.Create? 14 68
Delphi cmd execution 6 40
Intraweb download file link ? 1 88
In this tutorial I will show you how to use the Windows Speech API in Delphi. I will only cover basic functions such as text to speech and controlling the speed of the speech. SAPI Installation First you need to install the SAPI type library, th…
Introduction Raise your hands if you were as upset with FireMonkey as I was when I discovered that there was no TListview.  I use TListView in almost all of my applications I've written, and I was not going to compromise by resorting to TStringGrid…
It is a freely distributed piece of software for such tasks as photo retouching, image composition and image authoring. It works on many operating systems, in many languages.
Illustrator's Shape Builder tool will let you combine shapes visually and interactively. This video shows the Mac version, but the tool works the same way in Windows. To follow along with this video, you can draw your own shapes or download the file…

771 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

10 Experts available now in Live!

Get 1:1 Help Now