Sign in Join

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

Mailing lists / lazarus / 2015-08

How to detect that the Alt-key is released?

5 messages · 2 people ·

Reply by email Subscribe to this list Your subscription

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

Jürgen Hestermann

According to http://wiki.lazarus.freepascal.org/LCL_Key_Handling the "KeyUp" event should be triggered when the Alt-key is released.

But how do I tell this procedure that I want to check for the Alt-key? The "key" parameter is of type word.
What is the representation of the Alt-key here?
vk_alt does not exist.

And how about the "Shift" parameter?
It would tell me whether Alt is currently pressed or not but not whether it has been released.

A bit confusing....
Anybody knows how it works?

Reply by email

JuuS
to Jürgen Hestermann

Hi,

"Shift" keys are handled differently. When you to to the declaration of TShiftState you'll see:

  TShiftStateEnum = (ssShift, ssAlt, ssCtrl,
    ssLeft, ssRight, ssMiddle, ssDouble,
    // Extra additions
    ssMeta, ssSuper, ssHyper, ssAltGr, ssCaps, ssNum,
    ssScroll,ssTriple,ssQuad,ssExtra1,ssExtra2);
  TShiftState = set of TShiftStateEnum;

And you use it like:

if ssAlt in Shift then
  showmessage( 'Yessss. Alt.' ).

(btw. this WON'T work in onkeyup with just the alt key...when you lift alt (ie, keyup) it is no longer down and so is not in "Shift", to see it work do something like alt-a).

or

if [ ssShift, ssCtrl ] * Shift = [] then
    showmessage( 'shift or ctrl' );

play around with them, you'll get the results you want.

Also, you probably already know, be sure the form's keypreview property is set to true.

Julius

J?rgen Hestermann wrote 21 lines
According to http://wiki.lazarus.freepascal.org/LCL_Key_Handling the "KeyUp" event should be triggered when the Alt-key is released. But how do I tell this procedure that I want to check for the Alt-key? The "key" parameter is of type word. What is the representation of the Alt-key here? vk_alt does not exist. And how about the "Shift" parameter? It would tell me whether Alt is currently pressed or not but not whether it has been released. A bit confusing.... Anybody knows how it works? -- _______________________________________________ Lazarus mailing list http://lists.lazarus.freepascal.org/mailman/listinfo/lazarus

Reply by email

Jürgen Hestermann
to JuuS
JuuS wrote
(btw. this WON'T work in onkeyup with just the alt key...when you lift alt (ie, keyup) it is no longer down and so is not in "Shift", to see it work do something like alt-a).

But that's just what I want:
Detect the release of the Alt+key (independend from other keys state).

So it's not possible?

Reply by email

Jürgen Hestermann
to Jürgen Hestermann
J?rgen Hestermann wrote 7 lines
Am 2015-08-02 um 14:33 schrieb JuuS: > (btw. this WON'T work in onkeyup with just the alt key...when you lift > alt (ie, keyup) it is no longer down and so is not in "Shift", to see it > work do something like alt-a). But that's just what I want: Detect the release of the Alt+key (independend from other keys state). So it's not possible?

Well, I just found out how it works myself:

---------------------------
procedure TForm1.FormKeyUp(Sender: TObject; var Key: Word; Shift: TShiftState);
begin
if Key=vk_Menu then
    ShowMessage(' ALT key released');
end;
---------------------------

The main point I missed was that I did not know that the Alt-key is "vk_Menu". A strange name for the Alt-key. ;-(

Reply by email

JuuS
to Jürgen Hestermann: But that's just what I want: Detect the release of the Alt+key (independend from other…

Sorry I confused you, my specialty.

What I meant was if you use the 'ssAlt in Shift' line, it won't work if you only press the alt key because in a keyup handler the key is then up! And so won't pass the if statement. It would work in a keydown handler though.

if you press and hold alt and then press and release, for example, 'a' you will see it catch the state of the alt key.

as to the vk_menu, no idea. But to detect states of shift, control, alt, etc. the methods I showed are what is normally used.

J?rgen Hestermann wrote 15 lines
Am 2015-08-02 um 14:33 schrieb JuuS: > (btw. this WON'T work in onkeyup with just the alt key...when you lift > alt (ie, keyup) it is no longer down and so is not in "Shift", to see it > work do something like alt-a). But that's just what I want: Detect the release of the Alt+key (independend from other keys state). So it's not possible? -- _______________________________________________ Lazarus mailing list http://lists.lazarus.freepascal.org/mailman/listinfo/lazarus

Reply by email