Solved

CTRL Down/Up ????

Posted on 1998-07-22
7
512 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
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 

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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

A lot of questions regard threads in Delphi.   One of the more specific questions is how to show progress of the thread.   Updating a progressbar from inside a thread is a mistake. A solution to this would be to send a synchronized message to the…
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…
Windows 10 is mostly good. However the one thing that annoys me is how many clicks you have to do to dial a VPN connection. You have to go to settings from the start menu, (2 clicks), Network and Internet (1 click), Click VPN (another click) then fi…
This is used to tweak the memory usage for your computer, it is used for servers more so than workstations but just be careful editing registry settings as it may cause irreversible results. I hold no responsibility for anything you do to the regist…

911 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

22 Experts available now in Live!

Get 1:1 Help Now