First, the preface is pronounced, the key is to let the program can know which key is pressed on the current keyboard, and play the corresponding sound, the own procedure is of course not in the words, then other programs are currently pressing which key is I know? ? Use the keyboard hook to solve it well.
Download all source code size: 552K
Second, the basic principle of hooks Windows calls the tempering function, first call the function of the function chain first, we only need to place your own callback function to the chain, the backup function will be called first. So how do we take our own callback function to the link of the function chain? The function setWindowsHooKex () is implemented. Let's take a look at the prototype of the SetWindowsHookex function:
HHOOK SETWINDOWSHOKEX (int IDHOK, HOOKPROC LPFN, HINSTANCE HMOD, DWORD DWTHREADID); First Parameters: Specify Types of Hook, WH_Mouse, WH_Keyboard, etc. (Specific See MSDN) Second Parameters: Identifies the entrance address of the hook function The third parameter: the handle of the module where the hook function is located; the fourth parameter: the id of the hook-related function is used to specify which thread wants to hook the hook, and intercept the entire system.
It is also important to note that in order to capture all events, the hook function should be placed in the dynamic link library DLL.
Third, if the specific implementation theory, there is not much to say, run VC 6.0, create a new MFC AppWizard (DLL) project, name HOOK, use the default to create a DLL type option, that is, use the shared MFC DLL, click after completion Start writing code:
(1) Define global functions in hook.h
Bool installhook (); // Hook Mount LRESULT CALLBACK KeyboardProc (int Ncode, WPARAM WPARAM, LPARAM LPARAM); // Hook Function
(2) Add the code to define the global variable hook to the #ENDIF in the hook.cpp file:
Static HHOOK HKB = NULL; Hinstance Hins; // Hook Hook Function The module of the module (3) adds a core code
BOOL InstallHook () {hkb = setwindowshookex (wh_keyboard, (hookProc) KeyboardProc, hins, 0); return true;} The first parameter specifies the type of hook because we only use the keyboard operation, so set it to wh_keyboard; second The parameter specifies the entry address of the hook function to KeyboardProc, then call this function when the hook hooks to any message, ie when there is a keyboard input, the keyboard input is immediately caused by keyboard input, the third parameter is the module of the hook function. Handle; the last parameter is the ID of the hook-related function to specify which thread wants to keep the hook to hook which thread is 0; now, now, start definite when the key on the keyboard is going to do what the program is going to do ~ KeyboardProc Action:
LRESULT CALLBACK KeyboardProc (int nCode, WPARAM wParam, LPARAM lParam) {if (((lParam DWORD) & 0x40000000) && (HC_ACTION == nCode)) {switch (wParam) // keyboard identifier {case '' 1 '': sndPlaySound ( "1.wav", SND_ASYNC); Break; // When the numeric key 1 is pressed Case '' '2' ': SndPlaySound ("2.Wav", SND_ASYNC); Break; Case' '3': SNDPlaySound (" 3.wav ", SND_ASYNC); Break; Case '' 4 ': SndPlaySound (" 4.wav ", SND_ASYNC); Break; .... Case' 'A' ': SNDPLAYSOUND (" a.wav ", SND_ASYNC ); Break; // When the letter key A is pressed Case '' B '': SNDPLAYSOUND ("B.WAV", SND_ASYNC); Break; Case '' C '': SNDPlaySound ("C.WAV", SND_ASYNC) Break; Case '' D '': SNDPLAYSOUND ("D.WAV", SND_ASYNC); Break; ....}} LRESULT RETVAL = CallNexthooKex (HKB, NCODE, WPARAM, LPARAM); Return RetVal;} The above code In our use of play sound as a button, the active path of the first parameter defined by the API function SNDPlaySound (, for example, as a.wav to play under the C is defined "C: // a.wav "); Second parameter definition play mode, SND_ASYNC mode can release the sound file being played in time, stop the current sound to play the new sound, so there will be no blocking when we continue to hit I feel. In order to execute the SNDPLAYSOUND function, you must add: #include "MMS in HOOK.CPP YSTEM.H "and click" Project "on the VC menu -" Set "Enter the link property page, entered under the L object / library module: Winmm.lib is determined after you can. (4) Add output ID in hook.def End of the end
InsTallHookKeyboardProc is short, the keyboard hook is completed, and the compiled DLL file can be called freely with other programs. How do DLLs in the program? That is simple. Use VC 6.0 to create a new MFC AppWizard (EXE) project, named Keysound, click "OK" to select the program type as dialog box, click OK to click OK. On the keysounddlg.cpp file OnInitDialog () CDIALOG :: OnItDialog () of the initialization function ();
/ / The preventer from repeatedly residing in memory, but also errors in order to prevent two programs from reading the DLL.
CreateMutex (NULL, FALSE, "KeySound"); if (GetLastError () == ERROR_ALREADY_EXISTS) OnOK (); // read DLLstatic HINSTANCE hinstDLL; typedef BOOL (CALLBACK * inshook) (); inshook instkbhook; if (hinstDLL = LoadLibrary ((LPCTSTR) "Hook.dll")) {Instkbhook = (InShook) GetProcaddress (Hinstdll, "InstallHook); instkbhook ();} else {MessageBox (" Current Directory Can't find hook.dll file, program initialization failed " ); Onok ();} After the build-generated keysound.exe and hook.dll are placed in the same directory, define a sound file, run Keysound.exe, turn on the notepad or write board, experience the system to quickly quickly Read the pleasure of every button you press, ^ - ^
One thing must be explained, the standard keyboard has 101 keys, you want how many keys, you must define how many keys in the keyboardproc action above, 10 commonly used 10 numbers and 26 English letters will not bring you Too big, as long as the corresponding '' a '' corresponds to the A key, '' 1 '' corresponds to 1 button, but if you want more keys to have a variety of special music, it is likely to meet To some keyboard coding, such as the ESC button, you can't make it easy to use '' esc '' ', you have to use vk_escape, such as the Alt key to define it, without a keyboard coded table, will be quite a headache Here I introduce a method to let the program tell you the keyboard key name: Add a PretranslateMessage mapping for a project, add the following code:
Char Keyname [50]; ZeromeMory (Keyname, 50); if (pmsg -> message == wm_keydown) {getKeyNameText (PMSG-> LPARAM, Keyname, 50); MessageBox (keyName);} So when the program window is displayed Press a key to pop up a message displaying the name of the key, then use '' '', you can get up, such as the comma, '', '' and '' '', simple :) Here, all completed the key pronunciation program, by changing the name of the sound file without using the change program itself, it can replace the button sound, but there is a regret, the location of the sound file is in the hard disk cannot be changed, from C Can't play the moving D disk program, how can I flexibly read the sound file? You can use the API function getModuleFileName to get the directory where the program is located, the specific implementation method is as follows: (1) PUBLIC in hook.h: Add:
Bool InitInstance (); // Initialization Function (2) Add the code to define the global variable under the #ENDIF of hook.cpp:
Char szbuf [256]; char * p; cstring msg; (3) Adding appropriate positions in hook.cpp:
BOOL CHookApp :: InitInstance () {hins = AfxGetInstanceHandle (); GetModuleFileName (AfxGetInstanceHandle (), szBuf, sizeof (szBuf)); p = szBuf; while (strchr (p, '' // '')) {p = strchr (p, '' // '); p ;} * p =' '/ 0'; msg = szbuf; return true;} (4) New folder and named Sound; (5) Change the sound file Physical location definition method
Case '' 1 '': SNDPlaySound (MSG "Sound // 1.Wav", SND_ASYNC); Break; MSG is a directory where the program is currently located, plus the following code means playing 1 in the current directory in the current directory. 1. The WAV file, which change the absolute path of the sound file to a flexible relative path. You only need to put the keySound.exe, hook.dll and the sound folder under the same folder, will only move the entire folder. Any movement of the sound file.
Note when debugging: Place the hook.dll, the Sound directory in the execution directory of Keysound.exe. If you appear in the compilation link, you will add information such as UnreSolved External Symbol __3, please add WinMM.LIB in Project Settings.
Using the keyboard hook pronunciation key development procedures Author: GDGF Source: ttp: //www.vckbase.com/
Download all source code size: 552K