Sign in Join

Live chat for Free Pascal, Lazarus, Delphi and every Pascal programmer.

Mailing lists / fpc-pascal / 2017-02

KeyDown routine and Ctrl+Q

7 messages · 4 people · –

Reply by email Subscribe to this list Your subscription

Read-only archive. Use j and k to move between messages.

Jürgen Hestermann

I use a KEYDOWN routine in my main TFORM to check for the key Ctrl+Q:

-----------------------------------------------------------------
procedure TForm1.FormKeyDown(Sender : TObject;
                          var Key    : Word;
                              Shift  : TShiftState);
begin // TForm1.FormKeyDown
case key of
    VK_Q :
       begin
       if Shift=[ssCtrl] then
          begin
          ....
-----------------------------------------------------------------

But when I type "Ctrl+Q" then "Key" is 17 instead of VK_Q (81).

If I now use 17 to trigger the "Q" key, then it is also triggered when I just type the Ctrl-key (without any other key)!

This is all a bit weird.
How can I detect that "Ctrl+Q" is pressed without
getting fooled by any other key?

Vojtěch Čihák

Hi,
when you press CTRL+Q you will obtain two OnKeyDown events. The first with Key=17 (it is CTRL) and the second with Key=81 (Q). Both events will have ssCtrl in Shift.  
V.
______________________________________________________________

Show quoted text 5 lines
Od: Jürgen Hestermann Komu: FPC-Pascal users discussions Datum: 08.02.2017 22:12 Předmět: [fpc-pascal] KeyDown routine and Ctrl+Q

I use a KEYDOWN routine in my main TFORM to check for the key Ctrl+Q:

 -----------------------------------------------------------------
 procedure TForm1.FormKeyDown(Sender : TObject;
                          var Key    : Word;
                              Shift  : TShiftState);
 begin // TForm1.FormKeyDown
 case key of
    VK_Q :
       begin
       if Shift=[ssCtrl] then
          begin
          ....
 -----------------------------------------------------------------

But when I type "Ctrl+Q" then "Key" is 17 instead of VK_Q (81).

If I now use 17 to trigger the "Q" key, then it is also triggered when I just type the Ctrl-key (without any other key)!

This is all a bit weird.
How can I detect that "Ctrl+Q" is pressed without
getting fooled by any other key?

Mark Morgan Lloyd
to Jürgen Hestermann: I use a KEYDOWN routine in my main TFORM to check for the key Ctrl+Q:…
Jürgen Hestermann wrote 23 lines
I use a KEYDOWN routine in my main TFORM to check for the key Ctrl+Q: ----------------------------------------------------------------- procedure TForm1.FormKeyDown(Sender : TObject; var Key : Word; Shift : TShiftState); begin // TForm1.FormKeyDown case key of VK_Q : begin if Shift=[ssCtrl] then begin .... ----------------------------------------------------------------- But when I type "Ctrl+Q" then "Key" is 17 instead of VK_Q (81). If I now use 17 to trigger the "Q" key, then it is also triggered when I just type the Ctrl-key (without any other key)! This is all a bit weird. How can I detect that "Ctrl+Q" is pressed without getting fooled by any other key?

Watch out for operating systems which still handle ^S ^Q and possibly ^P specially. I was painfully reminded of this the other day when I hit ^S in an SSH session and everything ground to a halt.

Mark Morgan Lloyd markMLl .AT. telemetry.co .DOT. uk [Opinions above are the author's, not those of his employers or colleagues]
Jürgen Hestermann
to Vojtěch Čihák: when you press CTRL+Q you will obtain two OnKeyDown events. The first with Key=17 (it is…
Vojtěch Čihák wrote
when you press CTRL+Q you will obtain two OnKeyDown events. The first with Key=17 (it is CTRL) and the second with Key=81 (Q). Both events will have ssCtrl in Shift.

Yep, it makes sense for OnKeyDown.
Thanks for the explanation.

I don't know why I was under the impression that it did not work. It works now just the way I coded
it from start. I may have got bewildered by some other problem I got with my code...

BTW:
When Ctrl is pressed, then Shift is empty (which is expected). Only on the second key Q Shift contains ssCtrl.

Vojtěch Čihák
to Vojtěch Čihák: when you press CTRL+Q you will obtain two OnKeyDown events. The first with Key=17 (it is…

Strange. I tested in Linux+Qt and in Delphi7 (under Wine) and both have ssCtrl in Shift when I press Ctrl (i.e. Key=17).  
V.
______________________________________________________________

Show quoted text 5 lines
Od: Jürgen Hestermann Komu: FPC-Pascal users discussions Datum: 09.02.2017 11:52 Předmět: Re: [fpc-pascal] KeyDown routine and Ctrl+Q

BTW:
When Ctrl is pressed, then Shift is empty (which is expected). Only on the second key Q Shift contains ssCtrl.

Jürgen Hestermann
to Vojtěch Čihák
Vojtěch Čihák wrote
Strange. I tested in Linux+Qt and in Delphi7 (under Wine) and both have ssCtrl in Shift when I press Ctrl (i.e. Key=17).

Well, I looked at Shift in the debugger and I could swear that it showed me an empty set [].
But now I can't reproduce it anymore and I always get [ssCtrl] too. So you seem to be right that in both cases Shift=[ssCtrl].