Board index » cppbuilder » KeyDown and Key=0

KeyDown and Key=0

Hi everybody,
if I set the Key-Value of a KeyDown Method to 0, nothing really happens.
The key is shown in the TEdit as normal. Who can help?? The Code of the
KeyDown-Method looks like this:

void __fastcall TfrmMain::txtNummerVonKeyDown(TObject *Sender, WORD
&Key,
      TShiftState Shift)
{
  if ( Shift == TShiftState() )
  {
    if (Key != VK_BACK && Key != VK_DELETE && Key != VK_LEFT && Key !=
VK_RIGHT &&
          ( Key < 48 || Key > 57 ) )
    {
          Key = 0;
    }
  }
  else
    Key = 0;

Quote
}

The KeyPreview of the Form is set to true!!

if I press the 'a'-Button it character should not appear in the
edit-Field. But It does - although I set Key = 0.

Ciao

Andreas

 

Re:KeyDown and Key=0


The first thing I should point out to you is that the arrow keys don't ever
trigger the OnKey... events, so you can't test for VK_LEFT, VK_RIGHT, etc
the way you're doing it

As for your actual delemna, it looks like you're trying to prevent the user
from entering anything but numers, right?  Try this instead:

#include <ctype.h>

void __fastcall TfrmMain::txtNummerVonKeyDown(TObject *Sender, WORD &Key,
TShiftState Shift)
{
    if (Key != VK_BACK && Key != VK_DELETE && !isdigit(Key) )
        Key = 0;

Quote
}

If that doesn't do what you need, then try it inside the OnKeyPress event
instead of OnKeyDown

Gambit

Quote
"Andreas Homa" <h...@iml.fhg.de> wrote in message

news:3A65A4E2.678D3BD4@iml.fhg.de...

Quote
> if I set the Key-Value of a KeyDown Method to 0, nothing really happens.
> The key is shown in the TEdit as normal. Who can help?? The Code of the
> KeyDown-Method looks like this:
<snip>
> The KeyPreview of the Form is set to true!!

> if I press the 'a'-Button it character should not appear in the
> edit-Field. But It does - although I set Key = 0.

Other Threads