Board index » delphi » How can I kill a key press stone dead (Enter Key)

How can I kill a key press stone dead (Enter Key)

Hi,

I have some code in a (shared) OnKeyUp event of some edit boxes on my
main form so that pressing the enter key in any of these boxes pops up
a modal form from which to choose a customer.

In the modal form, pressing the enter key or clicking the OK button
confirms the customer selection and closes the form.    The edit boxes
in the main form are then populated with the customer details.   It's
much more complicated really but that's irrelevant.

All of this works perfectly except for two things.

1:  There is an annoying 'DING' sound with every enter key press.

2:  The Enter key used to close the modal form seems to remain in the
keyboard buffer or something.  It re-triggers the edit box KeyUp event
in the main form and the modal form pops straight back up again.
This continues again and again until you close it with another key
(escape) or with the mouse.

I'm already setting the Key values to 0 but that has no effect.
I've tried setting a variable, changing this in the modal form and
then checking whether it has changed in the KeyUp event and if so,
preventing the form popping up.   This works but then it takes *two*
keypresses to get the form to pop up. Aaarggh!    If I pop it up with
a function key, say F1, then I can use Enter to close it but this
feels clumsy to use.

Unfortunately the clients want to the use the Enter key because they
are used to DOS programs and they don't like those new-fangled 'mouse'
thingies.    I can't wait till the mouse fails and they have to drive
all of Win95 from the keyboard. < {*word*193}, aren't I >

Has anyone else encountered this and found a solution or can you tell
me how to flush the keyboard buffer?

Here's a simplified version of the code - nothing fancy in it.
KeyPreview is true in the modal form but not the main form.

Any help would be greatly appreciated,

Regards,
Colin Acheson

CODE IN THE MAIN FORM:

procedure TFormWork.EditBoxesKeyUp(Sender: TObject; var Key: Word;
          Shift: TShiftState);
begin
  Case Key of
    VK_RETURN:
      begin
        Key := 0;
        {function to call the modal form and retrieve a record}
        {code within function has no bearing on the problem}
           if FindCustomer((Sender as TCustomEdit).Text)
               then CustomerPopulate(Sender)
               else CustomerBlank(Sender);
      end;
    VK_ESCAPE:
      begin
        Key := 0;
        with (Sender as TCustomEdit) do Clear;
      end;
  end;
end;

CODE IN THE MODAL FORM:

procedure TFormCustSrch.FormKeyDown(Sender: TObject; var Key: Word;
  Shift: TShiftState);
begin
  case Key of
    VK_RETURN:
      begin
        Key := 0;
        ModalResult := mrOK;
      end;
    VK_ESCAPE:
      begin
        Key := 0;
        ModalResult := mrCancel;
      end;
  end;
end;

 

Re:How can I kill a key press stone dead (Enter Key)


It looks like the problem is with the way you handled the events.  When you
press enter, the modal form's key down event is fired.  In it you set key to
0, but then the edit boxes key up event is fired which will again send the
enter key.  So in effect you are sending the enter key twice by using key
down and key up.  when you reset key to 0, you are only doing it for the
individual WM_KEYDOWN or WM_KEYUP message, not both of them.

Quote
<no_direct_mail> wrote in message

news:3698833d.291411032@forums.inprise.com...
Quote
>Hi,

>I have some code in a (shared) OnKeyUp event of some edit boxes on my
>main form so that pressing the enter key in any of these boxes pops up
>a modal form from which to choose a customer.

>In the modal form, pressing the enter key or clicking the OK button
>confirms the customer selection and closes the form.    The edit boxes
>in the main form are then populated with the customer details.   It's
>much more complicated really but that's irrelevant.

>All of this works perfectly except for two things.

>1:  There is an annoying 'DING' sound with every enter key press.

>2:  The Enter key used to close the modal form seems to remain in the
>keyboard buffer or something.  It re-triggers the edit box KeyUp event
>in the main form and the modal form pops straight back up again.
>This continues again and again until you close it with another key
>(escape) or with the mouse.

>I'm already setting the Key values to 0 but that has no effect.
>I've tried setting a variable, changing this in the modal form and
>then checking whether it has changed in the KeyUp event and if so,
>preventing the form popping up.   This works but then it takes *two*
>keypresses to get the form to pop up. Aaarggh!    If I pop it up with
>a function key, say F1, then I can use Enter to close it but this
>feels clumsy to use.

>Unfortunately the clients want to the use the Enter key because they
>are used to DOS programs and they don't like those new-fangled 'mouse'
>thingies.    I can't wait till the mouse fails and they have to drive
>all of Win95 from the keyboard. < {*word*193}, aren't I >

>Has anyone else encountered this and found a solution or can you tell
>me how to flush the keyboard buffer?

>Here's a simplified version of the code - nothing fancy in it.
>KeyPreview is true in the modal form but not the main form.

>Any help would be greatly appreciated,

>Regards,
>Colin Acheson

>CODE IN THE MAIN FORM:

>procedure TFormWork.EditBoxesKeyUp(Sender: TObject; var Key: Word;
>          Shift: TShiftState);
>begin
>  Case Key of
>    VK_RETURN:
>      begin
>        Key := 0;
>        {function to call the modal form and retrieve a record}
>        {code within function has no bearing on the problem}
>           if FindCustomer((Sender as TCustomEdit).Text)
>               then CustomerPopulate(Sender)
>               else CustomerBlank(Sender);
>      end;
>    VK_ESCAPE:
>      begin
>        Key := 0;
>        with (Sender as TCustomEdit) do Clear;
>      end;
>  end;
>end;

>CODE IN THE MODAL FORM:

>procedure TFormCustSrch.FormKeyDown(Sender: TObject; var Key: Word;
>  Shift: TShiftState);
>begin
>  case Key of
>    VK_RETURN:
>      begin
>        Key := 0;
>        ModalResult := mrOK;
>      end;
>    VK_ESCAPE:
>      begin
>        Key := 0;
>        ModalResult := mrCancel;
>      end;
>  end;
>end;

Re:How can I kill a key press stone dead (Enter Key)


Ah,

Thank you.   I did not know the messages worked like that.   I will do
some more reading about them but I think you've given me the clue that
I need to fix it.    Thanks again for your help.

Regards,
Colin Acheson

On Sun, 10 Jan 1999 09:46:19 -0800, "Delphi User" <n...@nospam.com>
wrote:

Quote
>It looks like the problem is with the way you handled the events.  When you
>press enter, the modal form's key down event is fired.  In it you set key to
>0, but then the edit boxes key up event is fired which will again send the
>enter key.  So in effect you are sending the enter key twice by using key
>down and key up.  when you reset key to 0, you are only doing it for the
>individual WM_KEYDOWN or WM_KEYUP message, not both of them.

Re:How can I kill a key press stone dead (Enter Key)


Ok, the problem with the form is fixed.  I just moved the code in the
main form from the edit boxes' KeyUp event to their KeyDown event and
that fixed it.   I thought I'd tried every possible combination as I
had spent days on this.  (there was a lot of other stuff involved)

That only leaves the problem of the 'DING' sound when the enter key is
pressed.

Any advance on the DING, anyone?   A 'ding-less' office would be much
more acceptable.

TIA,
Regards,
Colin Acheson

Quote
On Sun, 10 Jan 1999 18:56:08 GMT, I wrote:
>Thank you.   I did not know the messages worked like that.   I will do
>some more reading about them but I think you've given me the clue that
>I need to fix it.    Thanks again for your help.

Re:How can I kill a key press stone dead (Enter Key)


Pretty much the same problem here.  When you press a key, windows generates
a key down, key up and keypress message.  In your keypress event, your still
getting the enter key, and the edit controls default handlers cause the
'ding'.  So in your onKeypress event, check for the enter keypress [chr(13)]
and change it to null[chr(0)].  that'll eliminate the sound.

Quote
<no_direct_mail> wrote in message

news:369930fd.4218830@forums.inprise.com...
Quote
>Ok, the problem with the form is fixed.  I just moved the code in the
>main form from the edit boxes' KeyUp event to their KeyDown event and
>that fixed it.   I thought I'd tried every possible combination as I
>had spent days on this.  (there was a lot of other stuff involved)

>That only leaves the problem of the 'DING' sound when the enter key is
>pressed.

>Any advance on the DING, anyone?   A 'ding-less' office would be much
>more acceptable.

>TIA,
>Regards,
>Colin Acheson

>On Sun, 10 Jan 1999 18:56:08 GMT, I wrote:

>>Thank you.   I did not know the messages worked like that.   I will do
>>some more reading about them but I think you've given me the clue that
>>I need to fix it.    Thanks again for your help.

Re:How can I kill a key press stone dead (Enter Key)


For the DING sound, 2 steps needed:
1. mmsystem unit needs to be added to a uses clause
2. playsound('C:\WINDOWS\MEDIA\Ding', 0, SND_SYNC);

Since I found a Ding sound from  c:\windows\media.  You can replace it with
whatever .wav file you want.
Hope this helps.

--Hanh

Quote
no_direct_mail wrote in message <369930fd.4218...@forums.inprise.com>...
>Ok, the problem with the form is fixed.  I just moved the code in the
>main form from the edit boxes' KeyUp event to their KeyDown event and
>that fixed it.   I thought I'd tried every possible combination as I
>had spent days on this.  (there was a lot of other stuff involved)

>That only leaves the problem of the 'DING' sound when the enter key is
>pressed.

>Any advance on the DING, anyone?   A 'ding-less' office would be much
>more acceptable.

>TIA,
>Regards,
>Colin Acheson

>On Sun, 10 Jan 1999 18:56:08 GMT, I wrote:

>>Thank you.   I did not know the messages worked like that.   I will do
>>some more reading about them but I think you've given me the clue that
>>I need to fix it.    Thanks again for your help.

Re:How can I kill a key press stone dead (Enter Key)


I was expecting it to be more devious than that.    Now that you've
enlightened me, that was easily fixed and it will be a much simpler
task next time.   I must say the help files are rather light on the
details.    Thank you very much for your help.

Regards
Colin Acheson

On Sun, 10 Jan 1999 16:36:29 -0800, "Delphi User" <n...@nospam.com>
wrote:

Quote
>Pretty much the same problem here.  When you press a key, windows generates
>a key down, key up and keypress message.  In your keypress event, your still
>getting the enter key, and the edit controls default handlers cause the
>'ding'.  So in your onKeypress event, check for the enter keypress [chr(13)]
>and change it to null[chr(0)].  that'll eliminate the sound.

><no_direct_mail> wrote in message
>news:369930fd.4218830@forums.inprise.com...
>>That only leaves the problem of the 'DING' sound when the enter key is
>>pressed.

Re:How can I kill a key press stone dead (Enter Key)


Quote
(no_direct_mail) wrote:
>I was expecting it to be more devious than that.    Now that you've
>enlightened me, that was easily fixed and it will be a much simpler
>task next time.   I must say the help files are rather light on the
>details.    Thank you very much for your help.

Hi Colin.

You can find source code for a TEnterAstab component in the ZD Journals
weekly hints on the Inprise web-site.  All controls on a form which
would normally 'ding', will use Enter as Tab.  As the article says,
"And, in the proud Delphi tradition, you won't have to write a single
line of code!"

Regards,
Chris Roberts

Other Threads