Can't send Browser_Back/Forward via SendInput() (only on Surface Pro 2)

195 Views Asked by At

some days ago, I got my Microsoft Arc Touch Mouse Surface Edition. Unfortunately, it doesn't have Forward/Back-Buttons (it performs only Page-Up/Page-Down).

So, I've written a little program, that catches the press of the Page_Up/Page_Down Keys and sends instead the Browser-Back/-Forward Button.

On my PC (Win 8.1 Pro) this works quite well. However, on my Surface Pro 2 (for which the program primarly was written) it won't send the Browser_Back-/Forward-Buttons. For every other key the program seems to work (like Space, letters, numbers).

Here the code for sending Keycodes:

      public static uint send(short keyCode) {

        INPUT structure = new INPUT();
        structure.type = (int)InputType.INPUT_KEYBOARD;
       structure.ki.wVk = keyCode;

        structure.ki.dwFlags = (int)KEYEVENTF.KEYDOWN;
        structure.ki.dwExtraInfo = GetMessageExtraInfo();

        INPUT input2 = new INPUT();
        structure.type = (int)InputType.INPUT_KEYBOARD;
        structure.ki.wVk = keyCode;

        input2.mi.dwFlags = (int)KEYEVENTF.KEYUP;
        input2.ki.dwExtraInfo = GetMessageExtraInfo();

        INPUT[] pInputs = new INPUT[] { structure, input2 };

        return SendInput(2, pInputs, Marshal.SizeOf(structure));
    }

And here the KeyboardHook:

       public static int KeyHookProc(int nCode, IntPtr wParam, IntPtr lParam) {

        KeyHookStruct myKeyHookStruct =
            (KeyHookStruct)Marshal.PtrToStructure(lParam, typeof(KeyHookStruct));

        if (nCode < 0) {
            return CallNextHookEx(hHook, nCode, wParam, lParam);
        } else {

            if (wParam == (IntPtr)0x0100) {


                int keyCode = myKeyHookStruct.vkCode;
                if (keyCode == 33) {

                    Input.send(0xA7);
                } else if (keyCode == 34) {

                    Input.send(0xA6);
                } else {
                    return CallNextHookEx(hHook, nCode, wParam, lParam);
                }


            }

            return 1;

        }

    }

So, if the Page-Down/page-Up button on my keyboard is pressed, the send()-Method is called. 0xA6 and 0xA7 are the Keycodes for Browser_Back and Browser_Forward.

On my Surface, the Page-Up/Page-Down buttons are recognized correctly. The send()-Method is also called, but it won'T send the correct Keycodes. According to Spy++, send() isn't sending anything if the Keycode is 0xA6 or 0xA7 (on my PC it works).

Where is the problem with the Surface Pro 2? What could I do, that it works, or how could I determine why it isn't working?

1

There are 1 best solutions below

5
On

The code you posted is wrong, as you never set type for input2 (the code use structure.type)

EDIT:

From MSDN docs for SendInput:

This function fails when it is blocked by UIPI. Note that neither GetLastError nor the return value will indicate the failure was caused by UIPI blocking.

This function is subject to UIPI. Applications are permitted to inject input only into applications that are at an equal or lesser integrity level.

You also should remove the call to GetMessageExtraInfo

New Edit:

I don't like the logic of your KeyHookProc function: if the message is NOT WM_KEYDOWN (0x0100), then you return 1...

I would go for:

if (wParam != (IntPtr)0x0100) {
   return CallNextHookEx(hHook, nCode, wParam, lParam);
} else {
   int keyCode = myKeyHookStruct.vkCode;
   if (keyCode == 33) {
      Input.send(0xA7);
   } else if (keyCode == 34) {
      Input.send(0xA6);
   } else {
      return CallNextHookEx(hHook, nCode, wParam, lParam);
   }
   return 1;
}