Lazarus

Programming => Widgetset => Cocoa => Topic started by: emhartner on August 19, 2019, 02:14:15 am

Title: Escape Key Closing MessageDlg When Called From the KeyDown Event Handler
Post by: emhartner on August 19, 2019, 02:14:15 am
When catching the escape key in FormKeyDown and then immediately asking a question in a MessageDlg, the MessageDlg shows up very quickly and then disappears.  In the example below, I see the MessageDlg flash briefly and then see the ShowMessage with "Yes pressed".  It looks like the escape is also closing down the MessageDlg.  When catching a different key in the example below (i.e. not escape) all works as expected.

Code: Pascal  [Select][+][-]
  1. procedure TForm1.FormKeyDown(Sender: TObject; var Key: Word; Shift: TShiftState
  2.   );
  3. begin
  4.   if Key = VK_ESCAPE
  5.   then if MessageDlg('Choose Yes or No.',mtConfirmation,[mbYes,mbNo],0) = mrYes
  6.        then ShowMessage('Yes pressed')
  7.        else ShowMessage('No pressed');
  8.  
  9.   inherited;
  10. end;
  11.  

This was working correctly in Lazarus 1.8.4.

Lazarus v2.1.0 r61720
fpc v3.3.1 r42727
Widgetset = cocoa
CPU family = x86_64

Thank you.
Title: Re: Escape Key Closing MessageDlg When Called From the KeyDown Event Handler
Post by: PascalDragon on August 19, 2019, 09:27:44 am
The problem is that the dialog reacts to the UP event for the Escape key. You show the dialog before the UP event is received and thus the dialog will receive that right away and close itself. So it should work if you put your code into a handler for OnKeyUp.
Title: Re: Escape Key Closing MessageDlg When Called From the KeyDown Event Handler
Post by: Handoko on August 19, 2019, 09:37:27 am
I cannot reproduce the behavior on Lazarus 2.0.2 Linux GTK2. I think it is OS dependent behavior.
Title: Re: Escape Key Closing MessageDlg When Called From the KeyDown Event Handler
Post by: wp on August 19, 2019, 11:09:01 am
I cannot reproduce the issue either (Window 10).

If I could I would have checked the following modifications:
Title: Re: Escape Key Closing MessageDlg When Called From the KeyDown Event Handler
Post by: emhartner on August 19, 2019, 07:39:17 pm
I have tried to "erase the key (Key := 0)" prior to the MessageDlg, but it did no good.

I'll try KeyPress / KeyUp to see if I can get the desired behavior.  However, please note that on previous versions with Carbon on Mac the functionality was as expected using KeyDown.

Thank you.
Title: Re: Escape Key Closing MessageDlg When Called From the KeyDown Event Handler
Post by: Thaddy on August 19, 2019, 07:48:17 pm
The issue is simply that the OnKeyDown keeps firing (which it should!).
The solution is simply to connect to OnKeyUp!!!!

I have explained that many times before....And fires only once, which is what you want.

Alternatively - if it is the only use - set OnKeyDown to nil in the event itself. Then it also fires just once.)bad practice, but possible)
TinyPortal © 2005-2018