1

For the past 3 hours or so I've been attempting to send keyboard input by writing to the keyboard device. I have successfully found and opened the keyboard device, but I'm stuck at the final step. I don't know exactly how to format the DeviceIoControl parameters and I don't really know where to start getting the values.

Currently I have the following taken partly from a library called Interception posted in another answer here. I left out all the device opening stuff to save space.

#define IOCTL_WRITE CTL_CODE(FILE_DEVICE_UNKNOWN, 0x820, METHOD_BUFFERED, FILE_ANY_ACCESS)

if(device != INVALID_HANDLE_VALUE) {
    DWORD dwReturned;
    KEYBOARD_INPUT_DATA kbinput;
    kbinput.UnitId = 0;
    kbinput.MakeCode = 0x2D;
    kbinput.Flags = KEY_MAKE;
    kbinput.Reserved = 0;
    kbinput.ExtraInformation = 0;

    DeviceIoControl(device, IOCTL_WRITE, &kbinput, sizeof(KEYBOARD_INPUT_DATA), NULL, 0, &dwReturned, NULL);
    kbinput.Flags = KEY_BREAK;
    DeviceIoControl(device, IOCTL_WRITE, &kbinput, sizeof(KEYBOARD_INPUT_DATA), NULL, 0, &dwReturned, NULL);
}

If I call GetLastError after the DeviceIoControl calls I get a return value of ERROR_INVALID_FUNCTION(1). I assume that means IOCTL_WRITE isn't the correct value, but I haven't the faintest idea on how to find the correct value and no amount of searching has gotten me any further.

ozdrgnaDiies
  • 1,909
  • 1
  • 19
  • 34
  • Do you absolutely what to use `DeviceIoControl()` ? Because otherwise, you could just use `SendInput()` (http://msdn.microsoft.com/en-us/library/ms646310%28v=vs.85%29.aspx) – Benoit Blanchon Oct 27 '13 at 08:37
  • 1
    I am trying to interact with the device directly because SendInput/keybd_event and other derivatives do not work in multiple cases. – ozdrgnaDiies Oct 27 '13 at 08:39
  • IOCTL_INTERNAL_I8042_KEYBOARD_WRITE_BUFFER might be the code you are looking for – o_weisman Oct 27 '13 at 08:59
  • @o_weisman that sounds promising, but after around 20 minutes of looking around and testing things I am unable to get any further. I can't get anything to compile in Visual Studio as ntdd8042.h isn't available and when compiled under MinGW I still get the same error. Do you have an example of its proper use? – ozdrgnaDiies Oct 27 '13 at 09:20
  • @ozdrgnaDiies I may have misled you. Seems you'll need the DDK (Driver Development Kit) for this to work. You could try it like so: #define IOCTL_INTERNAL_I8042_KEYBOARD_WRITE_BUFFER CTL_CODE(FILE_DEVICE_KEYBOARD, 0x0FF1, METHOD_NEITHER, FILE_ANY_ACCESS) – o_weisman Oct 27 '13 at 09:31
  • 3
    Don't use that IOCTL, it is used to send data to the keyboard controller. Looking at the WDK source code for the class driver and the i8042 driver, I see no IOCTL that lets you poke keystrokes. This is surely going to require a filter driver, look at the Joyflt.exe sample for example. – Hans Passant Oct 27 '13 at 13:08

0 Answers0