Board index » delphi » onKeyDown problem.

onKeyDown problem.

Hi,
I using Delph 1.02
I have a TForm with some buttons and a TPaintbox component.  I need to
catch  in the TForm's onKeyDown eventhandler the VK_LEFT and VK_RIGHT.
I set TForm's KeyPreview to TRUE, but it seems to me that thise virtual
key's does not generate a exception. Houw can I catch thise key's?

--
Regards Henk Hinssen
        The Netherlands.
        E-Mail: <mailto:hins...@pi.net>

 

Re:onKeyDown problem.


For some reason it only seems to work with edit comtrols. However, if
you use NoKeyUp, it will always work. However, the focus will have
shifted.

Hope this helps

Steve Griffiths

procedure TForm1.FormKeyUp(Sender: TObject; var Key: Word;
  Shift: TShiftState);
begin
 Case Key of
   VK_LEFT : begin
     ShowMessage('Left');
      Key := 0;
    end;
    VK_RIGHT : begin
     ShowMessage('Right');
      Key := 0;
    end;
  end;
end;

Quote
Henk Hinssen wrote:
> Hi,
> I using Delph 1.02
> I have a TForm with some buttons and a TPaintbox component.  I need to

> catch  in the TForm's onKeyDown eventhandler the VK_LEFT and VK_RIGHT.

> I set TForm's KeyPreview to TRUE, but it seems to me that thise
> virtual
> key's does not generate a exception. Houw can I catch thise key's?

> --
> Regards Henk Hinssen
>         The Netherlands.
>         E-Mail: <mailto:hins...@pi.net>

Re:onKeyDown problem.


Quote
In article <3456118F.EAF6F...@pi.net>, Henk Hinssen wrote:
> I have a TForm with some buttons and a TPaintbox component.  I need to
> catch  in the TForm's onKeyDown eventhandler the VK_LEFT and VK_RIGHT.
> I set TForm's KeyPreview to TRUE, but it seems to me that thise virtual
> key's does not generate a exception. Houw can I catch thise key's?

YOu need to go to an application level message handler to trap them properly.
The following unit demonstrates with the VK_UP and VK_DOWN keys:

unit Arrows;

interface

uses
  SysUtils, WinTypes, WinProcs, Messages, Classes, Graphics, Controls,
  Forms, Dialogs, StdCtrls;

type
  TFrmArrows = class(TForm)
    Edit1: TEdit;
    Edit2: TEdit;
    Edit3: TEdit;
    Edit4: TEdit;
    Memo1: TMemo;
    Button1: TButton;
    procedure FormCreate(Sender: TObject);
  private { Private-Deklarationen }
    procedure AppMessage(var Msg: TMsg; var Handled: Boolean);
  public { Public-Deklarationen }
  end;

var
  FrmArrows: TFrmArrows;

implementation

{$R *.DFM}

procedure TFrmArrows.AppMessage(var Msg: TMsg; var Handled: Boolean);
begin
  if Msg.Message = WM_KEYDOWN then
    if Msg.wParam = VK_UP then begin
      SelectNext(ActiveControl, false, true); { Move to previous control. }
      //PostMessage(Handle, WM_NEXTDLGCTL, 1, 0);
      Handled := true
    end
    else if Msg.wParam = VK_DOWN then begin
      SelectNext(ActiveControl, true, true); { Move to next control. }
      //PostMessage(Handle, WM_NEXTDLGCTL, 0, 0);
      Handled := true;
    end;
end;

procedure TFrmArrows.FormCreate(Sender: TObject);
begin
  Application.OnMessage := AppMessage;
end;

end.

Regards
Ralph (TeamB)
Herrsching, Germany, Tue, 28 Oct 1997 18:01 +0100 CET

Re:onKeyDown problem.


Henk,

the key preview feature is really a preview, it sees only the keys that the
control with focus also sees. Not all controls are interested in the arrow
keys, if such a control (e.g. a button) has focus your handler will not see
them. However, the form sees these keys in the form of CM_DIALOGKEY
messages and uses them to navigate between controls on the form. So you can
give your form a handler form CM_DIALOGKEY and act on the keys there.

  private  // form declaration
    Procedure CMDialogKey(Var Msg: TWMKey); message CM_DIALOGKEY;

Procedure TForm1.CMDialogKey(Var Msg: TWMKEY);
Begin
  Case msg.charcode of
    VK_LEFT: ...
    VK_RIGHT: ...
  Else  
    inherited;
  End;  
End;

Peter Below (TeamB)  100113.1...@compuserve.com)

Quote
In article <3456118F.EAF6F...@pi.net>, Henk Hinssen wrote:
> From: Henk Hinssen <hins...@pi.net>
> Newsgroups: borland.public.delphi.vcl.components.using
> Subject: onKeyDown problem.
> Date: Tue, 28 Oct 1997 17:23:43 +0100

> Hi,
> I using Delph 1.02
> I have a TForm with some buttons and a TPaintbox component.  I need to
> catch  in the TForm's onKeyDown eventhandler the VK_LEFT and VK_RIGHT.
> I set TForm's KeyPreview to TRUE, but it seems to me that thise virtual
> key's does not generate a exception. Houw can I catch thise key's?

> --
> Regards Henk Hinssen
>         The Netherlands.
>         E-Mail: <mailto:hins...@pi.net>

Other Threads