forked from Floorp-Projects/Floorp
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathKeyboardLayout.cpp
2650 lines (2349 loc) · 85.6 KB
/
KeyboardLayout.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
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
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
/* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
#include "mozilla/DebugOnly.h"
#include "mozilla/MouseEvents.h"
#include "mozilla/TextEvents.h"
#include "mozilla/Util.h"
#include "KeyboardLayout.h"
#include "nsIMM32Handler.h"
#include "nsMemory.h"
#include "nsToolkit.h"
#include "nsQuickSort.h"
#include "nsAlgorithm.h"
#include "nsUnicharUtils.h"
#include "WidgetUtils.h"
#include "WinUtils.h"
#include "nsWindowDbg.h"
#include "nsServiceManagerUtils.h"
#include "nsPrintfCString.h"
#include "nsIDOMKeyEvent.h"
#include "nsIIdleServiceInternal.h"
#ifdef MOZ_CRASHREPORTER
#include "nsExceptionHandler.h"
#endif
#include "npapi.h"
#include <windows.h>
#include <winuser.h>
#include <algorithm>
#ifndef WINABLEAPI
#include <winable.h>
#endif
namespace mozilla {
namespace widget {
// Unique id counter associated with a keydown / keypress events. Used in
// identifing keypress events for removal from async event dispatch queue
// in metrofx after preventDefault is called on keydown events.
static uint32_t sUniqueKeyEventId = 0;
struct DeadKeyEntry
{
PRUnichar BaseChar;
PRUnichar CompositeChar;
};
class DeadKeyTable
{
friend class KeyboardLayout;
uint16_t mEntries;
// KeyboardLayout::AddDeadKeyTable() will allocate as many entries as
// required. It is the only way to create new DeadKeyTable instances.
DeadKeyEntry mTable[1];
void Init(const DeadKeyEntry* aDeadKeyArray, uint32_t aEntries)
{
mEntries = aEntries;
memcpy(mTable, aDeadKeyArray, aEntries * sizeof(DeadKeyEntry));
}
static uint32_t SizeInBytes(uint32_t aEntries)
{
return offsetof(DeadKeyTable, mTable) + aEntries * sizeof(DeadKeyEntry);
}
public:
uint32_t Entries() const
{
return mEntries;
}
bool IsEqual(const DeadKeyEntry* aDeadKeyArray, uint32_t aEntries) const
{
return (mEntries == aEntries &&
!memcmp(mTable, aDeadKeyArray,
aEntries * sizeof(DeadKeyEntry)));
}
PRUnichar GetCompositeChar(PRUnichar aBaseChar) const;
};
/*****************************************************************************
* mozilla::widget::ModifierKeyState
*****************************************************************************/
ModifierKeyState::ModifierKeyState()
{
Update();
}
ModifierKeyState::ModifierKeyState(bool aIsShiftDown,
bool aIsControlDown,
bool aIsAltDown)
{
Update();
Unset(MODIFIER_SHIFT | MODIFIER_CONTROL | MODIFIER_ALT | MODIFIER_ALTGRAPH);
Modifiers modifiers = 0;
if (aIsShiftDown) {
modifiers |= MODIFIER_SHIFT;
}
if (aIsControlDown) {
modifiers |= MODIFIER_CONTROL;
}
if (aIsAltDown) {
modifiers |= MODIFIER_ALT;
}
if (modifiers) {
Set(modifiers);
}
}
ModifierKeyState::ModifierKeyState(Modifiers aModifiers) :
mModifiers(aModifiers)
{
EnsureAltGr();
}
void
ModifierKeyState::Update()
{
mModifiers = 0;
if (IS_VK_DOWN(VK_SHIFT)) {
mModifiers |= MODIFIER_SHIFT;
}
if (IS_VK_DOWN(VK_CONTROL)) {
mModifiers |= MODIFIER_CONTROL;
}
if (IS_VK_DOWN(VK_MENU)) {
mModifiers |= MODIFIER_ALT;
}
if (IS_VK_DOWN(VK_LWIN) || IS_VK_DOWN(VK_RWIN)) {
mModifiers |= MODIFIER_OS;
}
if (::GetKeyState(VK_CAPITAL) & 1) {
mModifiers |= MODIFIER_CAPSLOCK;
}
if (::GetKeyState(VK_NUMLOCK) & 1) {
mModifiers |= MODIFIER_NUMLOCK;
}
if (::GetKeyState(VK_SCROLL) & 1) {
mModifiers |= MODIFIER_SCROLLLOCK;
}
EnsureAltGr();
}
void
ModifierKeyState::Unset(Modifiers aRemovingModifiers)
{
mModifiers &= ~aRemovingModifiers;
// Note that we don't need to unset AltGr flag here automatically.
// For nsEditor, we need to remove Alt and Control flags but AltGr isn't
// checked in nsEditor, so, it can be kept.
}
void
ModifierKeyState::Set(Modifiers aAddingModifiers)
{
mModifiers |= aAddingModifiers;
EnsureAltGr();
}
void
ModifierKeyState::InitInputEvent(WidgetInputEvent& aInputEvent) const
{
aInputEvent.modifiers = mModifiers;
switch(aInputEvent.eventStructType) {
case NS_MOUSE_EVENT:
case NS_MOUSE_SCROLL_EVENT:
case NS_WHEEL_EVENT:
case NS_DRAG_EVENT:
case NS_SIMPLE_GESTURE_EVENT:
InitMouseEvent(aInputEvent);
break;
default:
break;
}
}
void
ModifierKeyState::InitMouseEvent(WidgetInputEvent& aMouseEvent) const
{
NS_ASSERTION(aMouseEvent.eventStructType == NS_MOUSE_EVENT ||
aMouseEvent.eventStructType == NS_WHEEL_EVENT ||
aMouseEvent.eventStructType == NS_DRAG_EVENT ||
aMouseEvent.eventStructType == NS_SIMPLE_GESTURE_EVENT,
"called with non-mouse event");
WidgetMouseEventBase& mouseEvent = *aMouseEvent.AsMouseEventBase();
mouseEvent.buttons = 0;
if (::GetKeyState(VK_LBUTTON) < 0) {
mouseEvent.buttons |= WidgetMouseEvent::eLeftButtonFlag;
}
if (::GetKeyState(VK_RBUTTON) < 0) {
mouseEvent.buttons |= WidgetMouseEvent::eRightButtonFlag;
}
if (::GetKeyState(VK_MBUTTON) < 0) {
mouseEvent.buttons |= WidgetMouseEvent::eMiddleButtonFlag;
}
if (::GetKeyState(VK_XBUTTON1) < 0) {
mouseEvent.buttons |= WidgetMouseEvent::e4thButtonFlag;
}
if (::GetKeyState(VK_XBUTTON2) < 0) {
mouseEvent.buttons |= WidgetMouseEvent::e5thButtonFlag;
}
}
bool
ModifierKeyState::IsShift() const
{
return (mModifiers & MODIFIER_SHIFT) != 0;
}
bool
ModifierKeyState::IsControl() const
{
return (mModifiers & MODIFIER_CONTROL) != 0;
}
bool
ModifierKeyState::IsAlt() const
{
return (mModifiers & MODIFIER_ALT) != 0;
}
bool
ModifierKeyState::IsAltGr() const
{
return IsControl() && IsAlt();
}
bool
ModifierKeyState::IsWin() const
{
return (mModifiers & MODIFIER_OS) != 0;
}
bool
ModifierKeyState::IsCapsLocked() const
{
return (mModifiers & MODIFIER_CAPSLOCK) != 0;
}
bool
ModifierKeyState::IsNumLocked() const
{
return (mModifiers & MODIFIER_NUMLOCK) != 0;
}
bool
ModifierKeyState::IsScrollLocked() const
{
return (mModifiers & MODIFIER_SCROLLLOCK) != 0;
}
Modifiers
ModifierKeyState::GetModifiers() const
{
return mModifiers;
}
void
ModifierKeyState::EnsureAltGr()
{
// If both Control key and Alt key are pressed, it means AltGr is pressed.
// Ideally, we should check whether the current keyboard layout has AltGr
// or not. However, setting AltGr flags for keyboard which doesn't have
// AltGr must not be serious bug. So, it should be OK for now.
if (IsAltGr()) {
mModifiers |= MODIFIER_ALTGRAPH;
}
}
/*****************************************************************************
* mozilla::widget::UniCharsAndModifiers
*****************************************************************************/
void
UniCharsAndModifiers::Append(PRUnichar aUniChar, Modifiers aModifiers)
{
MOZ_ASSERT(mLength < 5);
mChars[mLength] = aUniChar;
mModifiers[mLength] = aModifiers;
mLength++;
}
void
UniCharsAndModifiers::FillModifiers(Modifiers aModifiers)
{
for (uint32_t i = 0; i < mLength; i++) {
mModifiers[i] = aModifiers;
}
}
bool
UniCharsAndModifiers::UniCharsEqual(const UniCharsAndModifiers& aOther) const
{
if (mLength != aOther.mLength) {
return false;
}
return !memcmp(mChars, aOther.mChars, mLength * sizeof(PRUnichar));
}
bool
UniCharsAndModifiers::UniCharsCaseInsensitiveEqual(
const UniCharsAndModifiers& aOther) const
{
if (mLength != aOther.mLength) {
return false;
}
nsCaseInsensitiveStringComparator comp;
return !comp(mChars, aOther.mChars, mLength, aOther.mLength);
}
UniCharsAndModifiers&
UniCharsAndModifiers::operator+=(const UniCharsAndModifiers& aOther)
{
uint32_t copyCount = std::min(aOther.mLength, 5 - mLength);
NS_ENSURE_TRUE(copyCount > 0, *this);
memcpy(&mChars[mLength], aOther.mChars, copyCount * sizeof(PRUnichar));
memcpy(&mModifiers[mLength], aOther.mModifiers,
copyCount * sizeof(Modifiers));
mLength += copyCount;
return *this;
}
UniCharsAndModifiers
UniCharsAndModifiers::operator+(const UniCharsAndModifiers& aOther) const
{
UniCharsAndModifiers result(*this);
result += aOther;
return result;
}
/*****************************************************************************
* mozilla::widget::VirtualKey
*****************************************************************************/
// static
VirtualKey::ShiftState
VirtualKey::ModifiersToShiftState(Modifiers aModifiers)
{
ShiftState state = 0;
if (aModifiers & MODIFIER_SHIFT) {
state |= STATE_SHIFT;
}
if (aModifiers & MODIFIER_CONTROL) {
state |= STATE_CONTROL;
}
if (aModifiers & MODIFIER_ALT) {
state |= STATE_ALT;
}
if (aModifiers & MODIFIER_CAPSLOCK) {
state |= STATE_CAPSLOCK;
}
return state;
}
// static
Modifiers
VirtualKey::ShiftStateToModifiers(ShiftState aShiftState)
{
Modifiers modifiers = 0;
if (aShiftState & STATE_SHIFT) {
modifiers |= MODIFIER_SHIFT;
}
if (aShiftState & STATE_CONTROL) {
modifiers |= MODIFIER_CONTROL;
}
if (aShiftState & STATE_ALT) {
modifiers |= MODIFIER_ALT;
}
if (aShiftState & STATE_CAPSLOCK) {
modifiers |= MODIFIER_CAPSLOCK;
}
if ((modifiers & (MODIFIER_ALT | MODIFIER_CONTROL)) ==
(MODIFIER_ALT | MODIFIER_CONTROL)) {
modifiers |= MODIFIER_ALTGRAPH;
}
return modifiers;
}
inline PRUnichar
VirtualKey::GetCompositeChar(ShiftState aShiftState, PRUnichar aBaseChar) const
{
return mShiftStates[aShiftState].DeadKey.Table->GetCompositeChar(aBaseChar);
}
const DeadKeyTable*
VirtualKey::MatchingDeadKeyTable(const DeadKeyEntry* aDeadKeyArray,
uint32_t aEntries) const
{
if (!mIsDeadKey) {
return nullptr;
}
for (ShiftState shiftState = 0; shiftState < 16; shiftState++) {
if (!IsDeadKey(shiftState)) {
continue;
}
const DeadKeyTable* dkt = mShiftStates[shiftState].DeadKey.Table;
if (dkt && dkt->IsEqual(aDeadKeyArray, aEntries)) {
return dkt;
}
}
return nullptr;
}
void
VirtualKey::SetNormalChars(ShiftState aShiftState,
const PRUnichar* aChars,
uint32_t aNumOfChars)
{
NS_ASSERTION(aShiftState < ArrayLength(mShiftStates), "invalid index");
SetDeadKey(aShiftState, false);
for (uint32_t index = 0; index < aNumOfChars; index++) {
// Ignore legacy non-printable control characters
mShiftStates[aShiftState].Normal.Chars[index] =
(aChars[index] >= 0x20) ? aChars[index] : 0;
}
uint32_t len = ArrayLength(mShiftStates[aShiftState].Normal.Chars);
for (uint32_t index = aNumOfChars; index < len; index++) {
mShiftStates[aShiftState].Normal.Chars[index] = 0;
}
}
void
VirtualKey::SetDeadChar(ShiftState aShiftState, PRUnichar aDeadChar)
{
NS_ASSERTION(aShiftState < ArrayLength(mShiftStates), "invalid index");
SetDeadKey(aShiftState, true);
mShiftStates[aShiftState].DeadKey.DeadChar = aDeadChar;
mShiftStates[aShiftState].DeadKey.Table = nullptr;
}
UniCharsAndModifiers
VirtualKey::GetUniChars(ShiftState aShiftState) const
{
UniCharsAndModifiers result = GetNativeUniChars(aShiftState);
const ShiftState STATE_ALT_CONTROL = (STATE_ALT | STATE_CONTROL);
if (!(aShiftState & STATE_ALT_CONTROL)) {
return result;
}
if (!result.mLength) {
result = GetNativeUniChars(aShiftState & ~STATE_ALT_CONTROL);
result.FillModifiers(ShiftStateToModifiers(aShiftState));
return result;
}
if ((aShiftState & STATE_ALT_CONTROL) == STATE_ALT_CONTROL) {
// Even if the shifted chars and the unshifted chars are same, we
// should consume the Alt key state and the Ctrl key state when
// AltGr key is pressed. Because if we don't consume them, the input
// events are ignored on nsEditor. (I.e., Users cannot input the
// characters with this key combination.)
Modifiers finalModifiers = ShiftStateToModifiers(aShiftState);
finalModifiers &= ~(MODIFIER_ALT | MODIFIER_CONTROL);
result.FillModifiers(finalModifiers);
return result;
}
UniCharsAndModifiers unmodifiedReslt =
GetNativeUniChars(aShiftState & ~STATE_ALT_CONTROL);
if (!result.UniCharsEqual(unmodifiedReslt)) {
// Otherwise, we should consume the Alt key state and the Ctrl key state
// only when the shifted chars and unshifted chars are different.
Modifiers finalModifiers = ShiftStateToModifiers(aShiftState);
finalModifiers &= ~(MODIFIER_ALT | MODIFIER_CONTROL);
result.FillModifiers(finalModifiers);
}
return result;
}
UniCharsAndModifiers
VirtualKey::GetNativeUniChars(ShiftState aShiftState) const
{
#ifdef DEBUG
if (aShiftState < 0 || aShiftState >= ArrayLength(mShiftStates)) {
nsPrintfCString warning("Shift state is out of range: "
"aShiftState=%d, ArrayLength(mShiftState)=%d",
aShiftState, ArrayLength(mShiftStates));
NS_WARNING(warning.get());
}
#endif
UniCharsAndModifiers result;
Modifiers modifiers = ShiftStateToModifiers(aShiftState);
if (IsDeadKey(aShiftState)) {
result.Append(mShiftStates[aShiftState].DeadKey.DeadChar, modifiers);
return result;
}
uint32_t index;
uint32_t len = ArrayLength(mShiftStates[aShiftState].Normal.Chars);
for (index = 0;
index < len && mShiftStates[aShiftState].Normal.Chars[index]; index++) {
result.Append(mShiftStates[aShiftState].Normal.Chars[index], modifiers);
}
return result;
}
// static
void
VirtualKey::FillKbdState(PBYTE aKbdState,
const ShiftState aShiftState)
{
NS_ASSERTION(aShiftState < 16, "aShiftState out of range");
if (aShiftState & STATE_SHIFT) {
aKbdState[VK_SHIFT] |= 0x80;
} else {
aKbdState[VK_SHIFT] &= ~0x80;
aKbdState[VK_LSHIFT] &= ~0x80;
aKbdState[VK_RSHIFT] &= ~0x80;
}
if (aShiftState & STATE_CONTROL) {
aKbdState[VK_CONTROL] |= 0x80;
} else {
aKbdState[VK_CONTROL] &= ~0x80;
aKbdState[VK_LCONTROL] &= ~0x80;
aKbdState[VK_RCONTROL] &= ~0x80;
}
if (aShiftState & STATE_ALT) {
aKbdState[VK_MENU] |= 0x80;
} else {
aKbdState[VK_MENU] &= ~0x80;
aKbdState[VK_LMENU] &= ~0x80;
aKbdState[VK_RMENU] &= ~0x80;
}
if (aShiftState & STATE_CAPSLOCK) {
aKbdState[VK_CAPITAL] |= 0x01;
} else {
aKbdState[VK_CAPITAL] &= ~0x01;
}
}
/*****************************************************************************
* mozilla::widget::NativeKey
*****************************************************************************/
NativeKey::NativeKey(nsWindowBase* aWidget,
const MSG& aKeyOrCharMessage,
const ModifierKeyState& aModKeyState,
nsTArray<FakeCharMsg>* aFakeCharMsgs) :
mWidget(aWidget), mMsg(aKeyOrCharMessage), mDOMKeyCode(0),
mModKeyState(aModKeyState), mVirtualKeyCode(0), mOriginalVirtualKeyCode(0),
mFakeCharMsgs(aFakeCharMsgs && aFakeCharMsgs->Length() ?
aFakeCharMsgs : nullptr)
{
MOZ_ASSERT(aWidget);
KeyboardLayout* keyboardLayout = KeyboardLayout::GetInstance();
mKeyboardLayout = keyboardLayout->GetLayout();
mScanCode = WinUtils::GetScanCode(mMsg.lParam);
mIsExtended = WinUtils::IsExtendedScanCode(mMsg.lParam);
// On WinXP and WinServer2003, we cannot compute the virtual keycode for
// extended keys due to the API limitation.
bool canComputeVirtualKeyCodeFromScanCode =
(!mIsExtended || WinUtils::GetWindowsVersion() >= WinUtils::VISTA_VERSION);
switch (mMsg.message) {
case WM_KEYDOWN:
case WM_SYSKEYDOWN:
case WM_KEYUP:
case WM_SYSKEYUP: {
// First, resolve the IME converted virtual keycode to its original
// keycode.
if (mMsg.wParam == VK_PROCESSKEY) {
mOriginalVirtualKeyCode =
static_cast<uint8_t>(::ImmGetVirtualKey(mMsg.hwnd));
} else {
mOriginalVirtualKeyCode = static_cast<uint8_t>(mMsg.wParam);
}
// Most keys are not distinguished as left or right keys.
bool isLeftRightDistinguishedKey = false;
// mOriginalVirtualKeyCode must not distinguish left or right of
// Shift, Control or Alt.
switch (mOriginalVirtualKeyCode) {
case VK_SHIFT:
case VK_CONTROL:
case VK_MENU:
isLeftRightDistinguishedKey = true;
break;
case VK_LSHIFT:
case VK_RSHIFT:
mVirtualKeyCode = mOriginalVirtualKeyCode;
mOriginalVirtualKeyCode = VK_SHIFT;
isLeftRightDistinguishedKey = true;
break;
case VK_LCONTROL:
case VK_RCONTROL:
mVirtualKeyCode = mOriginalVirtualKeyCode;
mOriginalVirtualKeyCode = VK_CONTROL;
isLeftRightDistinguishedKey = true;
break;
case VK_LMENU:
case VK_RMENU:
mVirtualKeyCode = mOriginalVirtualKeyCode;
mOriginalVirtualKeyCode = VK_MENU;
isLeftRightDistinguishedKey = true;
break;
}
// If virtual keycode (left-right distinguished keycode) is already
// computed, we don't need to do anymore.
if (mVirtualKeyCode) {
break;
}
// If the keycode doesn't have LR distinguished keycode, we just set
// mOriginalVirtualKeyCode to mVirtualKeyCode. Note that don't compute
// it from MapVirtualKeyEx() because the scan code might be wrong if
// the message is sent/posted by other application. Then, we will compute
// unexpected keycode from the scan code.
if (!isLeftRightDistinguishedKey) {
break;
}
if (!canComputeVirtualKeyCodeFromScanCode) {
// The right control key and the right alt key are extended keys.
// Therefore, we never get VK_RCONTRL and VK_RMENU for the result of
// MapVirtualKeyEx() on WinXP or WinServer2003.
//
// If VK_CONTROL or VK_MENU key message is caused by an extended key,
// we should assume that the right key of them is pressed.
switch (mOriginalVirtualKeyCode) {
case VK_CONTROL:
mVirtualKeyCode = VK_RCONTROL;
break;
case VK_MENU:
mVirtualKeyCode = VK_RMENU;
break;
case VK_SHIFT:
// Neither left shift nor right shift is not an extended key,
// let's use VK_LSHIFT for invalid scan code.
mVirtualKeyCode = VK_LSHIFT;
break;
default:
MOZ_CRASH("Unsupported mOriginalVirtualKeyCode");
}
break;
}
NS_ASSERTION(!mVirtualKeyCode,
"mVirtualKeyCode has been computed already");
// Otherwise, compute the virtual keycode with MapVirtualKeyEx().
mVirtualKeyCode = ComputeVirtualKeyCodeFromScanCodeEx();
// The result might be unexpected value due to the scan code is
// wrong. For example, any key messages can be generated by
// SendMessage() or PostMessage() from applications. So, it's possible
// failure. Then, let's respect the extended flag even if it might be
// set intentionally.
switch (mOriginalVirtualKeyCode) {
case VK_CONTROL:
if (mVirtualKeyCode != VK_LCONTROL &&
mVirtualKeyCode != VK_RCONTROL) {
mVirtualKeyCode = mIsExtended ? VK_RCONTROL : VK_LCONTROL;
}
break;
case VK_MENU:
if (mVirtualKeyCode != VK_LMENU && mVirtualKeyCode != VK_RMENU) {
mVirtualKeyCode = mIsExtended ? VK_RMENU : VK_LMENU;
}
break;
case VK_SHIFT:
if (mVirtualKeyCode != VK_LSHIFT && mVirtualKeyCode != VK_RSHIFT) {
// Neither left shift nor right shift is not an extended key,
// let's use VK_LSHIFT for invalid scan code.
mVirtualKeyCode = VK_LSHIFT;
}
break;
default:
MOZ_CRASH("Unsupported mOriginalVirtualKeyCode");
}
break;
}
case WM_CHAR:
case WM_UNICHAR:
case WM_SYSCHAR:
// We cannot compute the virtual key code from WM_CHAR message on WinXP
// if it's caused by an extended key.
if (!canComputeVirtualKeyCodeFromScanCode) {
break;
}
mVirtualKeyCode = mOriginalVirtualKeyCode =
ComputeVirtualKeyCodeFromScanCodeEx();
NS_ASSERTION(mVirtualKeyCode, "Failed to compute virtual keycode");
break;
default:
MOZ_CRASH("Unsupported message");
}
if (!mVirtualKeyCode) {
mVirtualKeyCode = mOriginalVirtualKeyCode;
}
mDOMKeyCode =
keyboardLayout->ConvertNativeKeyCodeToDOMKeyCode(mOriginalVirtualKeyCode);
mKeyNameIndex =
keyboardLayout->ConvertNativeKeyCodeToKeyNameIndex(mOriginalVirtualKeyCode);
keyboardLayout->InitNativeKey(*this, mModKeyState);
mIsDeadKey =
(IsFollowedByDeadCharMessage() ||
keyboardLayout->IsDeadKey(mOriginalVirtualKeyCode, mModKeyState));
mIsPrintableKey = KeyboardLayout::IsPrintableCharKey(mOriginalVirtualKeyCode);
}
bool
NativeKey::IsFollowedByCharMessage() const
{
MSG nextMsg;
if (mFakeCharMsgs) {
nextMsg = mFakeCharMsgs->ElementAt(0).GetCharMsg(mMsg.hwnd);
} else {
if (!WinUtils::PeekMessage(&nextMsg, mMsg.hwnd, WM_KEYFIRST, WM_KEYLAST,
PM_NOREMOVE | PM_NOYIELD)) {
return false;
}
}
return (nextMsg.message == WM_CHAR ||
nextMsg.message == WM_SYSCHAR ||
nextMsg.message == WM_DEADCHAR);
}
bool
NativeKey::IsFollowedByDeadCharMessage() const
{
MSG nextMsg;
if (mFakeCharMsgs) {
nextMsg = mFakeCharMsgs->ElementAt(0).GetCharMsg(mMsg.hwnd);
} else {
if (!WinUtils::PeekMessage(&nextMsg, mMsg.hwnd, WM_KEYFIRST, WM_KEYLAST,
PM_NOREMOVE | PM_NOYIELD)) {
return false;
}
}
return (nextMsg.message == WM_DEADCHAR);
}
bool
NativeKey::IsIMEDoingKakuteiUndo() const
{
// Following message pattern is caused by "Kakutei-Undo" of ATOK or WXG:
// ---------------------------------------------------------------------------
// WM_KEYDOWN * n (wParam = VK_BACK, lParam = 0x1)
// WM_KEYUP * 1 (wParam = VK_BACK, lParam = 0xC0000001) # ATOK
// WM_IME_STARTCOMPOSITION * 1 (wParam = 0x0, lParam = 0x0)
// WM_IME_COMPOSITION * 1 (wParam = 0x0, lParam = 0x1BF)
// WM_CHAR * n (wParam = VK_BACK, lParam = 0x1)
// WM_KEYUP * 1 (wParam = VK_BACK, lParam = 0xC00E0001)
// ---------------------------------------------------------------------------
// This doesn't match usual key message pattern such as:
// WM_KEYDOWN -> WM_CHAR -> WM_KEYDOWN -> WM_CHAR -> ... -> WM_KEYUP
// See following bugs for the detail.
// https://bugzilla.mozilla.gr.jp/show_bug.cgi?id=2885 (written in Japanese)
// https://bugzilla.mozilla.org/show_bug.cgi?id=194559 (written in English)
MSG startCompositionMsg, compositionMsg, charMsg;
return WinUtils::PeekMessage(&startCompositionMsg, mMsg.hwnd,
WM_IME_STARTCOMPOSITION, WM_IME_STARTCOMPOSITION,
PM_NOREMOVE | PM_NOYIELD) &&
WinUtils::PeekMessage(&compositionMsg, mMsg.hwnd, WM_IME_COMPOSITION,
WM_IME_COMPOSITION, PM_NOREMOVE | PM_NOYIELD) &&
WinUtils::PeekMessage(&charMsg, mMsg.hwnd, WM_CHAR, WM_CHAR,
PM_NOREMOVE | PM_NOYIELD) &&
startCompositionMsg.wParam == 0x0 &&
startCompositionMsg.lParam == 0x0 &&
compositionMsg.wParam == 0x0 &&
compositionMsg.lParam == 0x1BF &&
charMsg.wParam == VK_BACK && charMsg.lParam == 0x1 &&
startCompositionMsg.time <= compositionMsg.time &&
compositionMsg.time <= charMsg.time;
}
UINT
NativeKey::GetScanCodeWithExtendedFlag() const
{
// MapVirtualKeyEx() has been improved for supporting extended keys since
// Vista. When we call it for mapping a scancode of an extended key and
// a virtual keycode, we need to add 0xE000 to the scancode.
// On Win XP and Win Server 2003, this doesn't support. On them, we have
// no way to get virtual keycodes from scancode of extended keys.
if (!mIsExtended ||
WinUtils::GetWindowsVersion() < WinUtils::VISTA_VERSION) {
return mScanCode;
}
return (0xE000 | mScanCode);
}
uint32_t
NativeKey::GetKeyLocation() const
{
switch (mVirtualKeyCode) {
case VK_LSHIFT:
case VK_LCONTROL:
case VK_LMENU:
case VK_LWIN:
return nsIDOMKeyEvent::DOM_KEY_LOCATION_LEFT;
case VK_RSHIFT:
case VK_RCONTROL:
case VK_RMENU:
case VK_RWIN:
return nsIDOMKeyEvent::DOM_KEY_LOCATION_RIGHT;
case VK_RETURN:
// XXX This code assumes that all keyboard drivers use same mapping.
return !mIsExtended ? nsIDOMKeyEvent::DOM_KEY_LOCATION_STANDARD :
nsIDOMKeyEvent::DOM_KEY_LOCATION_NUMPAD;
case VK_INSERT:
case VK_DELETE:
case VK_END:
case VK_DOWN:
case VK_NEXT:
case VK_LEFT:
case VK_CLEAR:
case VK_RIGHT:
case VK_HOME:
case VK_UP:
case VK_PRIOR:
// XXX This code assumes that all keyboard drivers use same mapping.
return mIsExtended ? nsIDOMKeyEvent::DOM_KEY_LOCATION_STANDARD :
nsIDOMKeyEvent::DOM_KEY_LOCATION_NUMPAD;
// NumLock key isn't included due to IE9's behavior.
case VK_NUMPAD0:
case VK_NUMPAD1:
case VK_NUMPAD2:
case VK_NUMPAD3:
case VK_NUMPAD4:
case VK_NUMPAD5:
case VK_NUMPAD6:
case VK_NUMPAD7:
case VK_NUMPAD8:
case VK_NUMPAD9:
case VK_DECIMAL:
case VK_DIVIDE:
case VK_MULTIPLY:
case VK_SUBTRACT:
case VK_ADD:
// Separator key of Brazilian keyboard or JIS keyboard for Mac
case VK_ABNT_C2:
return nsIDOMKeyEvent::DOM_KEY_LOCATION_NUMPAD;
case VK_SHIFT:
case VK_CONTROL:
case VK_MENU:
NS_WARNING("Failed to decide the key location?");
default:
return nsIDOMKeyEvent::DOM_KEY_LOCATION_STANDARD;
}
}
uint8_t
NativeKey::ComputeVirtualKeyCodeFromScanCode() const
{
return static_cast<uint8_t>(
::MapVirtualKeyEx(mScanCode, MAPVK_VSC_TO_VK, mKeyboardLayout));
}
uint8_t
NativeKey::ComputeVirtualKeyCodeFromScanCodeEx() const
{
bool VistaOrLater =
(WinUtils::GetWindowsVersion() >= WinUtils::VISTA_VERSION);
// NOTE: WinXP doesn't support mapping scan code to virtual keycode of
// extended keys.
NS_ENSURE_TRUE(!mIsExtended || VistaOrLater, 0);
return static_cast<uint8_t>(
::MapVirtualKeyEx(GetScanCodeWithExtendedFlag(), MAPVK_VSC_TO_VK_EX,
mKeyboardLayout));
}
PRUnichar
NativeKey::ComputeUnicharFromScanCode() const
{
return static_cast<PRUnichar>(
::MapVirtualKeyEx(ComputeVirtualKeyCodeFromScanCode(),
MAPVK_VK_TO_CHAR, mKeyboardLayout));
}
void
NativeKey::InitKeyEvent(WidgetKeyboardEvent& aKeyEvent) const
{
InitKeyEvent(aKeyEvent, mModKeyState);
}
void
NativeKey::InitKeyEvent(WidgetKeyboardEvent& aKeyEvent,
const ModifierKeyState& aModKeyState) const
{
nsIntPoint point(0, 0);
mWidget->InitEvent(aKeyEvent, &point);
switch (aKeyEvent.message) {
case NS_KEY_DOWN:
aKeyEvent.keyCode = mDOMKeyCode;
// Unique id for this keydown event and its associated keypress.
sUniqueKeyEventId++;
aKeyEvent.mUniqueId = sUniqueKeyEventId;
break;
case NS_KEY_UP:
aKeyEvent.keyCode = mDOMKeyCode;
// Set defaultPrevented of the key event if the VK_MENU is not a system
// key release, so that the menu bar does not trigger. This helps avoid
// triggering the menu bar for ALT key accelerators used in assistive
// technologies such as Window-Eyes and ZoomText or for switching open
// state of IME.
aKeyEvent.mFlags.mDefaultPrevented =
(mOriginalVirtualKeyCode == VK_MENU && mMsg.message != WM_SYSKEYUP);
break;
case NS_KEY_PRESS:
aKeyEvent.mUniqueId = sUniqueKeyEventId;
break;
default:
MOZ_CRASH("Invalid event message");
}
aKeyEvent.mIsRepeat = IsRepeat();
aKeyEvent.mKeyNameIndex = mKeyNameIndex;
aKeyEvent.location = GetKeyLocation();
aModKeyState.InitInputEvent(aKeyEvent);
}
bool
NativeKey::DispatchKeyEvent(WidgetKeyboardEvent& aKeyEvent,
const MSG* aMsgSentToPlugin) const
{
if (mWidget->Destroyed()) {
MOZ_CRASH("NativeKey tries to dispatch a key event on destroyed widget");
}
KeyboardLayout::NotifyIdleServiceOfUserActivity();
NPEvent pluginEvent;
if (aMsgSentToPlugin &&
mWidget->GetInputContext().mIMEState.mEnabled == IMEState::PLUGIN) {
pluginEvent.event = aMsgSentToPlugin->message;
pluginEvent.wParam = aMsgSentToPlugin->wParam;
pluginEvent.lParam = aMsgSentToPlugin->lParam;
aKeyEvent.pluginEvent = static_cast<void*>(&pluginEvent);
}
return (mWidget->DispatchKeyboardEvent(&aKeyEvent) || mWidget->Destroyed());
}
bool
NativeKey::HandleKeyDownMessage(bool* aEventDispatched) const
{
MOZ_ASSERT(mMsg.message == WM_KEYDOWN || mMsg.message == WM_SYSKEYDOWN);
if (aEventDispatched) {
*aEventDispatched = false;
}
bool defaultPrevented = false;
if (mFakeCharMsgs ||
!RedirectedKeyDownMessageManager::IsRedirectedMessage(mMsg)) {
// Ignore [shift+]alt+space so the OS can handle it.
if (mModKeyState.IsAlt() && !mModKeyState.IsControl() &&
mVirtualKeyCode == VK_SPACE) {
return false;
}
bool isIMEEnabled = WinUtils::IsIMEEnabled(mWidget->GetInputContext());
WidgetKeyboardEvent keydownEvent(true, NS_KEY_DOWN, mWidget);
InitKeyEvent(keydownEvent, mModKeyState);
if (aEventDispatched) {
*aEventDispatched = true;