C# - Sending messages to Google Chrome from C# application

后端 未结 4 718
北恋
北恋 2020-12-14 18:00

I\'ve been searching around, and I haven\'t found how I would do this from C#.

I was wanting to make it so I could tell Google Chrome to go Forward,

4条回答
  •  没有蜡笔的小新
    2020-12-14 18:46

    This is a great site for interop constants:

    pinvoke

    Another way of finding the values is to search koders.com, using C# as the language, for WM_KEYDOWN or the constant you're after:

    Koders.com search

    &H values look like that's from VB(6). pinvoke and koders both return results for VK_BROWSER_FORWARD,

    private const UInt32 WM_KEYDOWN        = 0x0100;
    private const UInt32 WM_KEYUP          = 0x0101;
    
    public const ushort VK_BROWSER_BACK = 0xA6;
    public const ushort VK_BROWSER_FORWARD = 0xA7;
    public const ushort VK_BROWSER_REFRESH = 0xA8;
    public const ushort VK_BROWSER_STOP = 0xA9;
    public const ushort VK_BROWSER_SEARCH = 0xAA;
    public const ushort VK_BROWSER_FAVORITES = 0xAB;
    public const ushort VK_BROWSER_HOME = 0xAC;
    

    (It's funny how many wrong defintions of VK constants are floating about, considering VK_* are 1 byte 0-255 values, and people have made them uints).

    Looks slightly different from your consts. I think the function you're after is SendInput (but I haven't tried it) as it's a virtual key.

    [DllImport("User32.dll")]
    private static extern uint SendInput(uint numberOfInputs, [MarshalAs(UnmanagedType.LPArray, SizeConst = 1)] KEYBOARD_INPUT[] input, int structSize);
    

    Explanation about the parameters:

    Parameters

    • nInputs- Number of structures in the pInputs array.
    • pInputs - Pointer to an array of INPUT structures. Each structure represents an event to be inserted into the keyboard or mouse input stream.
    • cbSize - Specifies the size, in bytes, of an INPUT structure. If cbSize is not the size of an INPUT structure, the function fails.

    This needs a KEYBOARD_INPUT type:

    [StructLayout(LayoutKind.Sequential)] 
    public struct KEYBOARD_INPUT
    { 
        public uint type; 
        public ushort vk; 
        public ushort scanCode; 
        public uint flags; 
        public uint time; 
        public uint extrainfo; 
        public uint padding1; 
        public uint padding2; 
    }
    

    And finally a sample, which I haven't tested if it works:

    /*
    typedef struct tagKEYBDINPUT {
        WORD wVk;
        WORD wScan;
        DWORD dwFlags;
        DWORD time;
        ULONG_PTR dwExtraInfo;
    } KEYBDINPUT, *PKEYBDINPUT;
    */
    public static void sendKey(int scanCode, bool press)
    {
            KEYBOARD_INPUT[] input = new KEYBOARD_INPUT[1];
            input[0] = new KEYBOARD_INPUT();
            input[0].type = INPUT_KEYBOARD;
            input[0].vk = VK_BROWSER_BACK;
    
        uint result = SendInput(1, input, Marshal.SizeOf(input[0]));
    }
    

    Also you'll need to focus the Chrome window using SetForegroundWindow

提交回复
热议问题