Board index » delphi » KeyDown Event

KeyDown Event

Hi,

As someone who has recently started programming with Delphi (in the past I
have used Basic), I am busy going through library books in a desperate
attempt to soak up knowledge.

As a test of this new found knowledge I have set myself a task to create a
Desktop Calculator.  To start with having very basic functions (e.g. add,
subtract, multiply divide), but perhaps adding functions to the code later
if required.

Everything seems to be going well until I have used the KeyDown event to
allow keyboard input as well.

The problem (finally) is I cannot seem to trap the Enter/Return Key.  All
other keys seem to work fine and call there relative routines.

The KeyDown event routine is as follows:

procedure TBigCalculator.FormKeyDown(Sender: TObject;
  var Key: Word; Shift: TShiftState);
begin
    Case key of
      VK_RETURN:ButtonEqualClick(Sender);
      VK_NUMPAD1:Button1Click(Sender);
      VK_NUMPAD2:Button2Click(Sender);
      VK_NUMPAD3:Button3Click(Sender);
      VK_NUMPAD4:Button4Click(Sender);
      VK_NUMPAD5:Button5Click(Sender);
      VK_NUMPAD6:Button6Click(Sender);
      VK_NUMPAD7:Button7Click(Sender);
      VK_NUMPAD8:Button8Click(Sender);
      VK_NUMPAD9:Button9Click(Sender);
      VK_NUMPAD0:Button0Click(Sender);
      VK_DIVIDE:ButtonDivClick(Sender);
      VK_MULTIPLY:ButtonMultClick(Sender);
      VK_ADD:ButtonPlusClick(Sender);
      VK_SUBTRACT:ButtonMinClick(Sender);
      VK_DECIMAL:ButtonDotClick(Sender);
    End;
end;

Regards
Keith

 

Re:KeyDown Event


I see no immediate error here, but I think you should consider
using the KeyPressed event rather than KeyDown. KeyDown
distinguishes for instance as far as I remember between ordinary
numeric keys and the numeric keys on the numpad which is not
what you would usually want. Wait a moment: Does KeyDown
apply VK_RETURN to the numpad enter key? I wonder.
(The KeyPressed value for enter/return is #23 unless I remember
wrong)

good luck

regards Sven

Quote
"Keith Bridge" <Ke...@kgbridge.freeserve.co.uk> wrote in message

news:959trv$njb$1@news6.svr.pol.co.uk...
Quote
> Hi,

> As someone who has recently started programming with Delphi (in the past I
> have used Basic), I am busy going through library books in a desperate
> attempt to soak up knowledge.

> As a test of this new found knowledge I have set myself a task to create a
> Desktop Calculator.  To start with having very basic functions (e.g. add,
> subtract, multiply divide), but perhaps adding functions to the code later
> if required.

> Everything seems to be going well until I have used the KeyDown event to
> allow keyboard input as well.

> The problem (finally) is I cannot seem to trap the Enter/Return Key.  All
> other keys seem to work fine and call there relative routines.

> The KeyDown event routine is as follows:

> procedure TBigCalculator.FormKeyDown(Sender: TObject;
>   var Key: Word; Shift: TShiftState);
> begin
>     Case key of
>       VK_RETURN:ButtonEqualClick(Sender);
>       VK_NUMPAD1:Button1Click(Sender);
>       VK_NUMPAD2:Button2Click(Sender);
>       VK_NUMPAD3:Button3Click(Sender);
>       VK_NUMPAD4:Button4Click(Sender);
>       VK_NUMPAD5:Button5Click(Sender);
>       VK_NUMPAD6:Button6Click(Sender);
>       VK_NUMPAD7:Button7Click(Sender);
>       VK_NUMPAD8:Button8Click(Sender);
>       VK_NUMPAD9:Button9Click(Sender);
>       VK_NUMPAD0:Button0Click(Sender);
>       VK_DIVIDE:ButtonDivClick(Sender);
>       VK_MULTIPLY:ButtonMultClick(Sender);
>       VK_ADD:ButtonPlusClick(Sender);
>       VK_SUBTRACT:ButtonMinClick(Sender);
>       VK_DECIMAL:ButtonDotClick(Sender);
>     End;
> end;

> Regards
> Keith

Re:KeyDown Event


The Enter key is trapped before you get a OnKeyDown event. Instead, set the
Default property of ButtonEqual to True and you will see that ButtonEqual
will be click if you press Enter !

"Keith Bridge" <Ke...@kgbridge.freeserve.co.uk> schreef in bericht
news:959trv$njb$1@news6.svr.pol.co.uk...

Quote
> Hi,

> As someone who has recently started programming with Delphi (in the past I
> have used Basic), I am busy going through library books in a desperate
> attempt to soak up knowledge.

> As a test of this new found knowledge I have set myself a task to create a
> Desktop Calculator.  To start with having very basic functions (e.g. add,
> subtract, multiply divide), but perhaps adding functions to the code later
> if required.

> Everything seems to be going well until I have used the KeyDown event to
> allow keyboard input as well.

> The problem (finally) is I cannot seem to trap the Enter/Return Key.  All
> other keys seem to work fine and call there relative routines.

> The KeyDown event routine is as follows:

> procedure TBigCalculator.FormKeyDown(Sender: TObject;
>   var Key: Word; Shift: TShiftState);
> begin
>     Case key of
>       VK_RETURN:ButtonEqualClick(Sender);
>       VK_NUMPAD1:Button1Click(Sender);
>       VK_NUMPAD2:Button2Click(Sender);
>       VK_NUMPAD3:Button3Click(Sender);
>       VK_NUMPAD4:Button4Click(Sender);
>       VK_NUMPAD5:Button5Click(Sender);
>       VK_NUMPAD6:Button6Click(Sender);
>       VK_NUMPAD7:Button7Click(Sender);
>       VK_NUMPAD8:Button8Click(Sender);
>       VK_NUMPAD9:Button9Click(Sender);
>       VK_NUMPAD0:Button0Click(Sender);
>       VK_DIVIDE:ButtonDivClick(Sender);
>       VK_MULTIPLY:ButtonMultClick(Sender);
>       VK_ADD:ButtonPlusClick(Sender);
>       VK_SUBTRACT:ButtonMinClick(Sender);
>       VK_DECIMAL:ButtonDotClick(Sender);
>     End;
> end;

> Regards
> Keith

Re:KeyDown Event


You might also have to set the Form's KeyPreview property to True.

Maybe.

Re:KeyDown Event


Quote
"M.H. Avegaart" <avegaartNOS...@mccomm.nl> wrote in message

news:95b820$scs$1@porthos.nl.uu.net...

Quote
> The Enter key is trapped before you get a OnKeyDown event. Instead, set
the
> Default property of ButtonEqual to True and you will see that ButtonEqual
> will be click if you press Enter !

  I think that's ok with OnKeyDown too.. I just checked it when KeyPreview
property of the form is true and everything works.

  Regards
  Alex Shlega

**********************************
ComKast meta search:
http://a1385.svwh.net/comkast/search
**********************************

Other Threads