BOOL SendText( LPCTSTR lpctszText ) { std::vector<INPUT> EventQueue; TCHAR Buff[120 * sizeof(TCHAR)] = {0}; GetLocaleInfo(LOCALE_USER_DEFAULT, LOCALE_ILANGUAGE, Buff, sizeof(Buff)); HKL hKeyboardLayout = ::LoadKeyboardLayout( Buff, KLF_ACTIVATE ); const size_t Len = wcslen( lpctszText ); for( size_t Index = 0; Index < Len; ++Index ) { INPUT Event = { 0 }; const SHORT Vk = VkKeyScanEx(lpctszText[Index], hKeyboardLayout); const UINT VKey = ::MapVirtualKey( LOBYTE( Vk ), 0 ); if( HIBYTE( Vk ) == 1 ) // Check if shift key needs to be pressed for this key { // Press shift key ::ZeroMemory( &Event, sizeof( Event )); Event.type = INPUT_KEYBOARD; Event.ki.dwFlags = KEYEVENTF_SCANCODE; Event.ki.wScan = ::MapVirtualKey( VK_LSHIFT, 0 ); EventQueue.push_back( Event ); } // Keydown ::ZeroMemory( &Event, sizeof( Event )); Event.type = INPUT_KEYBOARD; Event.ki.dwFlags = KEYEVENTF_SCANCODE; Event.ki.wScan = VKey; EventQueue.push_back( Event ); // Keyup ::ZeroMemory( &Event, sizeof( Event )); Event.type = INPUT_KEYBOARD; Event.ki.dwFlags = KEYEVENTF_SCANCODE | KEYEVENTF_KEYUP; Event.ki.wScan = VKey; EventQueue.push_back( Event ); if( HIBYTE( Vk ) == 1 )// Release if previously pressed { // Release shift key ::ZeroMemory( &Event, sizeof( Event )); Event.type = INPUT_KEYBOARD; Event.ki.dwFlags = KEYEVENTF_SCANCODE| KEYEVENTF_KEYUP; Event.ki.wScan = ::MapVirtualKey( VK_LSHIFT, 0 ); EventQueue.push_back( Event ); } }// End for if( hKeyboardLayout ) { UnloadKeyboardLayout( hKeyboardLayout ); } return static_cast<BOOL>(::SendInput( static_cast<UINT>(EventQueue.size()), &EventQueue[0], sizeof( INPUT ))); }
int main(int argc, char* argv[]) { //e0210804 sougou //e0200804 google setlocale(LC_CTYPE, "chs"); printf("%ls\n", L"输入法设置 0.1 - 设置搜狗为系统默认输入法"); printf("%ls\n", L"Copyright (C) 2013 qiyi ([email protected])"); HKL hkl = LoadKeyboardLayout("e0210804", KLF_ACTIVATE); SystemParametersInfo(SPI_SETDEFAULTINPUTLANG, 0, &hkl,SPIF_SENDCHANGE); UnloadKeyboardLayout(hkl); return 0; }
static void test_ImmGetDescription(void) { HKL hkl; WCHAR japime[] = { 'E', '0', '0', '1', '0', '4', '1', '1', 0 }; WCHAR descW[100]; CHAR descA[100]; UINT ret, lret; /* FIXME: invalid keyboard layouts should not pass */ ret = ImmGetDescriptionW(NULL, NULL, 0); todo_wine ok(!ret, "ImmGetDescriptionW failed, expected 0 received %d.\n", ret); /* load a language with valid IMM descriptions */ hkl = LoadKeyboardLayoutW(japime, KLF_ACTIVATE); todo_wine ok(hkl != 0, "LoadKeyboardLayoutW failed, expected != 0.\n"); ret = ImmGetDescriptionW(hkl, NULL, 0); if(!ret) { win_skip("ImmGetDescriptionW is not working for current loaded keyboard.\n"); return; } ret = ImmGetDescriptionW(hkl, descW, 0); ok(ret, "ImmGetDescriptionW failed, expected != 0 received 0.\n"); lret = ImmGetDescriptionW(hkl, descW, ret + 1); ok(lret, "ImmGetDescriptionW failed, expected != 0 received 0.\n"); ok(lret == ret, "ImmGetDescriptionW failed to return the correct amount of data. Expected %d, got %d.\n", ret, lret); lret = ImmGetDescriptionA(hkl, descA, ret + 1); ok(lret, "ImmGetDescriptionA failed, expected != 0 received 0.\n"); todo_wine ok(lret == ret, "ImmGetDescriptionA failed to return the correct amount of data. Expected %d, got %d.\n", ret, lret); ret /= 2; /* try to copy partially */ lret = ImmGetDescriptionW(hkl, descW, ret + 1); ok(lret, "ImmGetDescriptionW failed, expected != 0 received 0.\n"); ok(lret == ret, "ImmGetDescriptionW failed to return the correct amount of data. Expected %d, got %d.\n", ret, lret); ret = ImmGetDescriptionW(hkl, descW, 1); ok(!ret, "ImmGetDescriptionW failed, expected 0 received %d.\n", ret); UnloadKeyboardLayout(hkl); }
/* * Writes any changes in input methods to the registry */ VOID InputList_Process(VOID) { INPUT_LIST_NODE *pCurrent; DWORD dwIndex; /* Process deleted and edited input methods */ for (pCurrent = _InputList; pCurrent != NULL; pCurrent = pCurrent->pNext) { if ((pCurrent->wFlags & INPUT_LIST_NODE_FLAG_DELETED) || (pCurrent->wFlags & INPUT_LIST_NODE_FLAG_EDITED)) { if (UnloadKeyboardLayout(pCurrent->hkl)) { /* Only unload the edited input method, but does not delete it from the list */ if (!(pCurrent->wFlags & INPUT_LIST_NODE_FLAG_EDITED)) { InputList_RemoveNode(pCurrent); } } } } InputList_PrepareUserRegistry(); /* Find default input method */ for (pCurrent = _InputList; pCurrent != NULL; pCurrent = pCurrent->pNext) { if (pCurrent->wFlags & INPUT_LIST_NODE_FLAG_DEFAULT) { InputList_AddInputMethodToUserRegistry(1, pCurrent); break; } } if (SystemParametersInfoW(SPI_SETDEFAULTINPUTLANG, 0, (LPVOID)((LPDWORD)&pCurrent->hkl), 0)) { DWORD dwRecipients; dwRecipients = BSM_ALLCOMPONENTS; BroadcastSystemMessageW(BSF_POSTMESSAGE, &dwRecipients, WM_INPUTLANGCHANGEREQUEST, 0, (LPARAM)pCurrent->hkl); } /* Add methods to registry */ dwIndex = 2; for (pCurrent = _InputList; pCurrent != NULL; pCurrent = pCurrent->pNext) { if (pCurrent->wFlags & INPUT_LIST_NODE_FLAG_DEFAULT) continue; InputList_AddInputMethodToUserRegistry(dwIndex, pCurrent); dwIndex++; } }
LRESULT MainDialogProc(HWND hDlg, UINT Msg, WPARAM wParam, LPARAM lParam) { HKL hKl; switch (Msg) { case WM_INITDIALOG: { WCHAR Buf[255]; UpdateData(hDlg); hMainDlg = hDlg; SubclassWnd(GetDlgItem(hDlg, IDC_LIST), L"List"); SubclassWnd(GetDlgItem(hDlg, IDC_EDIT1), L"Edit1"); SubclassWnd(GetDlgItem(hDlg, IDC_KLID), L"Klid"); SubclassWnd(GetDlgItem(hDlg, ID_CANCEL), L"CancelB"); SubclassWnd(GetDlgItem(hDlg, IDC_ACTIVATE), L"ActivateB"); SubclassWnd(GetDlgItem(hDlg, IDC_REFRESH), L"RefreshB"); SubclassWnd(GetDlgItem(hDlg, IDC_UNLOAD), L"UnloadB"); SubclassWnd(GetDlgItem(hDlg, IDC_LOAD), L"LoadB"); CheckRadioButton(hDlg, IDC_FROMLIST, IDC_FROMEDIT, IDC_FROMLIST); SetWindowText(GetDlgItem(hDlg, IDC_KLID), L"00000419"); swprintf(Buf, L"Current thread id: %d", GetCurrentThreadId()); SetWindowText(GetDlgItem(hDlg, IDC_CURTHREAD), Buf); SetWindowText(GetDlgItem(hDlg, IDC_DELAY), L"0"); return 0; } /* WM_INITDIALOG */ case WM_COMMAND: { switch(LOWORD(wParam)) { case ID_CANCEL: { EndDialog(hDlg, ERROR_CANCELLED); break; } case IDC_ACTIVATE: { if((hKl = GetActivateHandle(hDlg)) != INVALID_HANDLE_VALUE) { Sleep(GetDelayMilliseconds(hDlg)); if(!(hKl = ActivateKeyboardLayout(hKl, GetActivateFlags(hDlg)))) FormatBox(hDlg, MB_ICONERROR, L"Error", L"ActivateKeyboardLayout() failed. %d", GetLastError()); else UpdateData(hDlg); //FormatBox(hDlg, 0, L"Activated", L"Prev - %x, err - %d.", hKl, // GetLastError()); } else MessageBox(hDlg, L"No item selected", L"Error", MB_ICONERROR); break; } case IDC_UNLOAD: { if((hKl = GetSelectedLayout(hDlg)) != INVALID_HANDLE_VALUE) { Sleep(GetDelayMilliseconds(hDlg)); if(!UnloadKeyboardLayout(hKl)) FormatBox(hDlg, MB_ICONERROR, L"Error", L"UnloadKeyboardLayout() failed. %d", GetLastError()); else UpdateData(hDlg); } else MessageBox(hDlg, L"No item selected", L"Error", MB_ICONERROR); break; } case IDC_LOAD: { WCHAR buf[255]; GetWindowText(GetDlgItem(hDlg, IDC_KLID), buf, sizeof(buf)); Sleep(GetDelayMilliseconds(hDlg)); if(!LoadKeyboardLayout(buf, GetLoadFlags(hDlg))) FormatBox(hDlg, MB_ICONERROR, L"Error", L"LoadKeyboardLayout() failed. %d", GetLastError()); else UpdateData(hDlg); break; } case IDC_REFRESH: { UpdateData(hDlg); break; } case IDC_NEWTHREAD: { if(!CreateThread(NULL, 0, ThreadProc, NULL, 0, NULL)) { FormatBox(hDlg, MB_ICONERROR, L"Error!", L"Can not create thread (%d).", GetLastError()); } } case IDC_LIST: { if(HIWORD(wParam) == LBN_SELCHANGE) { WCHAR buf[25]; if((hKl = GetSelectedLayout(hDlg)) != NULL) { swprintf(buf, L"%x", hKl); SetWindowText(GetDlgItem(hDlg, IDC_HANDLE), buf); } } break; } } return TRUE; } /* WM_COMMAND */ case WM_INPUTLANGCHANGE: { FormatMsg(L"dlg WM_INPUTLANGCHANGE lParam=%x wParam=%x\n", lParam, wParam); return FALSE; } case WM_INPUTLANGCHANGEREQUEST: { FormatMsg(L"dlg WM_INPUTLANGCHANGEREQUEST lParam=%x wParam=%x\n", lParam, wParam); UpdateData(hDlg); return FALSE; } case WM_CLOSE: { EndDialog(hDlg, ERROR_CANCELLED); return TRUE; } /* WM_CLOSE */ default: return FALSE; } }
int APIENTRY WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPTSTR lpCmdLine, int nCmdShow) { HKL kl; MessageBox(NULL,"DEBUG\n","Debug",0); HKEY hk = NULL; if( ERROR_SUCCESS != RegCreateKeyEx( HKEY_LOCAL_MACHINE, "SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Uninstall\\New Chewing IM", 0, NULL, 0, KEY_ALL_ACCESS , NULL, &hk, NULL) ) hk = NULL; printf("DEBUG\n"); if( strstr( lpCmdLine, "/uninstall" ) ) { char temp[1024]; _gen_event_name(temp, sizeof(temp), "ChewingServer"); HWND hwnd = FindWindow(temp, NULL); if ( hwnd ) { SendMessage(hwnd, WM_DESTROY, 0, 0); } if( hk ) { DWORD type = REG_DWORD, size = sizeof(DWORD); if( ERROR_SUCCESS == RegQueryValueEx( hk, "KeyboardLayout", 0, &type, (LPBYTE)&kl, &size ) ) { UnloadKeyboardLayout( kl ); char klstr[10]; wsprintf( klstr, "%X", kl ); char regpath[256]; lstrcpy( regpath, "Keyboard Layout\\Preload" ); HKEY hk2 = NULL; // Windows NT only, 9x will be supported in the future if( (GetVersion() < 0x80000000) ) { if( ERROR_SUCCESS == RegOpenKey( HKEY_CURRENT_USER, regpath, &hk2 ) ) { for( int i = 1; i <= 100; ++i ) { char num[4]; wsprintf( num, "%d", i ); type = REG_SZ; size = sizeof(regpath); if( ERROR_SUCCESS != RegQueryValueEx( hk2, num, 0, &type, (LPBYTE)regpath, &size ) ) continue; if( 0 == lstrcmp( regpath, klstr ) ) { RegDeleteValue( hk2, num ); break; } } RegCloseKey(hk2); } } wsprintf( regpath, "SYSTEM\\CurrentControlSet\\Control\\Keyboard Layouts\\%s", klstr ); RegDeleteKey( HKEY_LOCAL_MACHINE, regpath ); } } } else if ( strstr( lpCmdLine, "/privilege" ) ) { enable_access("ch_index.dat"); enable_access("dict.dat"); enable_access("us_freq.dat"); enable_access("ph_index.dat"); enable_access("fonetree.dat"); } else { char path[MAX_PATH]; GetSystemDirectory(path, MAX_PATH); lstrcat( path, "\\Chewing.ime" ); printf("Install Path:%s\n",path); kl = ImmInstallIME( path, (GetVersion() < 0x80000000) ? "中文 (繁體) - 新酷音輸入法" : "新酷音輸入法" ); printf("Imm Install IME Result: %d\n",kl); if( hk ) RegSetValueEx( hk, "KeyboardLayout", 0, REG_DWORD, (LPBYTE)&kl, sizeof(DWORD) ); } RegCloseKey( hk ); return 0; }