-
Notifications
You must be signed in to change notification settings - Fork 542
/
KeyEventSink.cpp
171 lines (149 loc) · 4.4 KB
/
KeyEventSink.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
#include "stdafx.h"
#include "WeaselIPC.h"
#include "WeaselTSF.h"
#include "KeyEvent.h"
#include "CandidateList.h"
void WeaselTSF::_ProcessKeyEvent(WPARAM wParam, LPARAM lParam, BOOL* pfEaten) {
if (!_IsKeyboardOpen() || _IsKeyboardDisabled()) {
*pfEaten = FALSE;
return;
}
// if server connection is Not OK, don't eat it.
if (!_EnsureServerConnected()) {
*pfEaten = FALSE;
return;
}
weasel::KeyEvent ke;
GetKeyboardState(_lpbKeyState);
if (!ConvertKeyEvent(static_cast<UINT>(wParam), lParam, _lpbKeyState, ke)) {
/* Unknown key event */
*pfEaten = FALSE;
} else {
// cheet key code when vertical auto reverse happened, swap up and down
if (_cand->GetIsReposition()) {
if (ke.keycode == ibus::Up)
ke.keycode = ibus::Down;
else if (ke.keycode == ibus::Down)
ke.keycode = ibus::Up;
}
*pfEaten = (BOOL)m_client.ProcessKeyEvent(ke);
}
}
STDAPI WeaselTSF::OnSetFocus(BOOL fForeground) {
if (fForeground)
m_client.FocusIn();
else {
m_client.FocusOut();
_AbortComposition();
}
return S_OK;
}
/* Some apps sends strange OnTestKeyDown/OnKeyDown combinations:
* Some sends OnKeyDown() only. (QQ2012)
* Some sends multiple OnTestKeyDown() for a single key event. (MS WORD 2010
* x64)
*
* We assume every key event will eventually cause a OnKeyDown() call.
* We use _fTestKeyDownPending to omit multiple OnTestKeyDown() calls,
* and for OnKeyDown() to check if the key has already been sent to the server.
*/
STDAPI WeaselTSF::OnTestKeyDown(ITfContext* pContext,
WPARAM wParam,
LPARAM lParam,
BOOL* pfEaten) {
_fTestKeyUpPending = FALSE;
if (_fTestKeyDownPending) {
*pfEaten = TRUE;
return S_OK;
}
_ProcessKeyEvent(wParam, lParam, pfEaten);
_UpdateComposition(pContext);
if (*pfEaten)
_fTestKeyDownPending = TRUE;
return S_OK;
}
STDAPI WeaselTSF::OnKeyDown(ITfContext* pContext,
WPARAM wParam,
LPARAM lParam,
BOOL* pfEaten) {
_fTestKeyUpPending = FALSE;
if (_fTestKeyDownPending) {
_fTestKeyDownPending = FALSE;
*pfEaten = TRUE;
} else {
_ProcessKeyEvent(wParam, lParam, pfEaten);
_UpdateComposition(pContext);
}
return S_OK;
}
STDAPI WeaselTSF::OnTestKeyUp(ITfContext* pContext,
WPARAM wParam,
LPARAM lParam,
BOOL* pfEaten) {
_fTestKeyDownPending = FALSE;
if (_fTestKeyUpPending) {
*pfEaten = TRUE;
return S_OK;
}
_ProcessKeyEvent(wParam, lParam, pfEaten);
_UpdateComposition(pContext);
if (*pfEaten)
_fTestKeyUpPending = TRUE;
return S_OK;
}
STDAPI WeaselTSF::OnKeyUp(ITfContext* pContext,
WPARAM wParam,
LPARAM lParam,
BOOL* pfEaten) {
_fTestKeyDownPending = FALSE;
if (_fTestKeyUpPending) {
_fTestKeyUpPending = FALSE;
*pfEaten = TRUE;
} else {
_ProcessKeyEvent(wParam, lParam, pfEaten);
if (!_async_edit)
_UpdateComposition(pContext);
}
return S_OK;
}
STDAPI WeaselTSF::OnPreservedKey(ITfContext* pContext,
REFGUID rguid,
BOOL* pfEaten) {
*pfEaten = FALSE;
return S_OK;
}
BOOL WeaselTSF::_InitKeyEventSink() {
com_ptr<ITfKeystrokeMgr> pKeystrokeMgr;
HRESULT hr;
if (_pThreadMgr->QueryInterface(&pKeystrokeMgr) != S_OK)
return FALSE;
hr = pKeystrokeMgr->AdviseKeyEventSink(_tfClientId, (ITfKeyEventSink*)this,
TRUE);
return (hr == S_OK);
}
void WeaselTSF::_UninitKeyEventSink() {
com_ptr<ITfKeystrokeMgr> pKeystrokeMgr;
if (_pThreadMgr->QueryInterface(&pKeystrokeMgr) != S_OK)
return;
pKeystrokeMgr->UnadviseKeyEventSink(_tfClientId);
}
BOOL WeaselTSF::_InitPreservedKey() {
return TRUE;
#if 0
com_ptr<ITfKeystrokeMgr> pKeystrokeMgr;
if (_pThreadMgr->QueryInterface(pKeystrokeMgr.GetAddressOf()) != S_OK)
{
return FALSE;
}
TF_PRESERVEDKEY preservedKeyImeMode;
/* Define SHIFT ONLY for now */
preservedKeyImeMode.uVKey = VK_SHIFT;
preservedKeyImeMode.uModifiers = TF_MOD_ON_KEYUP;
auto hr = pKeystrokeMgr->PreserveKey(
_tfClientId,
GUID_IME_MODE_PRESERVED_KEY,
&preservedKeyImeMode, L"", 0);
return SUCCEEDED(hr);
#endif
}
void WeaselTSF::_UninitPreservedKey() {}