form中有多个BitBtn,我想在BitBtn间用上下键来移动。但是,经测试,在TForm1.FormKeyDown事件中,键盘输入上下键,FormKeyDown事件却无法相应。如果是输入其他按键都可以相应FormKeyDown事件。怪怪啊!

解决方案 »

  1.   

    你可以试试unit Unit1;interfaceuses
      Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
      Dialogs, StdCtrls;type
      TForm1 = class(TForm)
        procedure FormCreate(Sender: TObject);
      private
        { Private declarations }
      public
        { Public declarations }
      end;
    type TMyButton=class(Tbutton)
      private
         procedure CMWantSpecialKey(var Message: TCMWantSpecialKey); message CM_WANTSPECIALKEY;
    end;
    var
      Form1: TForm1;implementation{$R *.dfm}
    procedure TMyButton.CMWantSpecialKey(var Message: TCMWantSpecialKey); begin
    inherited;
    // 我们只想处理向左方向键,其它的特殊键都给 Windows 处理
      case Message.CharCode of
        VK_UP  : showmessage('Up');
        VK_Down: showmessage('Down');
      end;
      Message.Result := 1;
    end;
    procedure TForm1.FormCreate(Sender: TObject);
    var Button1,button2:Tmybutton;
    begin
      button1:=Tmybutton.Create(nil);
      button1.Parent:=self;
      button1.Left:=100;
      button1.Top:=100;
      button1.Caption:='button1';
      button2:=Tmybutton.Create(nil);
      button2.Parent:=self;
      button2.Left:=100;
      button2.Top:=200;
      button2.Caption:='button2';
    end;end.
      

  2.   

    窗体的KeyPreview设置成true;在窗体事件
    procedure TForm1.FormKeyDown(Sender: TObject; var Key: Word; Shift: TShiftState);
    begin  case Key of
        VK_UP  : self.Perform(WM_NEXTDLGCTL,0,0);
        VK_Down: self.Perform(WM_NEXTDLGCTL,1,0);
      end;
      Key := 0;end;
      

  3.   

    用message实现了]
    你找以下资料很都的
      

  4.   

    没有大家讲的这么复杂吧
    其实在 object inspector 里设置taborder 的属性例如 BitBtn1为1,BitBtn2为2,BitBtn3为3,
    这样应该可以的
      

  5.   

    如果只有bitbtn,确实默认上下键就可以了;
    否则按lynmison(菻梓) 说的不错
      

  6.   

    反了,
    窗体的KeyPreview设置成true;在窗体事件procedure TForm1.FormKeyDown(Sender: TObject; var Key: Word; Shift: TShiftState);
    begin  case Key of
        VK_UP  : self.Perform(WM_NEXTDLGCTL,1,0);
        VK_Down: self.Perform(WM_NEXTDLGCTL,0,0);
      end;
      Key := 0;end;