Redefile two implementation methods of the keyboard
Contact: Dalian University of Technology Electronics 995 Class Sun Yuzhe
Email: sunyuzhe@263.net
Home: http://sunyuzhe.363.net
Tel: 0411-4702214
In the Windows operating system, if we want to redefine the keyboard, if you press a key, you can directly access the Internet. Press a key to close the window, etc., how to implement it! In Visual C , it is not possible to use a conventional Class Wizard method. Here we use two ways to implement it.
Method 1: Using the registerhotKey number implementation
Program implementation Principle: First, the user predetermined a hotkey, whether the program is a front program or a background program, as long as the user presses this button, performs our defined functions. The hotkey message WM_HOTKEY is captured in the program and knows which button is pressed by message parameters. Because the message WM_HOTKEY is not encapsulated in the ClassWizard in the VC, we only have a manual join code. The mapping and processing of the message.
The specific implementation steps are as follows:
l 1 Establish a project name: Hotkey based on Dialog Base, click Finish.
l 2 Declaring Hot Key Message Processing Function Prototype Added in HotKeydlg.h (after AFX_MSG font) adds the following statement (where the drawing portion is the code added):
// generated message map functions
// {{AFX_MSG (CHOTKEYDLG)
Virtual Bool OnInitdialog ();
AFX_MSG Void OnSysCommand (Uint Nid, LParam Lparam);
AFX_MSG void onpaint ();
AFX_MSG HCURSOR ONQUERYDRAGICON ();
LResult Onhotkey (WParam WPARAM, LPARAM LPARAM);
//}} AFX_MSG
Declare_message_map ()
The L 3 message is associated with the corresponding processing function to add message mapping macro in HotKeydlg.cpp, making the message to the corresponding processing function, adding the following statement (where the line portion is the code added):
Begin_MESSAGE_MAP (CHOTKEYDLG, CDIALOG)
// {{AFX_MSG_MAP (CHOTKEYDLG)
ON_WM_SYSCOMMAND ()
ON_WM_PAINT ()
ON_WM_QUERYDRAGICON ()
ON_MESSAGE (WM_HOTKEY, INHOTKEY)
//}} AFX_MSG_MAP
END_MESSAGE_MAP ()
L 4 For your convenience, you can use the ClassWizard in the ChoTKeyDLG class in advance to implement a function on the wordwizard response to WM_CREATE and WM_DESTROY messages, (using ClassWizard is easy to implement, please refer to books about VC, here Take it again).
l 5 Register the hotkey to the system
The following code is added to the oncreate () function to register the hotkey to the system, and the hotkey of this example is set to ESC.
RegisterhotKey (M_HWND, 1001, NULL, VK_ESCAPE);
// Function parameters Please refer to books about VC, and will not repeat it here
L 6 processing hotkey
The hotkey is processed in the message processing function on /otkey (), and the program code that the user wants to run, etc .: (Note The code is completely joined by hand-added code in HotKeydlg.cpp)
Lresult ChotKeydlg :: Onhotkey (WPARAM WPARAM, LPARAM LPARAM) {
Hwnd hwnd;
HWnd = :: getForegroundWindow ();
:: PostMessage (HWND, WM_CLOSE, 0, 0);
Return 0;
}
The L 7 program is running after the heat key is released.
Release the system resources in OnDestroy () to release the system resources through UnregisterhotKey ().
UnregisterhotKey (M_HWND, 1001);
l 8 After compiling and running the program, you will close the current window when you press the hotkey ESC.
Method 2: Implement the number of keyboard hooks
Speaking of hook functions, there may be very strange people in many beginners, I will say a few words here:
WINDOW message processing mechanism provides hooks in order to monitor the various event messages of the system in the application
Various reverse modulation function (hook) features. This hook function (hook) is similar to the interrupt driver, hook
You can hook a plurality of reverse modulation functions constitute a mounted function chain. The various messages generated by the system are first sent to various
Mount function, the mount function is monitored, modified, and controls the message according to the respective functions, and then pays the control
Delivery or transfer messages to the next hiking function, resulting in a window function. This reaction of the Window system
Although the adjustment of the function is slightly affected by the system's operational efficiency, it is very useful in many cases.
By reasonably efficiently utilizing the hook function monitoring mechanism that can be used to achieve a good effect.
l 1 The filter function that can be hung under Window includes 11 types:
Filter function of the Wh_CallWndProc window function
WH_CBT computer training filter function
WH_Debug debugging filter function
WH_GetMessage Get Message Filtering Function
Wh_hardware hardware message filter function
WH_JournalPlayback message playback filter function
WH_JournalRecord message logging filter function
WH_MOUSE mouse filter function
WH_MSGFilter message filtering function
WH_SYSMSGFILTER system message filter function
WH_keyboard keyboard filter function
Wh_keyboard_ll's keyboard filter function available above WindowsNT4.0
WH_Mouse_LL mouse filter function available above WindowsNT4.0
l 2 where the keyboard filtering function is the most commonly used filter function, no matter which type of filtering
Number, the basic methods of their hill are the same.
When the Window calls the mounted reverse call function, the function of the hanging-chain first is always called, so the keyboard must be hung.
The hook function uses a function setWindowsHooKex () to hook it in the function chain. As for whether the message is passed to the letter
The next function of the digital chain is determined by each specific function function, if the message needs to be traditionally given to the next letter
Number, you can call the CallNexThooKex () of the API function, if you do not pass the direct return.
l You can use the function setWindowshookex () in the program to mount the filter function, you must refer to the function
The type of the mount function, the entry address of the function, and the function are global or partial, and the connector
The specific call format of the number is as follows:
HHOOK SETWINDOWSHOKEX (Int IDHOK, HOOKPROC LPFN, HINSTANCE HMOD, DWORD DWTHREADID);
Where the first parameter is the type of hook; the second parameter is the address of the hook function; the third parameter is package
Module handle with hook function; the fourth parameter specifies the monitoring thread. If you specify a determined thread,
Thread special hook; if specified is empty, it is a global hook. Where the global hook function must be included in the DLL (dynamic link library), and the thread dedicated hook can also be included in the executable. Get control of hooks
After the function is completed, if you want the message to continue to pass, it must call another one.
The API function in the SDK is called to pass it. The hook function can also be lost by directly returning True.
Abandon this message and block the delivery of the message.
If the function is global, it must be placed in a DLL (dynamic link library), but I found out
In Window 2000, you can make a global keyboard function without writing a DLL (Dynamic Link Library). Use it to do a lot.
The UnHookWindowsHooKex (Int IDHOK) function enables uninstalling of the hook.
l We start writing the program! Establish an engineering name: KBoardHook based on Dialog Base, Click Finish.
l Adding on the top of kboardhookdlg.cpp
HHOOK HHKLOWLEVELKYBD2000;
The hhklowlevelkybd2000 is a global variable.
l For convenience of operations in advance in the CKBoardHookDLG class, use ClassWizard to implement a function on response to WM_CREATE and WM_DESTROY messages on a framework of oncreate () and onDestroy ().
l In the oncreate () function, the hook code is suspended by SETWINDOWSHOKEX and the system is as follows.
hhklowlevelkybd2000 = setwindowshookex (wh_keyboard_ll,
LowlevelKeyboardProc, AFXGetApp () -> M_HINSTANCE, 0);
l In OnDestroy (), unhookWindowsHookex () released the hook, release system resources, code as follows: UnhookWindowsHookex (HHKLOWLEVELKYBD2000);
l Declare LowlevelKeyboardProc in KBoardHookdlg.h, add the following code in Class CkboardHookdlg: Public CDialog
LResult Callback LowlevelKeyboardProc (int Ncode, WPARAM WPARAM, LPARAM LPARAM);
// When Ncode is 0 (there is a definition in WinUser.h: #define hc_action 0) WPARAM, LPARAM contains the keyboard information, WPARAM's value represents the message of the keyboard can be WM_KeyDown.
WM_KEYUP, LPARAM is a pointer to KBDLLHOOKSTRUCT.
l Adding the following code in kboardhookdlg.cpp (this code is hand-added, can not use ClassWZRD)
LResult Callback LowlevelKeyboardProc (int Ncode, WPARAM WPARAM, LPARAM LPARAM)
{
PkbdllhookStruct P = (pkbdllhookingtruct) lparam;
IF (ncode == hc_action) {
INT Vkey = Lobyte (P-> VKCode);
Switch (WPARAM)
{
Case WM_Keydown: {
IF (vkey == 27)
{
Hwnd hwnd;
HWnd = :: getForegroundWindow ();
:: PostMessage (HWND, WM_CLOSE, 0, 0);
}
}
}
}
Return CallNexthookex (NULL, NCODE, WPARAM, LPARAM);
}
At this time, the compiler has a total of 6
The first two faults are
Error C2065: 'wh_keyboard_ll': undeclared Identifier
Error C2065: 'PkbdllhookStruct': undeclared identifier
Look at MSDN clearly said that there is also a definition in Winuser.h, I tried to join #include "winuser.h" in front of KBoardHookdlg.cpp, but the result is still the same, let's chasing it in the end, look at Winuser.h I found that I will define wh_keyboard_ll and pkbdllhookstruct inside, but it is not easy to use, what should I do? Find them, copy it to KBoardHookDlg.h Central Plains, first hand-added definitions LowlevelKeyboardProc function (below is the code we have to copy)
#define wh_keyboard_ll 13
Typedef struct tagkbdllhookstruct {
DWORD VKCODE;
DWORD scancode;
DWORD FLAGS;
DWORD TIME;
DWORD DWEXTRAINFO;
} KBDLLHOOKSTRUCT, FAR * LPKBDLLHOOKSTRUCT, * PKBDLLHOOKSTRUCT
At this time, the program is compiled and the program can run. This way we also achieve the purpose of changing the keyboard.
Summary, the above two methods are different principles, and their loading methods are not the same, and for the first method, it can be universal in Win98 / Win2000 / WinXP, but for the second method, it can only be in Win2000. / WinXP application, if the application is written as a DLL in Win98, it is more cumbersome, but its function is powerful because it is hooks with the system, so the user will trigger it in any window, for example We want to shield the keyboard, just put the LowlevelKeyboardProc function
Return CallNexthookex (NULL, NCODE, WPARAM, LPARAM);
Change to Return True;
You can shield all buttons other than Ctrl Alt DEL.
As for why it defines the two quantities in Winuser.h but says that it is not defined, I also have to solve it, I hope that I will write this article to throw jade, and inspire you to learn VC interest.
Note (the author agrees with the work of the work and the online version)
Contact: Dalian University of Technology Electronics 995 Class Sun Yuzhe
Email: sunyuzhe@263.net
Home: http://sunyuzhe.363.net
Tel: 0411-4702214