SendInput vs. keybd_event

前端 未结 2 1974
野的像风
野的像风 2021-01-19 04:45

MSDN states that keybd_event has been superceded by SendInput. During a rewrite I switched to using SendInput...which was fine except when trying to send an

2条回答
  •  离开以前
    2021-01-19 05:24

    Problem 1

    You did not initialise KeyInputCount. So its value is undefined. Set it to zero before calling KeybdInput the first time. Or just get rid of it and use Length(KeyInputs) instead.

    Problem 2

    Your setting of dwFlags is incorrect. Don't include KEYEVENTF_EXTENDEDKEY. You did not include it in the code that calls keybd_event, and you should not include it for SendInput.

    Corrected code

    This version works.

    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;
    

提交回复
热议问题