gpt4 book ai didi

delphi - 如何以编程方式按下与在键盘上按下完全相同的键?

转载 作者:行者123 更新时间:2023-12-03 14:54:27 28 4
gpt4 key购买 nike

有谁知道如何以编程方式按下与在键盘上按下完全相同的键?PostMessage 失败,SendInput 失败,keybd_event 失败,SendMessage 失败,我什至尝试了使用 Vcl.Touch.Keyboard 模仿按键的不寻常方法。我什至编写了一个调用 sendkeys 的快速 vb 脚本文件,但也失败了。我在一个名为 Sellerdeck http://www.sellerdeck.com/ 的程序上尝试执行此操作时遇到了麻烦。

我已经尝试了以下所有方法,它们都适用于记事本,但在该程序上失败。窗口已成功调至前台

procedure bringToForegroung;
Var
findhandle1 : cardinal;
begin
findhandle1 := FindWindow(NIL, 'Business Plus - Test site: Online Catalogue');
ShowWindow(findhandle1,SW_SHOW);
SetForegroundWindow(findhandle1);
end;

1.

procedure SendF10;
Var
findhandle1 : cardinal;
begin
findhandle1 := FindWindow(NIL, 'Business Plus - Test site: Online Catalogue');
ShowWindow(findhandle1,SW_SHOW);
SetForegroundWindow(findhandle1);
PostMessage(findhandle1, WM_KEYDOWN, VK_F10, 0);
PostMessage(findhandle1, WM_KEYUP, VK_F10, 0);
end;

2.

 procedure SendAltF;
var
KeyInputs: array of TInput;
//--------------------------------------------
procedure KeybdInput(VKey: Byte; Flags: DWORD);
begin
SetLength(KeyInputs, Length(KeyInputs)+1);
KeyInputs[high(KeyInputs)].Itype := INPUT_KEYBOARD;
with KeyInputs[high(KeyInputs)].ki do
begin
wVk := VKey;
wScan := MapVirtualKey(wVk, 0);
dwFlags := Flags;
end;
end;
begin
KeybdInput(VK_MENU, 0); // Alt
KeybdInput(Ord('F'), 0);
KeybdInput(Ord('F'), KEYEVENTF_KEYUP);
KeybdInput(VK_MENU, KEYEVENTF_KEYUP); // Alt
SendInput(Length(KeyInputs), KeyInputs[0], SizeOf(KeyInputs[0]));
end;

3.

procedure SendKey2(Wnd,VK : Cardinal; Ctrl,Alt,Shift : Boolean);
var
MC,MA,MS : Boolean;
begin
// Try to bring target window to foreground
ShowWindow(Wnd,SW_SHOW);
SetForegroundWindow(Wnd);

// Get current state of modifier keys
MC:=Hi(GetAsyncKeyState(VK_CONTROL))>127;
MA:=Hi(GetAsyncKeyState(VK_MENU))>127;
MS:=Hi(GetAsyncKeyState(VK_SHIFT))>127;

// Press modifier keys if necessary (unless already pressed by real user)
if Ctrl<>MC then keybd_event(VK_CONTROL,0,Byte(MC)*KEYEVENTF_KEYUP,0);
if Alt<>MA then keybd_event(VK_MENU,0,Byte(MA)*KEYEVENTF_KEYUP,0);
if Shift<>MS then keybd_event(VK_SHIFT,0,Byte(MS)*KEYEVENTF_KEYUP,0);

// Press key
keybd_event(VK,0,0,0);
keybd_event(VK,0,KEYEVENTF_KEYUP,0);

// Release modifier keys if necessary
if Ctrl<>MC then keybd_event(VK_CONTROL,0,Byte(Ctrl)*KEYEVENTF_KEYUP,0);
if Alt<>MA then keybd_event(VK_MENU,0,Byte(Alt)*KEYEVENTF_KEYUP,0);
if Shift<>MS then keybd_event(VK_SHIFT,0,Byte(Shift)*KEYEVENTF_KEYUP,0);
end;

4.

procedure SendF10v2;
Var
findhandle1 : cardinal;
begin
findhandle1 := FindWindow(NIL, 'Business Plus - Test site: Online Catalogue');
ShowWindow(findhandle1,SW_SHOW);
SetForegroundWindow(findhandle1);
SendMessage(findhandle1, WM_KEYDOWN, VK_F10, 0);
SendMessage(findhandle1, WM_KEYUP, VK_F10, 0);
end;

5.

uses
Vcl.Touch.Keyboard, Vcl.Touch.KeyboardTypes;
type
procedure FormCreate(Sender: TObject);
protected {i.e. dont make form active}
procedure CreateParams(var Params: TCreateParams); override; {i.e. dont make keyboard form active}


procedure TForm1.CreateParams(var Params: TCreateParams);
begin
inherited CreateParams(Params);
with Params do
begin
ExStyle := ExStyle or WS_EX_NOACTIVATE;
WndParent := GetDesktopwindow;
end;
end;

procedure TForm1.SpeedButton1Click(Sender: TObject);
var
KeyData: TKeyData;
begin
KeyData := VKey(VK_F10, -1);
SendKey(KeyData, ksDown);
SendKey(KeyData, ksUp);
end;

6。 (VBS)

Option Explicit
Dim objShell
Set objShell = CreateObject("WScript.Shell")
objShell.Run """C:\Program Files (x86)\SellerDeck 2013\Catalog.exe"" /s ""Test site"" /n Administrator /w Administrator", 0, True
Wscript.Sleep 10000
objShell.SendKeys "%F"
Wscript.Sleep 1000
objShell.SendKeys "%F"
Wscript.Sleep 1000
objShell.SendKeys "%F"
Wscript.Sleep 1000
objShell.SendKeys "%F"
Wscript.Sleep 1000
objShell.SendKeys "%F"
Wscript.Sleep 1000
objShell.SendKeys "%F"
WScript.Quit

正如我所说,所有这些都可以在记事本中工作,否则我会认为是我的电脑出了问题。我正在尝试自动执行此 Sellerdeck 程序上的一些基本任务。有没有办法完全模仿按键?我希望这只是我错过了一些非常基本的东西。

最佳答案

我的猜测是您将命令发送到错误的窗口句柄。也许 FindWindow() 返回的主应用程序窗口不是唯一的按键句柄。

尝试使用调试工具,例如 WinSight(ws32.exe - 旧版本的 Delphi 中附带)来猜测正确的句柄...

关于delphi - 如何以编程方式按下与在键盘上按下完全相同的键?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26713030/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com