类似Form,Button这样的组件都有Keydown事件。如果输入焦点在button上,当有键盘输入的时候,button的keydown事件会被调用,而form的KeyDown事件就不起作用了。有没有什么办法可以原封不动的把该消息再次发给form 的keydown事件?

解决方案 »

  1.   

    SendMessage(Form1.Handle,WM_CHAR,VK_RETURN,0);
      

  2.   

    定义自己的消息:
    const WM_MYMESSAGE=WM_USER+1234;
    处理消息函数:
    procedure OnMyMessage(var message:tmessage);message WM_MYMESSAGE
    begin
      dispatch(xxxx//发送自定义消息
      

  3.   

    把Form的KeyPreview设为True就可
    以下代码为证
    function TWinControl.DoKeyDown(var Message: TWMKey): Boolean;
    var
      ShiftState: TShiftState;
      Form: TCustomForm;
    begin
      Result := True;
      Form := GetParentForm(Self);
      if (Form <> nil) and (Form <> Self) and Form.KeyPreview and
        TWinControl(Form).DoKeyDown(Message) then Exit;
      with Message do
      begin
        ShiftState := KeyDataToShiftState(KeyData);
        if not (csNoStdEvents in ControlStyle) then
        begin
          KeyDown(CharCode, ShiftState);
          if CharCode = 0 then Exit;
        end;
      end;
      Result := False;
    end;
      

  4.   

    最好还是用一楼的办法,至少是线程安全的。tcomponent.dispatch线程不安全。
      

  5.   

    WM_CHAR?OnKeyPress和OnKeyDown混了吧?要不就在Button接收到WM_KEYDOWN后,原封不动的发给Form!Application.OnMessage:=ApplicationMessage;procedure TForm1.ApplicationMessage(var Msg: tagMSG;
      var Handled: Boolean);
    begin
    if (Msg.hwnd=Button1.Handle) and (Msg.message=WM_KEYDOWN) then
      Form1.Perform(Msg.message,Msg.wParam,Msg.lParam);
    end;
      

  6.   

    最简单的方法:
    Button1KeyDown
    begin
    ...
    Form1.KeyDown(Sender);
    end;
      

  7.   

    no
    Button1KeyDown=Form1KeyDown