Board index » delphi » Why a Beep when Enter key is pressed?

Why a Beep when Enter key is pressed?

I wanted to be able to commit the text in a edit box by pressing  the
enter key so I added a case Key of #13 to the code as follows:

procedure TForm1.FreqEditKeyPress(Sender: TObject; var Key: Char);

    begin
      Case Key of #13:  {to cause the enter key to commit entry)
      begin        
        sendstring:= Freqedit.text;

VSSComm321.writecommdata(pchar(sendstring),length(sendstring));

        FreqEdit.text:='';         {clear the edit box for next entry}
        FreqEdit.SetFocus;      {reset focus for next entry}
      end;
    end;

The idea is that I want to repetitive enter data into the edit box
without having to reset the focus with the mouse.

Now when I press enter it does work but I get a beep every time?

Any Idea why?

Thanks -

Dave Alden -   d-al...@concentric.net

 

Re:Why a Beep when Enter key is pressed?


Dave,
  Change your code as follows:

procedure TForm1.FreqEditKeyPress(Sender: TObject; var Key: Char);

    begin
      Case Key of #13:  {to cause the enter key to commit entry)
      begin
        Key := #0;        {This will kill the beep}
        sendstring:= Freqedit.text;

VSSComm321.writecommdata(pchar(sendstring),length(sendstring));

        FreqEdit.text:='';         {clear the edit box for next entry}
        FreqEdit.SetFocus;      {reset focus for next entry}
      end;
    end;

Hope this helps!
--
Rod Geraghty (:>)
GERA-Tech
Ottawa, Canada
gera...@ibm.net

d-al...@concentric.net wrote in article
<33458a8f.6824...@news.concentric.net>...

Quote
> I wanted to be able to commit the text in a edit box by pressing  the
> enter key so I added a case Key of #13 to the code as follows:

> procedure TForm1.FreqEditKeyPress(Sender: TObject; var Key: Char);

>     begin
>       Case Key of #13:  {to cause the enter key to commit entry)
>       begin        
>         sendstring:= Freqedit.text;

> VSSComm321.writecommdata(pchar(sendstring),length(sendstring));

>         FreqEdit.text:='';         {clear the edit box for next entry}
>         FreqEdit.SetFocus;      {reset focus for next entry}
>       end;
>     end;

> The idea is that I want to repetitive enter data into the edit box
> without having to reset the focus with the mouse.

> Now when I press enter it does work but I get a beep every time?

> Any Idea why?

> Thanks -

> Dave Alden -   d-al...@concentric.net

Re:Why a Beep when Enter key is pressed?


Quote
d-al...@concentric.net wrote:

> I wanted to be able to commit the text in a edit box by pressing  the
> enter key so I added a case Key of #13 to the code as follows:

> procedure TForm1.FreqEditKeyPress(Sender: TObject; var Key: Char);

>     begin
>       Case Key of #13:  {to cause the enter key to commit entry)
>       begin
>         sendstring:= Freqedit.text;

> VSSComm321.writecommdata(pchar(sendstring),length(sendstring));

>         FreqEdit.text:='';         {clear the edit box for next entry}
>         FreqEdit.SetFocus;      {reset focus for next entry}
>       end;
>     end;

> The idea is that I want to repetitive enter data into the edit box
> without having to reset the focus with the mouse.

> Now when I press enter it does work but I get a beep every time?

> Any Idea why?

> Thanks -

> Dave Alden -   d-al...@concentric.net

You need to set Key:=#0 somewhere in you OnKeyPress handler to prevent
the beep.

Re:Why a Beep when Enter key is pressed?


Quote
d-al...@concentric.net wrote:
>I wanted to be able to commit the text in a edit box by pressing  the
>enter key so I added a case Key of #13 to the code as follows:
>>> snip <<<

You just have to add a single statement:

Key := #0;

... to the procedure.

---------------------------------------------
  Ove Kjeldgaard
    !o_kj...@post4.tele.dk
      Remove the ! if you wish to e-mail me
---------------------------------------------

Re:Why a Beep when Enter key is pressed?


Quote
On Sun, 30 Mar 1997 15:45:59 GMT, d-al...@concentric.net wrote:
>procedure TForm1.FreqEditKeyPress(Sender: TObject; var Key: Char);

>    begin
>      Case Key of #13:  {to cause the enter key to commit entry)
>      begin        
>        sendstring:= Freqedit.text;

>VSSComm321.writecommdata(pchar(sendstring),length(sendstring));

>        FreqEdit.text:='';         {clear the edit box for next entry}
>        FreqEdit.SetFocus;      {reset focus for next entry}

          Key := #0;    

Quote
>      end;
>    end;

>Now when I press enter it does work but I get a beep every time?

>Any Idea why?

Your code gets executed, but the value for Key still gets passed on
when your case statement finishes, hence the Beep.  To stop this, you
should set Key to #0 so that the program continues as though nothing
had happened.

Add the line that I have above in the position you find it and all
should be well.

Regards,
Greg.

Re:Why a Beep when Enter key is pressed?


Quote
d-al...@concentric.net wrote:

> I wanted to be able to commit the text in a edit box by pressing  the
> enter key so I added a case Key of #13 to the code as follows:

> procedure TForm1.FreqEditKeyPress(Sender: TObject; var Key: Char);

>     begin
>       Case Key of #13:  {to cause the enter key to commit entry)
>       begin
>         sendstring:= Freqedit.text;

> VSSComm321.writecommdata(pchar(sendstring),length(sendstring));

>         FreqEdit.text:='';         {clear the edit box for next entry}
>         FreqEdit.SetFocus;      {reset focus for next entry}
>       end;
>     end;

> The idea is that I want to repetitive enter data into the edit box
> without having to reset the focus with the mouse.

> Now when I press enter it does work but I get a beep every time?

> Any Idea why?

\

You don't really need to use the SetFocus if you swallow the the "RETURN
key.".  By doing this you also get rid of the "Beeping".

Put an extra line like this in place of the SetFocus()..:

   Key = Chr(0);

Good Luck,

JBLi

Other Threads