Mar*_*orf 4 delphi keyboard-events sendinput
MSDN声明keybd_event已被SendInput取代.在重写期间,我切换到使用SendInput ... 除了尝试发送Alt-key组合时,这很好.在Win7 64位系统上(尚未在其他地方尝试过),在目标应用程序中显示击键之前,发送Alt键会导致长时间延迟.
有什么想法吗?或者我做错了什么?现在,我已经回到了keybd_event - 下面的第二个版本.
//Keyboard input from this version appears only after a ~4-5 second
//time lag...
procedure SendAltM;
var
KeyInputs: array of TInput;
KeyInputCount: Integer;
//--------------------------------------------
procedure KeybdInput(VKey: Byte; Flags: DWORD);
begin
Inc(KeyInputCount);
SetLength(KeyInputs, KeyInputCount);
KeyInputs[KeyInputCount - 1].Itype := INPUT_KEYBOARD;
with KeyInputs[KeyInputCount - 1].ki do
begin
wVk := VKey;
wScan := MapVirtualKey(wVk, 0);
dwFlags := KEYEVENTF_EXTENDEDKEY;
dwFlags := Flags or dwFlags;
time := 0;
dwExtraInfo := 0;
end;
end;
begin
KeybdInput(VK_MENU, 0); // Alt
KeybdInput(Ord('M'), 0);
KeybdInput(Ord('M'), KEYEVENTF_KEYUP);
KeybdInput(VK_MENU, KEYEVENTF_KEYUP); // Alt
SendInput(KeyInputCount, KeyInputs[0], SizeOf(KeyInputs[0]));
end;
//Keyboard input from this version appears immediately...
procedure SendAltM;
begin
keybd_event( VK_MENU, MapVirtualkey( VK_MENU, 0 ), 0, 0);
keybd_event( Ord('M'), MapVirtualKey( Ord('M'),0), 0, 0);
keybd_event( Ord('M'), MapVirtualKey( Ord('M'),0), KEYEVENTF_KEYUP, 0);
keybd_event( VK_MENU, MapVirtualkey( VK_MENU, 0 ), KEYEVENTF_KEYUP, 0);
end;
Run Code Online (Sandbox Code Playgroud)
问题1
你没有初始化KeyInputCount
.所以它的价值是不确定的.KeybdInput
在第一次调用之前将其设置为零.或者只是摆脱它而使用Length(KeyInputs)
.
问题2
您的设置dwFlags
不正确.不包括在内KEYEVENTF_EXTENDEDKEY
.您没有将它包含在调用的代码中keybd_event
,您不应该将其包含在内SendInput
.
更正了代码
这个版本有效.
procedure SendAltM;
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('M'), 0);
KeybdInput(Ord('M'), KEYEVENTF_KEYUP);
KeybdInput(VK_MENU, KEYEVENTF_KEYUP); // Alt
SendInput(Length(KeyInputs), KeyInputs[0], SizeOf(KeyInputs[0]));
end;
Run Code Online (Sandbox Code Playgroud)
归档时间: |
|
查看次数: |
10597 次 |
最近记录: |