-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathKeyEventSink.cpp
More file actions
454 lines (384 loc) · 12.6 KB
/
KeyEventSink.cpp
File metadata and controls
454 lines (384 loc) · 12.6 KB
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
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
//////////////////////////////////////////////////////////////////////
//
// THIS CODE AND INFORMATION IS PROVIDED "AS IS" WITHOUT WARRANTY OF
// ANY KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING BUT NOT LIMITED
// TO THE IMPLIED WARRANTIES OF MERCHANTABILITY AND/OR FITNESS FOR A
// PARTICULAR PURPOSE.
//
// Copyright (C) 2003 Microsoft Corporation. All rights reserved.
//
// KeyEventSink.cpp
//
// ITfKeyEventSink implementation.
//
//////////////////////////////////////////////////////////////////////
#include "Globals.h"
#include "TextService.h"
#include "CandidateList.h"
#include <cwctype>
//
// GUID for the preserved keys.
//
/* 6a0bde41-6adf-11d7-a6ea-00065b84435c */
static const GUID GUID_PRESERVEDKEY_ONOFF = {
0x6a0bde41,
0x6adf,
0x11d7,
{0xa6, 0xea, 0x00, 0x06, 0x5b, 0x84, 0x43, 0x5c}
};
/* 6a0bde42-6adf-11d7-a6ea-00065b84435c */
static const GUID GUID_PRESERVEDKEY_F6 = {
0x6a0bde42,
0x6adf,
0x11d7,
{0xa6, 0xea, 0x00, 0x06, 0x5b, 0x84, 0x43, 0x5c}
};
//
// the preserved keys declaration
//
// VK_KANJI is the virtual key for Kanji key, which is available in 106
// Japanese keyboard.
//
static const TF_PRESERVEDKEY c_pkeyOnOff0 = { 0xC0, TF_MOD_ALT };
static const TF_PRESERVEDKEY c_pkeyOnOff1 = { VK_KANJI, TF_MOD_IGNORE_ALL_MODIFIER };
static const TF_PRESERVEDKEY c_pkeyF6 = { VK_F6, TF_MOD_ON_KEYUP };
//
// the description for the preserved keys
//
static const WCHAR c_szPKeyOnOff[] = L"OnOff";
static const WCHAR c_szPKeyF6[] = L"Function 6";
namespace
{
bool CanTranslateToPrintableChar(WPARAM wParam, LPARAM lParam)
{
if ((wParam >= 'A' && wParam <= 'Z') || (wParam >= '0' && wParam <= '9'))
{
return true;
}
BYTE keyboardState[256] = {};
if (!GetKeyboardState(keyboardState))
{
return false;
}
WCHAR translated[4] = {};
const UINT scanCode = (static_cast<UINT>(lParam) >> 16) & 0xFF;
const HKL keyboardLayout = GetKeyboardLayout(0);
const int translatedCount = ToUnicodeEx(
static_cast<UINT>(wParam),
scanCode,
keyboardState,
translated,
ARRAYSIZE(translated),
0,
keyboardLayout);
if (translatedCount > 0 && iswprint(translated[0]))
{
return true;
}
if (translatedCount < 0)
{
BYTE emptyState[256] = {};
ToUnicodeEx(
static_cast<UINT>(wParam),
scanCode,
emptyState,
translated,
ARRAYSIZE(translated),
0,
keyboardLayout);
}
return false;
}
}
//+---------------------------------------------------------------------------
//
// _IsKeyEaten
//
//----------------------------------------------------------------------------
BOOL CTextService::_IsKeyEaten(ITfContext* pContext, WPARAM wParam, LPARAM lParam)
{
// 1. キーボードが無効なら何も食べない
if (_IsKeyboardDisabled())
{
if (wParam == VK_F12)
{
DebugLog(L"[_IsKeyEaten] wParam=VK_F12 result=FALSE reason=keyboard_disabled\r\n");
}
return FALSE;
}
//
// 2. 入力モード共通で扱いたいキー
// - F12: IME 内部の「あ/ENG」モード切替用
// → KeyHandler(CKeyHandlerEditSession)に処理させるため IME が食う
//
if (wParam == VK_F12)
{
DebugLog(L"[_IsKeyEaten] wParam=VK_F12 result=TRUE\r\n");
return TRUE;
}
//
// 3. IME OFF なら食べない
if (!_IsKeyboardOpen())
{
if (wParam == VK_F12)
{
DebugLog(L"[_IsKeyEaten] wParam=VK_F12 result=FALSE reason=keyboard_closed\r\n");
}
return FALSE;
}
//
// 4. 候補ウィンドウ表示中は、キー処理を CandidateList 側に任せる
// (元サンプルと同じ挙動)
//
if (_pCandidateList &&
_pCandidateList->_IsContextCandidateWindow(pContext))
{
return FALSE;
}
//
// 5. ENG モード (半角英数モード) のとき:
// - 通常のキー入力はアプリケーションにそのまま流す
// - つまりローマ字かな変換は走らない
//
if (GetEffectiveInputMode() == InputMode::DirectInput)
{
// 上で F12 だけは TRUE で返しているので、ここでは全て FALSE
return FALSE;
}
//
// 6. ここからは「ひらがなモード」のキー判定
// - 元のサンプルのロジックをベースに、ローマ字入力用として A〜Z, 0〜9 を食う
//
switch (wParam)
{
case VK_SHIFT:
if (GetEffectiveInputMode() == InputMode::Hiragana)
return TRUE;
return FALSE;
case VK_LEFT:
case VK_RIGHT:
case VK_RETURN:
case VK_SPACE:
case VK_BACK:
case VK_DELETE:
case VK_ESCAPE:
// composition 中だけ IME 側で処理
if (GetCompositionPhase() != CompositionPhase::Idle)
return TRUE;
return FALSE;
}
// A〜Z はローマ字かな入力として IME が食う
if (CanTranslateToPrintableChar(wParam, lParam))
return TRUE;
return FALSE;
}
//+---------------------------------------------------------------------------
//
// OnSetFocus
//
// Called by the system whenever this service gets the keystroke device focus.
//----------------------------------------------------------------------------
STDAPI CTextService::OnSetFocus(BOOL fForeground)
{
UNREFERENCED_PARAMETER(fForeground);
_ReloadSettings();
return S_OK;
}
//+---------------------------------------------------------------------------
//
// OnTestKeyDown
//
// Called by the system to query this service wants a potential keystroke.
//----------------------------------------------------------------------------
STDAPI CTextService::OnTestKeyDown(ITfContext* pContext, WPARAM wParam, LPARAM lParam, BOOL* pfEaten)
{
UNREFERENCED_PARAMETER(lParam);
if (pfEaten == NULL)
return E_INVALIDARG;
*pfEaten = _IsKeyEaten(pContext, wParam, lParam);
if (wParam == VK_F12)
{
DebugLog(L"[OnTestKeyDown] wParam=VK_F12 pfEaten=%s\r\n", *pfEaten ? L"TRUE" : L"FALSE");
}
return S_OK;
}
//+---------------------------------------------------------------------------
//
// OnKeyDown
//
// Called by the system to offer this service a keystroke. If *pfEaten == TRUE
// on exit, the application will not handle the keystroke.
//----------------------------------------------------------------------------
STDAPI CTextService::OnKeyDown(ITfContext* pContext, WPARAM wParam, LPARAM lParam, BOOL* pfEaten)
{
UNREFERENCED_PARAMETER(lParam);
if (pfEaten == NULL)
return E_INVALIDARG;
*pfEaten = _IsKeyEaten(pContext, wParam, lParam);
if (wParam == VK_F12)
{
DebugLog(L"[OnKeyDown] wParam=VK_F12 pfEaten=%s\r\n", *pfEaten ? L"TRUE" : L"FALSE");
}
if (*pfEaten)
{
// IME 側で処理するキーは EditSession で処理
_InvokeKeyHandler(pContext, wParam, lParam);
}
return S_OK;
}
//+---------------------------------------------------------------------------
//
// OnTestKeyUp
//
// Called by the system to query this service wants a potential keystroke.
//----------------------------------------------------------------------------
STDAPI CTextService::OnTestKeyUp(ITfContext* pContext, WPARAM wParam, LPARAM lParam, BOOL* pfEaten)
{
UNREFERENCED_PARAMETER(pContext);
UNREFERENCED_PARAMETER(wParam);
UNREFERENCED_PARAMETER(lParam);
if (pfEaten == NULL)
return E_INVALIDARG;
// KeyUp は特に処理しないので常に FALSE
*pfEaten = FALSE;
return S_OK;
}
//+---------------------------------------------------------------------------
//
// OnKeyUp
//
// Called by the system to offer this service a keystroke. If *pfEaten == TRUE
// on exit, the application will not handle the keystroke.
//----------------------------------------------------------------------------
STDAPI CTextService::OnKeyUp(ITfContext* pContext, WPARAM wParam, LPARAM lParam, BOOL* pfEaten)
{
UNREFERENCED_PARAMETER(pContext);
UNREFERENCED_PARAMETER(wParam);
UNREFERENCED_PARAMETER(lParam);
if (pfEaten == NULL)
return E_INVALIDARG;
*pfEaten = FALSE;
return S_OK;
}
//+---------------------------------------------------------------------------
//
// OnPreservedKey
//
// Called when a hotkey (registered by us, or by the system) is typed.
//----------------------------------------------------------------------------
STDAPI CTextService::OnPreservedKey(ITfContext* pContext, REFGUID rguid, BOOL* pfEaten)
{
if (pfEaten == NULL)
return E_INVALIDARG;
DebugLog(L"[KeyEventSink] OnPreservedKey entered\r\n");
DebugLogGuid(L"[KeyEventSink] OnPreservedKey rguid", rguid);
if (IsEqualGUID(rguid, GUID_PRESERVEDKEY_ONOFF))
{
BOOL fOpen = _IsKeyboardOpen();
_SetKeyboardOpen(fOpen ? FALSE : TRUE);
*pfEaten = TRUE;
}
else
{
// GUID_PRESERVEDKEY_F6 は今のところ何もしない(サンプルのまま)
*pfEaten = FALSE;
}
UNREFERENCED_PARAMETER(pContext);
_UpdateLanguageBar();
return S_OK;
}
//+---------------------------------------------------------------------------
//
// _InitKeyEventSink
//
// Advise a keystroke sink.
//----------------------------------------------------------------------------
BOOL CTextService::_InitKeyEventSink()
{
ITfKeystrokeMgr* pKeystrokeMgr;
HRESULT hr = E_FAIL;
hr = _pThreadMgr->QueryInterface(IID_ITfKeystrokeMgr, (void**)&pKeystrokeMgr);
DebugLogHr(L"[KeyEventSink] QueryInterface(ITfKeystrokeMgr)", hr);
if (hr != S_OK)
{
DebugLogBool(L"[KeyEventSink] _InitKeyEventSink", FALSE);
return FALSE;
}
hr = pKeystrokeMgr->AdviseKeyEventSink(_tfClientId, (ITfKeyEventSink*)this, TRUE);
DebugLogHr(L"[KeyEventSink] AdviseKeyEventSink", hr);
pKeystrokeMgr->Release();
DebugLogBool(L"[KeyEventSink] _InitKeyEventSink", (hr == S_OK));
return (hr == S_OK);
}
//+---------------------------------------------------------------------------
//
// _UninitKeyEventSink
//
// Unadvise a keystroke sink. Assumes we have advised one already.
//----------------------------------------------------------------------------
void CTextService::_UninitKeyEventSink()
{
ITfKeystrokeMgr* pKeystrokeMgr;
if (_pThreadMgr->QueryInterface(IID_ITfKeystrokeMgr, (void**)&pKeystrokeMgr) != S_OK)
return;
pKeystrokeMgr->UnadviseKeyEventSink(_tfClientId);
pKeystrokeMgr->Release();
}
//+---------------------------------------------------------------------------
//
// _InitPreservedKey
//
// Register a hot key.
//----------------------------------------------------------------------------
BOOL CTextService::_InitPreservedKey()
{
ITfKeystrokeMgr* pKeystrokeMgr;
HRESULT hr = E_FAIL;
hr = _pThreadMgr->QueryInterface(IID_ITfKeystrokeMgr, (void**)&pKeystrokeMgr);
DebugLogHr(L"[KeyEventSink] QueryInterface(ITfKeystrokeMgr) for preserved", hr);
if (hr != S_OK)
{
DebugLogBool(L"[KeyEventSink] _InitPreservedKey", FALSE);
return FALSE;
}
// register Alt+~ key
hr = pKeystrokeMgr->PreserveKey(_tfClientId,
GUID_PRESERVEDKEY_ONOFF,
&c_pkeyOnOff0,
c_szPKeyOnOff,
lstrlen(c_szPKeyOnOff));
DebugLogHr(L"[KeyEventSink] PreserveKey Alt+~", hr);
// register KANJI key
hr = pKeystrokeMgr->PreserveKey(_tfClientId,
GUID_PRESERVEDKEY_ONOFF,
&c_pkeyOnOff1,
c_szPKeyOnOff,
lstrlen(c_szPKeyOnOff));
DebugLogHr(L"[KeyEventSink] PreserveKey KANJI", hr);
// register F6 key (サンプルのまま登録しておく。必要なら後で利用可能)
hr = pKeystrokeMgr->PreserveKey(_tfClientId,
GUID_PRESERVEDKEY_F6,
&c_pkeyF6,
c_szPKeyF6,
lstrlen(c_szPKeyF6));
DebugLogHr(L"[KeyEventSink] PreserveKey F6", hr);
pKeystrokeMgr->Release();
DebugLogBool(L"[KeyEventSink] _InitPreservedKey", (hr == S_OK));
return (hr == S_OK);
}
//+---------------------------------------------------------------------------
//
// _UninitPreservedKey
//
// Uninit a hot key.
//----------------------------------------------------------------------------
void CTextService::_UninitPreservedKey()
{
ITfKeystrokeMgr* pKeystrokeMgr;
if (_pThreadMgr->QueryInterface(IID_ITfKeystrokeMgr, (void**)&pKeystrokeMgr) != S_OK)
return;
pKeystrokeMgr->UnpreserveKey(GUID_PRESERVEDKEY_ONOFF, &c_pkeyOnOff0);
pKeystrokeMgr->UnpreserveKey(GUID_PRESERVEDKEY_ONOFF, &c_pkeyOnOff1);
pKeystrokeMgr->UnpreserveKey(GUID_PRESERVEDKEY_F6, &c_pkeyF6);
pKeystrokeMgr->Release();
}