55
66typedef void (__cdecl* GameUIHookProc_t)(int *);
77
8- static void __cdecl ToggleIME (int * lParam) {
9- auto keepState = lParam && *lParam != 0 ;
10- // enable IME or disable IME
11- HRESULT hr{};
12- ITfThreadMgr* pThreadMgr = nullptr ;
13- hr = CoCreateInstance (CLSID_TF_ThreadMgr, nullptr , CLSCTX_INPROC_SERVER , IID_ITfThreadMgr, (void **)&pThreadMgr);
14- if (FAILED (hr) || pThreadMgr == nullptr ) {
15- printf (" Failed to create ITfThreadMgr instance: %#X \n " , hr);
8+ static void __cdecl DeactiveKeyboard (int * lParam) {
9+ auto deactive = lParam && *lParam == 0 ;
10+
11+ if (!deactive) {
12+ return ;
13+ }
14+
15+ ITfThreadMgr2* pThreadMgr = nullptr ;
16+
17+ HRESULT hr = CoCreateInstance (CLSID_TF_ThreadMgr, nullptr , CLSCTX_INPROC_SERVER , IID_ITfThreadMgr2, (void **)&pThreadMgr);
18+
19+ if (!SUCCEEDED (hr)) {
1620 return ;
1721 }
18- TfClientId clientId = 0 ;
19- if (!keepState) {
22+
23+ DWORD dwFlags = 0 ;
24+ hr = pThreadMgr->GetActiveFlags (&dwFlags);
25+
26+ auto activated = (TF_TMF_ACTIVATED & dwFlags) != 0 ;
27+
28+ if (activated) {
2029 hr = pThreadMgr->Deactivate ();
2130 }
2231
2332 pThreadMgr->Release ();
2433}
25- static void __cdecl RequestTSF (int * open) {
26- auto shouldEnableTSF = open && *open != 0 ;
2734
28- if (!shouldEnableTSF) {
35+ static void __cdecl ActiveKeyboard (int * lParam) {
36+ auto active = lParam && *lParam != 0 ;
37+
38+ ITfThreadMgr2* pThreadMgr = nullptr ;
39+
40+ HRESULT hr = CoCreateInstance (CLSID_TF_ThreadMgr, nullptr , CLSCTX_INPROC_SERVER , IID_ITfThreadMgr2, (void **)&pThreadMgr);
41+
42+ if (!SUCCEEDED (hr)) {
2943 return ;
3044 }
3145
32- // enable TSF or disable TSF
33- HRESULT hr{} ;
46+ DWORD dwFlags = 0 ;
47+ hr = pThreadMgr-> GetActiveFlags (&dwFlags) ;
3448
35- ITfThreadMgr* pThreadMgr = nullptr ;
49+ auto activated = ( TF_TMF_ACTIVATED & dwFlags) != 0 ;
3650
37- hr = CoCreateInstance (CLSID_TF_ThreadMgr, nullptr , CLSCTX_INPROC_SERVER , IID_ITfThreadMgr, (void **)&pThreadMgr);
51+ TfClientId tfClientId = TF_CLIENTID_NULL ;
52+ if (!activated) {
53+ hr = pThreadMgr->Activate (&tfClientId);
3854
39- if (FAILED (hr) || pThreadMgr == nullptr ) {
40- printf (" Failed to create ITfThreadMgr instance: %#X \n " , hr);
41- return ;
42- }
55+ if (!SUCCEEDED (hr)) {
56+ return ;
57+ }
4358
44- TfClientId clientId = 0 ;
45- hr = pThreadMgr->Activate (&clientId);
59+ ITfDocumentMgr* pDocMgr = nullptr ;
4660
47- if (FAILED (hr)) {
48- printf (" Failed to %s TSF\n " , shouldEnableTSF ? " activate" : " deactivate" );
49- }
61+ hr = pThreadMgr->CreateDocumentMgr (&pDocMgr);
5062
51- pThreadMgr->Release ();
63+ ITfContext* context = nullptr ;
64+ TfEditCookie cookie{};
65+ hr = pDocMgr->CreateContext (tfClientId, 0 , nullptr , &context, &cookie);
66+
67+ if (!SUCCEEDED (hr)) {
68+ return ;
69+ }
70+ hr = pDocMgr->Push (context);
71+
72+ if (!SUCCEEDED (hr)) {
73+ return ;
74+ }
75+ hr = pThreadMgr->SetFocus (pDocMgr);
76+ }
5277}
5378
5479static void PatchAsync (LPARAM lParam) {
@@ -72,8 +97,8 @@ static void PatchAsync(LPARAM lParam) {
7297 pGameUIHooks = *(GameUIHookProc_t**)pGameUIHooksBase;
7398 }
7499
75- pGameUIHooks[31 ] = RequestTSF ;
76- pGameUIHooks[2 ] = ToggleIME ;
100+ pGameUIHooks[31 ] = ActiveKeyboard ;
101+ pGameUIHooks[2 ] = DeactiveKeyboard ;
77102
78103 printf (" TSF patch success\n " );
79104}
0 commit comments