How can I remove Beep when I press Enter key?

This is the forum for miscellaneous technical/programming questions.

Moderator: 2ffat

How can I remove Beep when I press Enter key?

Postby Augusto » Sun Jan 14, 2018 4:07 pm

Hi friends!
I have a form with many TEdits.
Every time I press Enter key, then I switch the focus to the next field but I get always a "beep".
I am trying to remove it using the code suggested also in the Embarcadero forum but it doesn't work.
How can I solve the problem?

Example:

void __fastcall TForm9::Edit1KeyUp(TObject *Sender, WORD &Key, TShiftState Shift)

{
if (Key == VK_RETURN) {
Key = NULL; // To remove beep
if ((Form9->RadioGroup1->ItemIndex == 0) || (Form9->RadioGroup1->ItemIndex == 3)) {
Form9->Edit2->SetFocus();
}
if ((Form9->RadioGroup1->ItemIndex == 1) || (Form9->RadioGroup1->ItemIndex == 4)) {
Form9->Edit3->SetFocus();
}
...

Also
Key = 0;
do beep the same.

Thank you in advance for your collaboration.
Best regards,
Augusto
Augusto
Active Poster
Active Poster
 
Posts: 10
Joined: Sun Jan 14, 2018 2:40 pm

Re: How can I remove Beep when I press Enter key?

Postby HsiaLin » Mon Jan 15, 2018 3:13 am

If im not mistaken it was something like this.

Code: Select all
if(Trim(Edit1->Text).Length() > 0 && Key == VK_RETURN)
{
    Key=0;
   //do your thing here
}
HsiaLin
BCBJ Master
BCBJ Master
 
Posts: 297
Joined: Sun Jul 08, 2007 6:29 pm

Re: How can I remove Beep when I press Enter key?

Postby rlebeau » Mon Jan 15, 2018 11:16 am

Augusto wrote:I have a form with many TEdits.
Every time I press Enter key, then I switch the focus to the next field but I get always a "beep".
I am trying to remove it using the code suggested also in the Embarcadero forum but it doesn't work.
How can I solve the problem?


Setting the Key parameter to 0 is correct, but you need to do it in the OnKeyPress event instead of the OnKeyUp event:

Code: Select all
void __fastcall TForm9::Edit1KeyPress(TObject *Sender, Char &Key)
{
   if (Key == VK_RETURN) {
      Key = 0; // To remove beep
      ...
   }
}


Also, don't use the global Form9 pointer inside of methods of the TForm9 class. Use the implicit 'this' pointer instead.

And I would suggest using a 'switch' statement instead of a series of 'if' statements.

Try this:

Code: Select all
void __fastcall TForm9::Edit1KeyPress(TObject *Sender, Char &Key)
{
   if (Key == VK_RETURN) {
      Key = 0; // To remove beep
      switch (RadioGroup1->ItemIndex) {
         case 0:
         case 3:
            Edit2->SetFocus();
            break;
         case 1:
         case 4:
            Edit3->SetFocus();
            break;
         ...
      }
   }
}
Remy Lebeau (TeamB)
Lebeau Software
User avatar
rlebeau
BCBJ Author
BCBJ Author
 
Posts: 1509
Joined: Wed Jun 01, 2005 3:21 am
Location: California, USA

Re: How can I remove Beep when I press Enter key?

Postby Augusto » Mon Jan 15, 2018 4:10 pm

rlebeau wrote:Setting the Key parameter to 0 is correct, but you need to do it in the OnKeyPress event instead of the OnKeyUp event:

Code: Select all
void __fastcall TForm9::Edit1KeyPress(TObject *Sender, Char &Key)
{
   if (Key == VK_RETURN) {
      Key = 0; // To remove beep
      ...
   }
}


Hi rlebeau,
Yes, you are right!
I am asking to myself why I have used "KeyUp" instead of "KeyPress".
I admit, it's my fault.
Can be I have done this mistake the first time and then I have repeated it on all the rest.

rlebeau wrote:Also, don't use the global Form9 pointer inside of methods of the TForm9 class. Use the implicit 'this' pointer instead.


I prefer to be honest, I am a newbie with C++, thank you for this information and I am going to study the differences. I prefer to go deeper.
At the moment I have not clear which is the problem writing the code using the global Form9, however, please let me to study and understand better.

rlebeau wrote:And I would suggest using a 'switch' statement instead of a series of 'if' statements.


About this detail, I have a question for you.
Is it only because it is more readable?
Or because it is faster?
Or because in this way you evaluate "RadioGroup1->ItemIndex" only one time?
Or what else?

Thank you, very much for your support.
Augusto
Active Poster
Active Poster
 
Posts: 10
Joined: Sun Jan 14, 2018 2:40 pm

Re: How can I remove Beep when I press Enter key?

Postby rlebeau » Mon Jan 15, 2018 5:57 pm

Augusto wrote:At the moment I have not clear which is the problem writing the code using the global Form9, however, please let me to study and understand better.


The global pointer is only used for Forms that are auto-created at runtime, or if you assign the pointer manually. Either way, it limits your ability to use multiple instances of the same Form class at the same time (you will likely want to do that eventually as your skills evolve).

Non-static class methods have a hidden 'this' parameter, which points to the object that the method was called on. So, code inside such a method does not need to use an external pointer to find the object. For instance:

Code: Select all
void __fastcall TForm9::Edit1KeyPress(TObject *Sender, Char &Key)
{
    ...
    if ((Form9->RadioGroup1->ItemIndex == 0) || (Form9->RadioGroup1->ItemIndex == 3)) {
        Form9->Edit2->SetFocus();
    }
    ...
}


Becomes:

Code: Select all
// Edit1KeyPress() is a non-static method of TForm9, so 'this'
// is a pointer to a TForm9 object in memory...
void __fastcall TForm9::Edit1KeyPress(TObject *Sender, Char &Key)
{
    ...
    if ((this->RadioGroup1->ItemIndex == 0) || (this->RadioGroup1->ItemIndex == 3)) {
        this->Edit2->SetFocus();
    }
    ...
}


And 'this' is implicit, so you don't need to use it explicitly (there are other use cases for that, but this is not one of them), so you can simply omit it:

Code: Select all
void __fastcall TForm9::Edit1KeyPress(TObject *Sender, Char &Key)
{
    ...
    if ((RadioGroup1->ItemIndex == 0) || (RadioGroup1->ItemIndex == 3)) {
        Edit2->SetFocus();
    }
    ...
}


Augusto wrote:
rlebeau wrote:And I would suggest using a 'switch' statement instead of a series of 'if' statements.


About this detail, I have a question for you.
Is it only because it is more readable?
Or because it is faster?
Or because in this way you evaluate "RadioGroup1->ItemIndex" only one time?


All of the above:

- It is less code to read

- it is (usually) faster. Worse case, the performance is the same as multiple 'if' statements. But best case, the compiler/optimizer can arrange the code blocks in such a way that the 'switch' value can be used to calculate an index in to a lookup table of memory addresses for those blocks, then execution flow can simply jump right to the appropriate code block with fewer comparisons needed to find that block ('switch' comparisons do not have to be linear).

- the 'switch' condition is evaluated only once, and the resulting value is compared to the 'case' values, or even used as a lookup to jump to the code block directly.
Remy Lebeau (TeamB)
Lebeau Software
User avatar
rlebeau
BCBJ Author
BCBJ Author
 
Posts: 1509
Joined: Wed Jun 01, 2005 3:21 am
Location: California, USA

Re: How can I remove Beep when I press Enter key?

Postby Augusto » Tue Jan 16, 2018 2:26 am

rlebeau wrote:[
The global pointer is only used for Forms that are auto-created at runtime, or if you assign the pointer manually. Either way, it limits your ability to use multiple instances of the same Form class at the same time (you will likely want to do that eventually as your skills evolve).

Hi rlebeau!
Now it is clear, I have not thinked to use multiple instances, it is a very interesting possibility.

rlebeau wrote:All of the above:
- It is less code to read
- it is (usually) faster. Worse case, the performance is the same as multiple 'if' statements. But best case, the compiler/optimizer can arrange the code blocks in such a way that the 'switch' value can be used to calculate an index in to a lookup table of memory addresses for those blocks, then execution flow can simply jump right to the appropriate code block with fewer comparisons needed to find that block ('switch' comparisons do not have to be linear).
- the 'switch' condition is evaluated only once, and the resulting value is compared to the 'case' values, or even used as a lookup to jump to the code block directly.

I agree.
Thank you.
Augusto
Active Poster
Active Poster
 
Posts: 10
Joined: Sun Jan 14, 2018 2:40 pm


Return to Technical

Who is online

Users browsing this forum: No registered users and 7 guests

cron