Icon View Thread

The following is the text of the current message along with any replies.
Messages 1 to 7 of 7 total
Thread TEdit OnKeyDown Event
Tue, Mar 10 2015 5:18 AMPermanent Link

Eivind

All

I used to have an OnKeyDown event on my login form to trigger the login when the user hits the Enter key. Code below. This code was on the OnKeyDown for both the username and password TEdit's

if Key = 13 then
  btLoginClick(Self)

This worked perfectly until I upgraded to the newest build a few days ago. I did some checking and all other keys but the Enter key trigger this event. Nothing at all happens when the enter key is clicked. Ok, so after some thinking and checking out the help file, I set the key preview for the form to true and handled the OnKeyDown event for the form instead. This event is fired when Enter is clicked. Here is the problem..... If the user enters invalid login credentials I will show a dialog using ShowMessage with an error message. If I now click on enter to close the ShowMessage dialog a new login attempt is triggered as the form handles all the key events. The ShowMessage dialog does not close until I hit enter a few times. On all those Enter clicks, a new login attempt is executed and more ShowMessage dialogs are triggered.

So, the question is, how can I trigger an OnKeyDown event on the Enter click on the TEdit, or get the form to close the Message Dialog when an enter is clicked? It all worked perfectly until the last build.

Thanks

Eivind
Tue, Mar 10 2015 10:01 AMPermanent Link

Matthew Jones

Eivind wrote:

> So, the question is, how can I trigger an OnKeyDown event on the
> Enter click on the TEdit, or get the form to close the Message Dialog
> when an enter is clicked? It all worked perfectly until the last
> build.

I've checked, and I have a test for #13 in OnKeyPress which may be
different to the OnKeyDown. But it works for just the edit box. Worth a
try?

--

Matthew Jones
Tue, Mar 10 2015 10:24 AMPermanent Link

Tim Young [Elevate Software]

Elevate Software, Inc.

Avatar

Email timyoung@elevatesoft.com

Eivind,

<< This worked perfectly until I upgraded to the newest build a few days
ago. >>

I just tried this here using the latest build and it worked fine:

function TForm1.Edit1KeyDown(Sender: TObject; Key: Integer; ShiftKey,
CtrlKey, AltKey: Boolean): Boolean;
begin
  Result := true;
  if (Key = 13) then
     begin
     Result := false;
     Button3Click(nil);
     end;
end;

Are you sure that you're assigning the proper Result in all cases ?

<< If I now click on enter to close the ShowMessage dialog a new login
attempt is triggered as the form handles all the key events. The ShowMessage
dialog does not close until I hit enter a few times. On all those Enter
clicks, a new login attempt is executed and more ShowMessage dialogs are
triggered. >>

Please email me an example of this.  EWB specifically checks if the control
receiving the key-up event is the one that had the key-down event in the
first place, so I don't see how this could occur.  Plus, there's the issue
of the message dialog *not* being a child of the form in question, therefore
it can't possible get preview key events for it....

Tim Young
Elevate Software
www.elevatesoft.com
Wed, Mar 11 2015 4:34 AMPermanent Link

Eivind

Ok, after some more research, I finally found the problem. Sure the OnKeyDown event worked as expected, but only with KeyPreview for the form set to True. If set o False, nothing happens when push the Enter key. This is supposed be be like this I guess. What I found out was that KeyPreview had to be set to True for a TMemo to work too. If KeyPreview was set to False, then pressing the Enter key did not create a new line in the TMemo. Is this correct?

Down to the real issue as mentioned before... KeyPreview is set to True.... I have a login form with two TEdit for username and password. Both are linked to a OnKeyDown procedure that calls the Login button OnClick method when Enter is clicked. Up until this point, it all work as it are supposed to do if KeyPreview is set to True.

The Login Button OnClick method calls a TServerRequest to log in the user. If login credentials are correct, the Login form is closed and the Main form is shown. Now, if the user inputs incorrect credential, a 401 code is returned to the TServerRequest OnComplete and inside this method, a ShowMessage is executed with a error message to the user. Now.... If clicking the Enter key to close the message dialog, nothing happens and a NEW loging attempt is executed. It looks like when the ShowMessage dialog is triggered from the TServerRequest, it does not have focus and the form catches the Enter and tries to log the user in again. So then I tried to physically click on the header of the  error message dialog before I click Enter.... And would you know... Enter closes the dialog. So yea, it all apears that the ShowMessage dialog triggered from inside the OnComplete does not have focus for the main form when hitting Enter to close it. Instead a new login attempt is fired

Any ideas on how to solve this? Obviously I cannot expect the user to first click on the header of the dialog before hitting enter to close it. Would using MessageDlg work differently than a ShowMessage?

Thanks for any thoughts

Eivind
Wed, Mar 11 2015 6:13 AMPermanent Link

Matthew Jones

Just one thought, the TPanel has KeyPreview too, and I seem to remember
some thing about having to have it in all the parents or something to
get to the form. Been discussed previously.

Can't help on the main issue though, but not been aware of such am
issue myself.Indeed, just checking it here doesn't get a repeat of the
Enter on a MessageDlg.

function TfrmLogin.editEmailKeyDown(Sender: TObject; Key: Integer;
ShiftKey, CtrlKey, AltKey: Boolean): Boolean;
begin
   if Key = 13 then
       btnLoginClick(nil);
   Result := true;
end;


MessageDlg(szMessage, 'Login incorrect', mtConfirmation, [mbOK], mbOK,
MessageConfirmed, False);

--

Matthew Jones
Wed, Mar 11 2015 10:06 AMPermanent Link

Eivind

Mattew

Thanks for your suggestions. That is actually more or exactly what I do as well. However, I found the "bug". In the next line of code after the ShowMessage dialog is triggered, I call the SetFocus for the username TEdit. Once I removed that line of code, the enter key closes the dialog. Ok, I can see why as javascript are async. However, did not think it could take focus away from a modal dialog. Lessen learned Smile

Eivind
Wed, Mar 11 2015 11:19 AMPermanent Link

Matthew Jones

Eivind wrote:

> Thanks for your suggestions. That is actually more or exactly what I
> do as well. However, I found the "bug". In the next line of code
> after the ShowMessage dialog is triggered, I call the SetFocus for
> the username TEdit. Once I removed that line of code, the enter key
> closes the dialog. Ok, I can see why as javascript are async.
> However, did not think it could take focus away from a modal dialog.
> Lessen learned Smile

Thanks for posting that. I expect you could set the focus before the
dialog, and it would probably return. But yes, the async dialogs are
fun!

--

Matthew Jones
Image