Board index » delphi » OnKeyPress Event Not Working?

OnKeyPress Event Not Working?

I've written an OnKeyPress event for a DBGrid.  Simple enough, right?

procedure TfmMain.MyDBGridKeyPress(Sender: TObject; var Key: Char);
begin
  if key = vk_Return then
    bbOK.Click;
end;

This code gives an errror: "Incompatible types" on the first line after
begin.  For the KeyPress event, the D3 documentation states:

The Key parameter is the key on the keyboard. For non-alphanumeric keys,
you must use WinAPI virtual key codes to determine the key pressed. For
more information, search for VkKeyScan and VkKeyScanEx in the Win32
Developer's Reference (WIN32.HLP).

Aside:  The VkKeyScan topic in the WIN32.HLP file does NOT list virtual key
codes.

However, being the resourceful programmer I am, I got it from my trusty
Win32API Bible.  The virtual keycode for the Return key is:  vk_Return.

Why doesn't this code work?

David Sweeney
Texas A&M University
adapti...@tamu.edu

 

Re:OnKeyPress Event Not Working?


David,
  Are you sure that your looking at the right help file?  I don't have D3
but in D2 the OnKeyPress event doesn't use the virtual key code but rather
requires that you feed it the Char value of the key.  For the return key
this is #13 so give the following a try:

procedure TForm1.Edit1KeyPress(Sender: TObject; var Key: Char);
begin
  if key = #13 then
    bbOK.Click;
end;

 It works in D2 and I doubt they made a change to it in D3.  Perhaps the
helps files a messed up.  By the way if you want to get rid of the beep
that results from hitting the return key in a TEdit with this code then
write it as follows (I'm not sure if the DBGrid also beeps):

procedure TForm1.Edit1KeyPress(Sender: TObject; var Key: Char);
begin
  if key = #13 then
  begin
    key := #0;  {Kills the key and stops the beep}
    bbOK.Click;
  end;
end;

Hope this helps!
--

Rodney E Geraghty
GERA-Tech
Ottawa, Canada
gera...@ibm.net

David Sweeney <da...@ats.tamu.edu> wrote in article
<01bc8805$c626b210$0acdc280@ats>...

Quote
> I've written an OnKeyPress event for a DBGrid.  Simple enough, right?

> procedure TfmMain.MyDBGridKeyPress(Sender: TObject; var Key: Char);
> begin
>   if key = vk_Return then
>     bbOK.Click;
> end;

> This code gives an errror: "Incompatible types" on the first line after
> begin.  For the KeyPress event, the D3 documentation states:

> The Key parameter is the key on the keyboard. For non-alphanumeric keys,
> you must use WinAPI virtual key codes to determine the key pressed. For
> more information, search for VkKeyScan and VkKeyScanEx in the Win32
> Developer's Reference (WIN32.HLP).

> Aside:  The VkKeyScan topic in the WIN32.HLP file does NOT list virtual
key
> codes.

> However, being the resourceful programmer I am, I got it from my trusty
> Win32API Bible.  The virtual keycode for the Return key is:  vk_Return.

> Why doesn't this code work?

> David Sweeney
> Texas A&M University
> adapti...@tamu.edu

Re:OnKeyPress Event Not Working?


Quote
>I've written an OnKeyPress event for a DBGrid.  Simple enough, right?

>procedure TfmMain.MyDBGridKeyPress(Sender: TObject; var Key: Char);
>begin
>  if key = vk_Return then
>    bbOK.Click;
>end;

>This code gives an errror: "Incompatible types" on the first line after
>begin.  For the KeyPress event, the D3 documentation states:

here's your mistake...
VK_ENTER type is not Char. If you want to check here if Enter was pressed
use this :

if key = #13 then // #13 stands for Chr(13)
....

but if you still want to use VK_XXXX then you have to do it in OnKeyDown
event

Eriks

Re:OnKeyPress Event Not Working?


David,

I'm not sure why your code didn't work, but the following will work:

procedure TYourForm.FormKeyPress(Sender: TObject; var Key: Char);
begin
  if Key = #13 then
  begin
    Key := #0;
    bbOK.Click;
  end;
end;

Hope this helps.

Bill Taylor
b...@btechdesign.com
http://www.btechdesign.com

Re:OnKeyPress Event Not Working?


Quote
David Sweeney wrote:

> I've written an OnKeyPress event for a DBGrid.  Simple enough, right?

> procedure TfmMain.MyDBGridKeyPress(Sender: TObject; var Key: Char);
> begin
>   if key = vk_Return then
>     bbOK.Click;
> end;

> This code gives an errror: "Incompatible types" on the first line after
> begin.  For the KeyPress event, the D3 documentation states:

> The Key parameter is the key on the keyboard. For non-alphanumeric keys,
> you must use WinAPI virtual key codes to determine the key pressed. For
> more information, search for VkKeyScan and VkKeyScanEx in the Win32
> Developer's Reference (WIN32.HLP).

> Aside:  The VkKeyScan topic in the WIN32.HLP file does NOT list virtual key
> codes.

> However, being the resourceful programmer I am, I got it from my trusty
> Win32API Bible.  The virtual keycode for the Return key is:  vk_Return.

> Why doesn't this code work?

OnKeyPress uses ASCII characters. Use OnKeyUp, if you need to use
virtual key codes.

M.Samson-
http://www.tucson.com/objectware
database design, reporting, query, demo tools and freebees

Re:OnKeyPress Event Not Working?


Quote
"David Sweeney" <da...@ats.tamu.edu> wrote:
>I've written an OnKeyPress event for a DBGrid.  Simple enough, right?
>procedure TfmMain.MyDBGridKeyPress(Sender: TObject; var Key: Char);
>begin
>  if key = vk_Return then
>    bbOK.Click;
>end;
>This code gives an errror: "Incompatible types" on the first line after
>begin.  For the KeyPress event, the D3 documentation states:
>The Key parameter is the key on the keyboard. For non-alphanumeric keys,
>you must use WinAPI virtual key codes to determine the key pressed. For
>more information, search for VkKeyScan and VkKeyScanEx in the Win32
>Developer's Reference (WIN32.HLP).
>Aside:  The VkKeyScan topic in the WIN32.HLP file does NOT list virtual key
>codes.
>However, being the resourceful programmer I am, I got it from my trusty
>Win32API Bible.  The virtual keycode for the Return key is:  vk_Return.
>Why doesn't this code work?
>David Sweeney
>Texas A&M University
>adapti...@tamu.edu

Try to use the hexadecimal value for the enter key: $0D.
Thus write:
procedure TfmMain.MyDBGridKeyPress(Sender: TObject; var Key: Char);
begin
  if key = $0D then
    bbOK.Click;
end;

That should do the trick
(In my D1 and D2 applications it works ...)

Re:OnKeyPress Event Not Working?


Quote
M. Samson wrote:

> David Sweeney wrote:

> > I've written an OnKeyPress event for a DBGrid.  Simple enough, right?

> > procedure TfmMain.MyDBGridKeyPress(Sender: TObject; var Key: Char);
> > begin
> >   if key = vk_Return then
> >     bbOK.Click;
> > end;

> > This code gives an errror: "Incompatible types" on the first line after

snip

Quote

> OnKeyPress uses ASCII characters. Use OnKeyUp, if you need to use
> virtual key codes.

This is basically correct but of course not a lot of use if you really
want to use the OnKeyPress event.  The simple answer is to convert the
VK_RETURN to ascii using chr(VK_RETURN).

Ian

Re:OnKeyPress Event Not Working?


Quote
"David Sweeney" <da...@ats.tamu.edu> wrote:
>I've written an OnKeyPress event for a DBGrid.  Simple enough, right?
>procedure TfmMain.MyDBGridKeyPress(Sender: TObject; var Key: Char);
>begin
>  if key = vk_Return then
>    bbOK.Click;
>end;
>This code gives an errror: "Incompatible types" on the first line after
>begin.  For the KeyPress event, the D3 documentation states:
>The Key parameter is the key on the keyboard. For non-alphanumeric keys,
>you must use WinAPI virtual key codes to determine the key pressed. For
>more information, search for VkKeyScan and VkKeyScanEx in the Win32
>Developer's Reference (WIN32.HLP).
>Aside:  The VkKeyScan topic in the WIN32.HLP file does NOT list virtual key
>codes.
>However, being the resourceful programmer I am, I got it from my trusty
>Win32API Bible.  The virtual keycode for the Return key is:  vk_Return.
>Why doesn't this code work?
>David Sweeney
>Texas A&M University
>adapti...@tamu.edu

Oops, made a mistake in the previous reply...
The corrected answer is:

Use the OnKeyDown event instead of the OnKeyPressEvent.
This can handle virtual keycodes (Key is of type Word instead of
char).

Use the hexadecimal value for the enter key: $0D.
Thus write:
procedure TfmMain.MyDBGridKeyDown(Sender: TObject; var Key: Word;
  Shift: TShiftState);
begin
  if key = $0D then
    bbOK.Click;
end;

That should do the trick
(In my D1 and D2 applications it works ...)

To err is human, to forgive ...

John

Re:OnKeyPress Event Not Working?


|>I've written an OnKeyPress event for a DBGrid.  Simple enough, right?
|>
|>procedure TfmMain.MyDBGridKeyPress(Sender: TObject; var Key: Char);
|>begin
|>  if key = vk_Return then
|>    bbOK.Click;
|>end;
|>
|>This code gives an errror: "Incompatible types" on the first line after
|>begin.  For the KeyPress event, the D3 documentation states:
|>
|>The Key parameter is the key on the keyboard. For non-alphanumeric keys,
|>you must use WinAPI virtual key codes to determine the key pressed. For
|>more information, search for VkKeyScan and VkKeyScanEx in the Win32
|>Developer's Reference (WIN32.HLP).
|>
|>Aside:  The VkKeyScan topic in the WIN32.HLP file does NOT list virtual key
|>codes.
|>
|>However, being the resourceful programmer I am, I got it from my trusty
|>Win32API Bible.  The virtual keycode for the Return key is:  vk_Return.
|>
|>Why doesn't this code work?
|>
|>David Sweeney
|>Texas A&M University
|>adapti...@tamu.edu
key is declared as a char.  #13 is for the return key.  so
  If key = #13 then

Other Threads