There are two ways to implement the shortcut keys of the custom Dialog Box program.

Source: Internet
Author: User

Author: Zhu Jincan

Source: http://www.cnblogs.com/clever101

 

After creating a project, my module is complete (my module is a dialog box ).Program), But others have not completed, I have to cooperate with others to test, specifically, click the button to send tasks to others. I think it is complicated to use the mouse, so I want to add shortcuts. I thought that I could turn off the screen, just press the keyboard. I press the keypad's number key 1 to send Task 1, press the number key 2 to send Task 2 ......

 

Define shortcut keys for buttons. The common practice is as follows:

 

Method 1Write special characters to the button's Caption

For example, add "ATL + O" to the "open" button and write "open (& O)" to the caption )"

 

Method 2 Registration System hotkeys:

1Declared hotkey message processing function prototype
In. H, add the following statement to the Message ing Declaration (after afx_msg:

 

Lresult onhotkey (wparam, lparam );

2 . the message is associated with the corresponding processing function
in. add a message ing macro to CPP to associate the message with the corresponding processing function.
on_message (wm_hotkey, onhotkey);
3. for future operations,
Create a function in advance in the class to respond to the wm_create and wm_destroy messages
framework of oncreate () and ondestroy ()
4. register the hotkey with the System
Add the following Code to the oncreate () function to register the hotkey with the system, in this example, set the hotkey to
Ctrl + Shift +.

Registerhotkey (m_hwnd, 1001 , Mod_control | Mod_shift, ' A ' );
Registerhotkey (m_hwnd, 1002 , Mod_control | Mod_shift, ' A ' );


5. Handling hotkeys
Process the hotkey in the message processing function onhotkey () and add the program code you want to run.

Code
Lresult C **** : Onhotkey (wparam, lparam)
If (Wparam = 1001 | Wparam = 1002 )
Cwnd: setforegroundwindow (); // Make the activated window appear in the foreground
MessageBox ( " Hello! " );
// You can add code here
Return 0 ;

 

6. Remove the hotkey after running the program.
In ondestroy (), unregisterhotkey () is used to cancel hotkey registration and release system resources.

Unregisterhotkey (m_hwnd, 1001 );
Unregisterhotkey (m_hwnd, 1002 );

 

7. Compile and run the program

 

Both methods share a common drawback, that is, they must use a combination of keys, that is, I must use two fingers. I am determined that only one finger is enough. At first, I thought that as long as the responseWm_charThe message is enough, and later I found that it is not enough, becauseWhen there is nothing in a dialog box, the onchar event can be received to the form. Otherwise, the default message is sent to the input focus window. After some effort, I found two methods:

 

Method 1 use a keyboard HOOK:

 

A Brief Introduction to the functions required for keyboard hooks:

WindowsWhen a callback function is called, it first calls the function at the beginning of the function chain. If we place our callback function at the beginning of the chain, the callback function will be called first. So how can we place our own callback functions at the beginning of the function chain? The setwindowshookex () function implements this function. Let's first take a look at the prototype of the setwindowshookex function:

 

Hhook setwindowshookex (
Int Idhook,
Hookproc lpfn,
Hinstance hmod,
DWORD dwthreadid
);

The first parameter: Specifies the hook type. There are more than 10 types of hooks, such as wh_mouse and wh_keyboard (see msdn for details)
The second parameter indicates the entry address of the hook function.
Third parameter: handle of the module where the hook function is located;
The fourth parameter: the ID of the hook-related function is used to specify the thread to which the hook is to hook. If the value is 0, messages of the entire system are intercepted.

 

The specific implementation is as follows:

 

 Run VS 2005 to create an MFC Dialog Box program, and then add the Code:

1.Define a global hook handle:

 

Static Hhook hkb = NULL;

 

2.Define the hook callback function:

 

Code
Lresult callback keyboardproc ( Int Ncode, wparam, lparam)
{
If (DWORD) lparam & Zero X 40000000 ) && (Hc_action = Ncode ))
{
Switch (Wparam) // Keyboard key ID
{
// Press the keypad number key 1 to send the wm_command message to the first button.
Case Vk_numpad1:
{

Hwnd = (Theapp. m_pmainwnd) -> Getsafehwnd ();
: Sendmessage (hwnd, wm_command, makewparam (idok, bn_clicked), (lparam) (theapp. m_pmainwnd) -> Getdlgitem (idok) -> M_hwnd ));
Break ;
}
Case Vk_numpad2:
{
Hwnd = (Theapp. m_pmainwnd) -> Getsafehwnd ();

: Sendmessage (hwnd, wm_command, makewparam (idcancel, bn_clicked), (lparam) (theapp. m_pmainwnd) -> Getdlgitem (idcancel) -> M_hwnd ));
Break ;
}
Case Vk_numpad3:
{
Hwnd = (Theapp. m_pmainwnd) -> Getsafehwnd ();
: Sendmessage (hwnd, wm_command, makewparam (idc_button1, bn_clicked), (lparam) (theapp. m_pmainwnd) -> Getdlgitem (idc_button1) -> M_hwnd ));
Break ;
}
Case Vk_numpad4:
{
Hwnd = (Theapp. m_pmainwnd) -> Getsafehwnd ();
: Sendmessage (hwnd, wm_command, makewparam (idc_button2, bn_clicked), (lparam) (theapp. m_pmainwnd) -> Getdlgitem (idc_button2) -> M_hwnd ));
Break ;
}
Default :
Break ;
}
}
Lresult retval = Callnexthookex (hkb, ncode, wparam, lparam );
Return Retval;
}

 

3.In the dialog boxOninitdialog() Function callSetwindowshookexFunction to set the keyboard HOOK:

 

Hkb = Setwindowshookex (wh_keyboard, (hookproc) keyboardproc, theapp. m_hinstance, 0 );

 

4.Unmount the hook in the destructor in the dialog box:

 

If (Hkb)
Unhookwindowshookex (hkb );

 

Method 2 is simpler, that is, heavy loadPretranslatemessageThe Code is as follows:

 

Code
Bool ctestchardlg: pretranslatemessage (msg * PMSG)
{
// Todo: Add dedicated code and/or call the base class here
// If the keyboard is intercepted and the message is pressed, the key is analyzed and a message is sent to the corresponding button.
If (PMSG -> Message = Wm_keydown)
{
Uint Ikey = (Uint) PMSG -> Wparam;
Switch (Ikey)
{
Case Vk_numpad1:
Sendmessage (wm_command, makewparam (idc_button1, bn_clicked), (lparam) (getdlgitem (idc_button1) -> M_hwnd ));
: Setfocus (getdlgitem (idc_button1) -> M_hwnd );
Break ;
Case Vk_numpad2:
Sendmessage (wm_command, makewparam (idc_button2, bn_clicked), (lparam) (getdlgitem (idc_button2) -> M_hwnd ));
: Setfocus (getdlgitem (idc_button2) -> M_hwnd );
Break ;
Case Vk_numpad3:
Sendmessage (wm_command, makewparam (idc_button3, bn_clicked), (lparam) (getdlgitem (idc_button3) -> M_hwnd ));
: Setfocus (getdlgitem (idc_button3) -> M_hwnd );
Break ;
Case Vk_numpad4:
Sendmessage (wm_command, makewparam (idc_button4, bn_clicked), (lparam) (getdlgitem (idc_button4) -> M_hwnd ));
: Setfocus (getdlgitem (idc_button4) -> M_hwnd );
Break ;
Default :
Break ;
}

}

ReturnCdialog: pretranslatemessage (PMSG );
}

 

References:

<1>Use keyboard hooks to develop the key pronunciation program,Author: gdgf, http://www.vckbase.com/document/viewdoc? Id = 271

 

<2>Why the dialog box does not handle wm_char messages, http://topic.csdn.net/t/20030622/19/1944358.html

 

<3>Hook Function preliminary understanding, http://www.qqgb.com/Program/VC/VCZH/Program_54891.html

 

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.