Why should I use SendInput over mouse and keybd events?

87 views Asked by At

When looking up how to use keybd_event on Microsoft Learn, it says:

Note This function has been superseded. Use SendInput instead.

But when looking at how to use SendInput, it looks a lot more complex for not much, if any, gain, even though it can handle mouse and keyboard similarly.

For instance: Pressing Ctrl down with minimal effort:

keybd_event:

windll.user32.keybd_event(0x11, 0, 0, 0)

sendInput takes 6 more lines while also calling a very similar-looking function, KEYBDINPUT:

lib = WinDLL('user32')
lib.SendInput.argtypes = w.UINT,POINTER(INPUT),c_int
lib.SendInput.restype = w.UINT

i = INPUT()
i.type = INPUT_KEYBOARD
i.ki = KEYBDINPUT(0,0x11,0,0,0)
lib.SendInput(1,byref(i),sizeof(INPUT))

1

There are 1 answers

0
Malady On

As a comment on SendInput vs. keybd_event says, emphasis mine:

This big problem with keybd_event is that your events may get spliced with real ones. So SendInput is the job. No idea why you are having problems. Yet.

David Heffernan
Sep 6, 2013 at 16:50