-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathKeyHandler.cpp
More file actions
600 lines (503 loc) · 16 KB
/
KeyHandler.cpp
File metadata and controls
600 lines (503 loc) · 16 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
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
//////////////////////////////////////////////////////////////////////
//
// 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.
//
// KeyHandler.cpp
//
// the handler routines for key events
//
//////////////////////////////////////////////////////////////////////
#include "Globals.h"
#include "EditSession.h"
#include "TextService.h"
#include "CandidateList.h"
#include <cwctype>
//+---------------------------------------------------------------------------
//
// CKeyHandlerEditSession
//
//----------------------------------------------------------------------------
class CKeyHandlerEditSession : public CEditSessionBase
{
public:
CKeyHandlerEditSession(CTextService* pTextService, ITfContext* pContext, WPARAM wParam, LPARAM lParam) : CEditSessionBase(pTextService, pContext)
{
_wParam = wParam;
_lParam = lParam;
}
// ITfEditSession
STDMETHODIMP DoEditSession(TfEditCookie ec);
private:
WPARAM _wParam;
LPARAM _lParam;
};
//+---------------------------------------------------------------------------
//
// DoEditSession
//
//----------------------------------------------------------------------------
STDAPI CKeyHandlerEditSession::DoEditSession(TfEditCookie ec)
{
switch (_wParam)
{
case VK_LEFT:
case VK_RIGHT:
return _pTextService->_HandleArrowKey(ec, _pContext, _wParam);
case VK_RETURN:
return _pTextService->_HandleReturnKey(ec, _pContext);
case VK_SHIFT:
return _pTextService->_HandleShiftKey(ec, _pContext);
case VK_SPACE:
return _pTextService->_HandleSpaceKey(ec, _pContext);
case VK_BACK:
return _pTextService->_HandleBackspaceKey(ec, _pContext);
case VK_DELETE:
return _pTextService->_HandleDeleteKey(ec, _pContext);
case VK_ESCAPE:
return _pTextService->_HandleEscapeKey(ec, _pContext);
case VK_F12:
DebugLog(L"[KeyHandler] F12 path entered\r\n");
return _pTextService->_HandleModeToggleKey(ec, _pContext);
default:
if ((_wParam >= 'A' && _wParam <= 'Z') ||
(_wParam >= '0' && _wParam <= '9'))
return _pTextService->_HandleCharacterKey(ec, _pContext, _wParam, _lParam);
return _pTextService->_HandleCharacterKey(ec, _pContext, _wParam, _lParam);
break;
}
return S_OK;
}
static WCHAR NormalizeRawInputChar(WCHAR ch, InputMode mode)
{
switch (mode)
{
case InputMode::Hiragana:
case InputMode::HalfwidthKatakana:
case InputMode::FullwidthKatakana:
if (ch >= L'A' && ch <= L'Z')
{
return static_cast<WCHAR>(ch - L'A' + L'a');
}
return ch;
case InputMode::DirectInput:
case InputMode::FullwidthAlphanumeric:
default:
return ch;
}
}
static bool TryTranslateVirtualKeyToChar(WPARAM wParam, LPARAM lParam, WCHAR* ch)
{
if (ch == nullptr)
{
return false;
}
if ((wParam >= 'A' && wParam <= 'Z') || (wParam >= '0' && wParam <= '9'))
{
*ch = static_cast<WCHAR>(wParam);
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]))
{
*ch = translated[0];
return true;
}
if (translatedCount < 0)
{
BYTE emptyState[256] = {};
ToUnicodeEx(
static_cast<UINT>(wParam),
scanCode,
emptyState,
translated,
ARRAYSIZE(translated),
0,
keyboardLayout);
}
return false;
}
static bool IsShiftKeyDown()
{
return (GetKeyState(VK_SHIFT) & 0x8000) != 0;
}
//+---------------------------------------------------------------------------
//
// IsRangeCovered
//
// Returns TRUE if pRangeTest is entirely contained within pRangeCover.
//
//----------------------------------------------------------------------------
BOOL IsRangeCovered(TfEditCookie ec, ITfRange* pRangeTest, ITfRange* pRangeCover)
{
LONG lResult;
if (pRangeCover->CompareStart(ec, pRangeTest, TF_ANCHOR_START, &lResult) != S_OK ||
lResult > 0)
{
return FALSE;
}
if (pRangeCover->CompareEnd(ec, pRangeTest, TF_ANCHOR_END, &lResult) != S_OK ||
lResult < 0)
{
return FALSE;
}
return TRUE;
}
//+---------------------------------------------------------------------------
//
// _HandleCharacterKey
//
// If the keystroke happens within a composition, eat the key and return S_OK.
//
//----------------------------------------------------------------------------
HRESULT CTextService::_HandleCharacterKey(TfEditCookie ec, ITfContext* pContext, WPARAM wParam, LPARAM lParam)
{
if (!_IsComposing())
{
_StartComposition(pContext);
}
if (!_IsComposing())
{
return E_FAIL;
}
InputMode mode = _GetCompositionInputMode();
WCHAR rawChar = 0;
if (!TryTranslateVirtualKeyToChar(wParam, lParam, &rawChar))
{
_pendingAlphabeticShift = FALSE;
return S_FALSE;
}
const bool alphabeticChar = (rawChar >= L'A' && rawChar <= L'Z') || (rawChar >= L'a' && rawChar <= L'z');
if (_pendingAlphabeticShift &&
GetEffectiveInputMode() == InputMode::Hiragana &&
alphabeticChar)
{
_compositionState.SetAlphabeticPreeditActive(true);
mode = InputMode::DirectInput;
}
else if (_pendingAlphabeticShift)
{
_pendingAlphabeticShift = FALSE;
}
WCHAR ch = NormalizeRawInputChar(rawChar, mode);
if (_compositionState.IsAlphabeticPreeditActive())
{
if (alphabeticChar)
{
ch = _compositionState.GetRawInput().empty()
? static_cast<WCHAR>(towupper(rawChar))
: static_cast<WCHAR>(towlower(rawChar));
}
else
{
ch = rawChar;
}
}
_compositionState.Begin();
_compositionState.InsertRawChar(ch, mode, _romajiConverter);
_pendingAlphabeticShift = FALSE;
_compositionPhase = _compositionState.GetPhase();
return _UpdateCompositionText(ec, pContext);
}
HRESULT CTextService::_HandleShiftKey(TfEditCookie ec, ITfContext* pContext)
{
UNREFERENCED_PARAMETER(ec);
UNREFERENCED_PARAMETER(pContext);
if (GetEffectiveInputMode() == InputMode::Hiragana &&
_compositionState.GetPhase() != CompositionPhase::Converting &&
_compositionState.GetPhase() != CompositionPhase::CandidateSelecting &&
_compositionState.GetPhase() != CompositionPhase::RechunkSelecting)
{
_pendingAlphabeticShift = TRUE;
return S_OK;
}
return S_FALSE;
}
//+---------------------------------------------------------------------------
//
// _HandleReturnKey
//
//----------------------------------------------------------------------------
HRESULT CTextService::_HandleReturnKey(TfEditCookie ec, ITfContext* pContext)
{
_pendingAlphabeticShift = FALSE;
CompositionPhase phase = _compositionState.GetPhase();
if (phase == CompositionPhase::Idle)
{
return S_OK;
}
if (phase == CompositionPhase::CandidateSelecting ||
phase == CompositionPhase::Converting ||
phase == CompositionPhase::RechunkSelecting)
{
return _CommitCurrentCandidate(ec, pContext);
}
_TerminateComposition(ec, pContext);
return S_OK;
}
//+---------------------------------------------------------------------------
//
// _HandleSpaceKey
//
//----------------------------------------------------------------------------
HRESULT CTextService::_HandleSpaceKey(TfEditCookie ec, ITfContext* pContext)
{
_pendingAlphabeticShift = FALSE;
const bool shiftDown = IsShiftKeyDown();
CompositionPhase phase = _compositionState.GetPhase();
if (phase == CompositionPhase::Idle)
{
return S_OK;
}
if (phase == CompositionPhase::CandidateSelecting)
{
if (!shiftDown)
{
return _SelectNextCandidate(ec, pContext);
}
if (!_compositionState.BeginRechunkSelection(_romajiConverter))
{
return S_OK;
}
_compositionPhase = _compositionState.GetPhase();
HRESULT hr = _UpdateCompositionText(ec, pContext);
if (FAILED(hr))
{
return hr;
}
return _ShowCandidateList(ec, pContext);
}
if (phase == CompositionPhase::RechunkSelecting)
{
return _SelectNextCandidate(ec, pContext);
}
if (phase == CompositionPhase::Converting)
{
if (!_compositionState.BeginSegmentSelection())
{
return S_OK;
}
if (shiftDown)
{
_compositionState.BeginRechunkSelection(_romajiConverter);
}
_compositionPhase = _compositionState.GetPhase();
HRESULT hr = _UpdateCompositionText(ec, pContext);
if (FAILED(hr))
{
return hr;
}
return _ShowCandidateList(ec, pContext);
}
std::wstring leftContext;
if (_pComposition != NULL)
{
ITfRange* pRangeComposition = NULL;
if (_pComposition->GetRange(&pRangeComposition) == S_OK && pRangeComposition != NULL)
{
leftContext = _GetLeftContextText(ec, pRangeComposition);
pRangeComposition->Release();
}
}
if (!_compositionState.StartConversion(_kanaKanjiConverter, _GetCompositionInputMode(), _romajiConverter, leftContext))
{
return S_OK;
}
if (_compositionState.IsAlphabeticPreeditActive() || _liveConversionEnabled != FALSE)
{
if (!_compositionState.BeginSegmentSelection())
{
_compositionPhase = _compositionState.GetPhase();
return _UpdateCompositionText(ec, pContext);
}
if (shiftDown)
{
_compositionState.BeginRechunkSelection(_romajiConverter);
}
_compositionPhase = _compositionState.GetPhase();
HRESULT hr = _UpdateCompositionText(ec, pContext);
if (FAILED(hr))
{
return hr;
}
return _ShowCandidateList(ec, pContext);
}
_compositionPhase = _compositionState.GetPhase();
return _UpdateCompositionText(ec, pContext);
}
//+---------------------------------------------------------------------------
//
// _HandleArrowKey
//
// Update the selection within a composition.
//
//----------------------------------------------------------------------------
HRESULT CTextService::_HandleArrowKey(TfEditCookie ec, ITfContext* pContext, WPARAM wParam)
{
_pendingAlphabeticShift = FALSE;
if (!_IsComposing() || _pComposition == NULL)
{
return S_FALSE;
}
CompositionPhase phase = _compositionState.GetPhase();
if (phase == CompositionPhase::CandidateSelecting ||
phase == CompositionPhase::RechunkSelecting)
{
bool moved = (wParam == VK_LEFT)
? _compositionState.MoveFocusLeft()
: _compositionState.MoveFocusRight();
_compositionPhase = _compositionState.GetPhase();
if (!moved)
{
return S_OK;
}
return _UpdateCompositionText(ec, pContext);
}
if (phase == CompositionPhase::Converting)
{
return S_OK;
}
InputMode mode = _GetCompositionInputMode();
if (wParam == VK_LEFT)
{
_compositionState.MoveLeft(mode, _romajiConverter);
}
else
{
_compositionState.MoveRight(mode, _romajiConverter);
}
return _UpdateCompositionText(ec, pContext);
}
//+---------------------------------------------------------------------------
//
// _InvokeKeyHandler
//
// This text service is interested in handling keystrokes to demonstrate the
// use the compositions. Some apps will cancel compositions if they receive
// keystrokes while a compositions is ongoing.
//
//----------------------------------------------------------------------------
HRESULT CTextService::_InvokeKeyHandler(ITfContext* pContext, WPARAM wParam, LPARAM lParam)
{
CKeyHandlerEditSession* pEditSession;
HRESULT hr = E_FAIL;
// we'll insert a char ourselves in place of this keystroke
if ((pEditSession = new CKeyHandlerEditSession(this, pContext, wParam, lParam)) == NULL)
goto Exit;
// we need a lock to do our work
// nb: this method is one of the few places where it is legal to use
// the TF_ES_SYNC flag
hr = pContext->RequestEditSession(_tfClientId, pEditSession, TF_ES_SYNC | TF_ES_READWRITE, &hr);
pEditSession->Release();
Exit:
return hr;
}
HRESULT CTextService::_HandleBackspaceKey(TfEditCookie ec, ITfContext* pContext)
{
_pendingAlphabeticShift = FALSE;
if (!_IsComposing() || _pComposition == nullptr)
{
return S_FALSE;
}
if (_compositionState.GetPhase() == CompositionPhase::Converting ||
_compositionState.GetPhase() == CompositionPhase::CandidateSelecting ||
_compositionState.GetPhase() == CompositionPhase::RechunkSelecting)
{
return _CancelConversion(ec, pContext);
}
InputMode mode = _GetCompositionInputMode();
if (!_compositionState.Backspace(mode, _romajiConverter))
{
return S_OK;
}
if (_compositionState.Empty())
{
_ClearCompositionText(ec, pContext);
_TerminateComposition(ec, pContext);
return S_OK;
}
_compositionPhase = _compositionState.GetPhase();
return _UpdateCompositionText(ec, pContext);
}
HRESULT CTextService::_HandleDeleteKey(TfEditCookie ec, ITfContext* pContext)
{
_pendingAlphabeticShift = FALSE;
if (!_IsComposing() || _pComposition == nullptr)
{
return S_FALSE;
}
if (_compositionState.GetPhase() == CompositionPhase::Converting ||
_compositionState.GetPhase() == CompositionPhase::CandidateSelecting ||
_compositionState.GetPhase() == CompositionPhase::RechunkSelecting)
{
return _CancelConversion(ec, pContext);
}
InputMode mode = _GetCompositionInputMode();
if (!_compositionState.Delete(mode, _romajiConverter))
{
return S_OK;
}
if (_compositionState.Empty())
{
_ClearCompositionText(ec, pContext);
_TerminateComposition(ec, pContext);
return S_OK;
}
_compositionPhase = _compositionState.GetPhase();
return _UpdateCompositionText(ec, pContext);
}
HRESULT CTextService::_HandleEscapeKey(TfEditCookie ec, ITfContext* pContext)
{
_pendingAlphabeticShift = FALSE;
if (_compositionState.GetPhase() == CompositionPhase::RechunkSelecting)
{
if (!_compositionState.CancelRechunkSelection())
{
return S_OK;
}
_compositionPhase = _compositionState.GetPhase();
return _UpdateCompositionText(ec, pContext);
}
if (_compositionState.GetPhase() != CompositionPhase::Converting &&
_compositionState.GetPhase() != CompositionPhase::CandidateSelecting)
{
return S_OK;
}
HRESULT hr = _CancelConversion(ec, pContext);
_CloseCandidateList();
return hr;
}
HRESULT CTextService::_HandleModeToggleKey(TfEditCookie ec, ITfContext* pContext)
{
UNREFERENCED_PARAMETER(ec);
UNREFERENCED_PARAMETER(pContext);
DebugLog(L"[KeyHandler] F12 toggle executed before user=%d effective=%d\r\n",
static_cast<int>(GetUserInputMode()),
static_cast<int>(GetEffectiveInputMode()));
ToggleInputMode();
_SetKeyboardOpen(TRUE);
_UpdateLanguageBar();
DebugLog(L"[KeyHandler] F12 toggle executed after user=%d effective=%d\r\n",
static_cast<int>(GetUserInputMode()),
static_cast<int>(GetEffectiveInputMode()));
return S_OK;
}