2013-09-06 1 views
4

MSDN은 keybd_event가 SendInput에 의해 대체되었다고 말합니다. 다시 쓰는 동안 SendInput ...을 사용하여 전환했습니다. Alt 키 조합을 보낼 때을 제외하고는 이었습니다. Win7 64 비트 시스템 (다른 곳에서는 아직 시도하지 않음)에서 Alt 키를 보내면 대상 응용 프로그램에서 키 스트로크가 명백해지기 전에 긴 지연이 발생합니다.SendInput 대 keybd_event

왜 그런가? 아니면 내가 뭘 잘못 했니? 지금은 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; 
+0

keybd_event''에이 큰 문제가 귀하의 이벤트가 실제 사람과 접합 얻을 수 있다는 것이다. 그래서'SendInput'이 그 일입니다. 왜 당신은 문제가 있는지 전혀 모르겠다. 아직. –

답변

7

문제 1

당신은 KeyInputCount를 초기화하지 않았다. 그래서 그 값은 정의되지 않았습니다. 처음으로 KeybdInput에 전화하기 전에 0으로 설정하십시오. 아니면 그냥 제거하고 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; 
+0

초기화되지 않은 카운트는 지연을 설명합니다. OS는 배열의 모든 가짜 null 마우스 이벤트를 마침내 4 개의 유효한 키보드 이벤트에 도달하기 전에 누가 처리 하는지를 알 수 있습니다. –

+0

@Rob 주요 문제는 내가 생각하는 잘못된 플래그입니다. –

+0

David와 @Rob. 감사합니다. 초기화되지 않은 KeyInputCount 또는 플래그가 문제인지 여부를 알 수 없습니다. 이걸 알아 내서 기뻐. 아, 그리고 KeyInputCount가 초기화되지 않았 음을 알기가 쉽습니다 ... 누군가 다른 사람이 지적 했으니 까! –

3

이 식으로 keybd_event :

keybd_event(VK_MENU, MapVirtualkey(VK_MENU, 0), KEYEVENTF_EXTENDEDKEY or 0, 0); 
    keybd_event(Ord('M'), MapVirtualKey(Ord('M'),0), KEYEVENTF_EXTENDEDKEY or 0, 0); 
    keybd_event(Ord('M'), MapVirtualKey(Ord('M'),0), KEYEVENTF_EXTENDEDKEY or KEYEVENTF_KEYUP, 0); 
    keybd_event(VK_MENU, MapVirtualkey(VK_MENU, 0), KEYEVENTF_EXTENDEDKEY or KEYEVENTF_KEYUP, 0); 
관련 문제