Let the webbrowser control respond to keyboard shortcuts or mouse information

Source: Internet
Author: User

The IE control (webbrowser Control) added through SDK or ATL cannot respond to the tab message of the keyboard, which is inconvenient when entering the form. You need to click the input box with the mouse each time, I have seen someArticleIt is said that the response message can be obtained through the idochostshowui interface and the idochostuihandler2 interface of the IE control. However, I tried these methods but it was not successful ~ _~. So we adopted a relatively biased method: hook keyboard messages.

First, set the keyboard message processing function. This function is used to obtain the keyboard input key information. If this key is a tab key and is pressed, send the message to the shortcut key processing function of the IE control for shortcut key processing. Similarly, in order to submit an HTML form through the Enter key, it is determined that the input key is the return key and the key is released, send the message to the shortcut key processing function of the IE control for shortcut key processing.

 

Declare a global hhook handle first

 
Hhook h_keyboard;

Message Processing functionsAs follows:

 Lresult callback keyboardproc (  Int Code, //  Hook code Wparam, // Virtual-key code Lparam //  Keystroke-message information  ){  If (Wparam = vk_tab & getkeystate (wparam) < 0  ){  //  Press the tab key  //  : MessageBox (null, _ T ("tab"), _ T (""), 0 );  // The following describes how to obtain the browser object and send the shortcut key like the object.CodeHere, m_wndie is of the axwindow type. If you directly use the SDK to create the IE control, the previous code is different, but the method for obtaining the interface is the same Cwebwindow * pwebwindow = G_webwindow; MSG; msg. hwnd = Pwebwindow-> M_wndie.m_hwnd; MSG. Message = Wm_keydown; msg. lparam = Lparam; msg. wparam = Wparam; hresult HR = S_ OK; iwebbrowser2ptr browser; HR = Pwebwindow-> m_wndie.querycontrol (_ uuidof (iwebbrowser2 ),( Void **)&Browser); atlassert (succeeded (HR); ccomptr <Idispatch> Disp; HR = Browser-> get_document (& Disp); atlassert (succeeded (HR ));  //  Ccomptr <ihtmldocument2> Doc; Ccomqiptr <ioleinplaceactiveobject> Spinplace; HR = Disp-> QueryInterface (_ uuidof (ioleinplaceactiveobject ),( Void **)& Spinplace); atlassert (succeeded (HR ));  If (Spinplace)  Bool Bret = (spinplace-> translateaccelerator (& MSG) = s_ OK )? True: false ;}  Else   If (Wparam = vk_return & getkeystate (wparam)> 0  ){  ///  Release the Enter key          //  : MessageBox (null, _ T ("enter"), _ T (""), 0 );          /// /The following code gets the browser object and sends the shortcut key like the object. Here, m_wndie is of the axwindow type. If you use the SDK to create the IE control, the previous code is different, however, the method for obtaining the interface is the same. Cwebwindow * pwebwindow = G_webwindow; MSG; msg. hwnd = Pwebwindow-> M_wndie.m_hwnd; MSG. Message = Wm_keyup; msg. lparam = Lparam; msg. wparam = Wparam; hresult HR = S_ OK; iwebbrowser2ptr browser; HR = Pwebwindow-> m_wndie.querycontrol (_ uuidof (iwebbrowser2 ),( Void **)&Browser); atlassert (succeeded (HR); ccomptr <Idispatch> Disp; HR = Browser-> get_document (& Disp); atlassert (succeeded (HR ));  //  Ccomptr <ihtmldocument2> Doc; Ccomqiptr <ioleinplaceactiveobject> Spinplace; HR = Disp-> QueryInterface (_ uuidof (ioleinplaceactiveobject ),( Void **)& Spinplace); atlassert (succeeded (HR ));  If (Spinplace)  Bool Bret = (spinplace-> translateaccelerator (& MSG) = s_ OK )? True: false ;}  //  A non-zero value is returned, indicating that the Keyboard Message has been processed.      Return  Callnexthookex (h_keyboard, code, wparam, lparam );} 

 

ThenSet Hook FunctionsIn the initialization part of the main function:

 
H_keyboard = setwindowshookex (wh_keyboard, keyboardproc, hinstance,0);

Finally,Release hookBefore the exit of the main function:

 
Unhookwindowshookex (h_keyboard );

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.