delphi 如何模拟鼠标点击特定的虚拟键盘按钮?

soat7uwm  于 2023-05-17  发布在  其他
关注(0)|答案(1)|浏览(159)

有一个带有虚拟键盘的窗体。我想模拟鼠标点击一个特定的虚拟键盘按钮时,用户点击真实的的键盘按钮。我想做到这一点有两个原因:
1.我计划在我的虚拟键盘上有一个修改器按钮,我想要受到真实的的按钮按下和按下的影响。
1.按下真实的键盘时虚拟键盘上的绘画效果。
所以,我有一些代码,在按下真实的的键盘按钮“A”似乎成功点击虚拟“A”按钮,但它并不能正常工作的所有方式。

TForm1 = class(TForm)
    TouchKeyboard1: TTouchKeyboard;
  private
    { Private declarations }
  protected
    procedure WndProc(var Message: TMessage); override;
  public
    { Public declarations }
  end;
  TCastTouchKeyboard= class(TTouchKeyboard);
var
  Form1: TForm1;

implementation
{$R *.dfm}

procedure TForm1.WndProc(var Message: TMessage);
 var
  MouseLDownMsg: TWMMButtonDown;
begin
  inherited;                          {ScanCode of letter A}
  if (Message.Msg = WM_KEYDOWN) and (Lo(Message.lParamHi) = $1E) then
  with MouseLDownMsg, TCastTouchKeyboard(TouchKeyboard1) do
   begin
    Msg:= WM_LBUTTONDOWN;
    Pos:= PointToSmallPoint(Buttons[31].BoundsRect.CenterPoint);
    WndProc(TMessage(MouseLDownMsg))
   end
end;
end.

我会很感激任何关于处理虚拟键盘上适当的鼠标点击真实键盘上的键向下和向上按下的一般想法的建议。

niknxzdl

niknxzdl1#

你说得对,雷米。代码如下:

procedure TForm1.WndProc(var Message: TMessage);
 var
  Input: TInput;
  Point: TPoint;
begin
  inherited;
  with Input, Point, TCastTouchKeyboard(TouchKeyboard1), Message do
  if (Msg = WM_KEYDOWN) and (LParamHi = $1E) and (Buttons[31].State <> TCustomKeyboardButton.TDrawState.dsPressed) then
   begin
    Point:= TouchKeyboard1.ClientToScreen(Buttons[31].BoundsRect.CenterPoint);
    X:= Round(X / GetSystemMetricsForWindow(SM_CXSCREEN, Handle) * Succ(High(Word)));
    Y:= Round(Y / GetSystemMetricsForWindow(SM_CYSCREEN, Handle) * Succ(High(Word)));
    Itype:= INPUT_MOUSE;
    with mi do begin
      dx:= X;
      dy:= Y;
      mouseData:= 0;
      dwFlags:= MOUSEEVENTF_MOVE or MOUSEEVENTF_ABSOLUTE;
      time:= 0;
      dwExtraInfo:= 0;
      SendInput(1, Input, SizeOf(TInput));
      dwFlags:= MOUSEEVENTF_LEFTDOWN;
      SendInput(1, Input, SizeOf(TInput));
      dwFlags:= MOUSEEVENTF_LEFTUP;
      SendInput(1, Input, SizeOf(TInput))
    end
   end
end;

谢谢

相关问题