summaryrefslogtreecommitdiffstats
path: root/widget/windows/KeyboardLayout.h
blob: 5f2b9b6004a14361d7dc52ea537ba41c744c182d (plain)
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
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
/* -*- 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/. */

#ifndef KeyboardLayout_h__
#define KeyboardLayout_h__

#include "mozilla/RefPtr.h"
#include "nscore.h"
#include "nsString.h"
#include "nsWindowBase.h"
#include "nsWindowDefs.h"
#include "mozilla/Attributes.h"
#include "mozilla/EventForwards.h"
#include "mozilla/TextEventDispatcher.h"
#include "mozilla/widget/WinMessages.h"
#include "mozilla/widget/WinModifierKeyState.h"
#include <windows.h>

#define NS_NUM_OF_KEYS 70

#define VK_OEM_1 0xBA     // ';:' for US
#define VK_OEM_PLUS 0xBB  // '+' any country
#define VK_OEM_COMMA 0xBC
#define VK_OEM_MINUS 0xBD  // '-' any country
#define VK_OEM_PERIOD 0xBE
#define VK_OEM_2 0xBF
#define VK_OEM_3 0xC0
// '/?' for Brazilian (ABNT)
#define VK_ABNT_C1 0xC1
// Separator in Numpad for Brazilian (ABNT) or JIS keyboard for Mac.
#define VK_ABNT_C2 0xC2
#define VK_OEM_4 0xDB
#define VK_OEM_5 0xDC
#define VK_OEM_6 0xDD
#define VK_OEM_7 0xDE
#define VK_OEM_8 0xDF
#define VK_OEM_102 0xE2
#define VK_OEM_CLEAR 0xFE

class nsIUserIdleServiceInternal;

namespace mozilla {
namespace widget {

enum ScanCode : uint16_t {
  eCapsLock = 0x003A,
  eNumLock = 0xE045,
  eShiftLeft = 0x002A,
  eShiftRight = 0x0036,
  eControlLeft = 0x001D,
  eControlRight = 0xE01D,
  eAltLeft = 0x0038,
  eAltRight = 0xE038,
};

// 0: nsIWidget's native modifier flag
// 1: Virtual keycode which does not distinguish whether left or right location.
// 2: Virtual keycode which distinguishes whether left or right location.
// 3: Scan code.
static const uint32_t sModifierKeyMap[][4] = {
    {nsIWidget::CAPS_LOCK, VK_CAPITAL, 0, ScanCode::eCapsLock},
    {nsIWidget::NUM_LOCK, VK_NUMLOCK, 0, ScanCode::eNumLock},
    {nsIWidget::SHIFT_L, VK_SHIFT, VK_LSHIFT, ScanCode::eShiftLeft},
    {nsIWidget::SHIFT_R, VK_SHIFT, VK_RSHIFT, ScanCode::eShiftRight},
    {nsIWidget::CTRL_L, VK_CONTROL, VK_LCONTROL, ScanCode::eControlLeft},
    {nsIWidget::CTRL_R, VK_CONTROL, VK_RCONTROL, ScanCode::eControlRight},
    {nsIWidget::ALT_L, VK_MENU, VK_LMENU, ScanCode::eAltLeft},
    {nsIWidget::ALT_R, VK_MENU, VK_RMENU, ScanCode::eAltRight}};

class KeyboardLayout;

class MOZ_STACK_CLASS UniCharsAndModifiers final {
 public:
  UniCharsAndModifiers() {}
  UniCharsAndModifiers operator+(const UniCharsAndModifiers& aOther) const;
  UniCharsAndModifiers& operator+=(const UniCharsAndModifiers& aOther);

  /**
   * Append a pair of unicode character and the final modifier.
   */
  void Append(char16_t aUniChar, Modifiers aModifiers);
  void Clear() {
    mChars.Truncate();
    mModifiers.Clear();
  }
  bool IsEmpty() const {
    MOZ_ASSERT(mChars.Length() == mModifiers.Length());
    return mChars.IsEmpty();
  }

  char16_t CharAt(size_t aIndex) const {
    MOZ_ASSERT(aIndex < Length());
    return mChars[aIndex];
  }
  Modifiers ModifiersAt(size_t aIndex) const {
    MOZ_ASSERT(aIndex < Length());
    return mModifiers[aIndex];
  }
  size_t Length() const {
    MOZ_ASSERT(mChars.Length() == mModifiers.Length());
    return mChars.Length();
  }

  bool IsProducingCharsWithAltGr() const {
    return !IsEmpty() && (ModifiersAt(0) & MODIFIER_ALTGRAPH) != 0;
  }

  void FillModifiers(Modifiers aModifiers);
  /**
   * OverwriteModifiersIfBeginsWith() assigns mModifiers with aOther between
   * [0] and [aOther.mLength - 1] only when mChars begins with aOther.mChars.
   */
  void OverwriteModifiersIfBeginsWith(const UniCharsAndModifiers& aOther);

  bool UniCharsEqual(const UniCharsAndModifiers& aOther) const;
  bool UniCharsCaseInsensitiveEqual(const UniCharsAndModifiers& aOther) const;
  bool BeginsWith(const UniCharsAndModifiers& aOther) const;

  const nsString& ToString() const { return mChars; }

 private:
  nsAutoString mChars;
  // 5 is enough number for normal keyboard layout handling.  On Windows,
  // a dead key sequence may cause inputting up to 5 characters per key press.
  CopyableAutoTArray<Modifiers, 5> mModifiers;
};

struct DeadKeyEntry {
  char16_t BaseChar;
  char16_t 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)));
  }

  char16_t GetCompositeChar(char16_t aBaseChar) const;
};

class VirtualKey {
 public:
  enum ShiftStateIndex : uint8_t {
    //  0 - Normal
    eNormal = 0,
    //  1 - Shift
    eShift,
    //  2 - Control
    eControl,
    //  3 - Control + Shift
    eControlShift,
    //  4 - Alt
    eAlt,
    //  5 - Alt + Shift
    eAltShift,
    //  6 - Alt + Control (AltGr)
    eAltGr,
    //  7 - Alt + Control + Shift (AltGr + Shift)
    eAltGrShift,
    //  8 - CapsLock
    eWithCapsLock,
    //  9 - CapsLock + Shift
    eShiftWithCapsLock,
    // 10 - CapsLock + Control
    eControlWithCapsLock,
    // 11 - CapsLock + Control + Shift
    eControlShiftWithCapsLock,
    // 12 - CapsLock + Alt
    eAltWithCapsLock,
    // 13 - CapsLock + Alt + Shift
    eAltShiftWithCapsLock,
    // 14 - CapsLock + Alt + Control (CapsLock + AltGr)
    eAltGrWithCapsLock,
    // 15 - CapsLock + Alt + Control + Shift (CapsLock + AltGr + Shift)
    eAltGrShiftWithCapsLock,
  };

  enum ShiftStateFlag {
    STATE_SHIFT = 0x01,
    STATE_CONTROL = 0x02,
    STATE_ALT = 0x04,
    STATE_CAPSLOCK = 0x08,
    // ShiftState needs to have AltGr state separately since this is necessary
    // for lossless conversion with Modifiers.
    STATE_ALTGRAPH = 0x80,
    // Useful to remove or check Ctrl and Alt flags.
    STATE_CONTROL_ALT = STATE_CONTROL | STATE_ALT,
  };

  typedef uint8_t ShiftState;

  static ShiftState ModifiersToShiftState(Modifiers aModifiers);
  static ShiftState ModifierKeyStateToShiftState(
      const ModifierKeyState& aModKeyState) {
    return ModifiersToShiftState(aModKeyState.GetModifiers());
  }
  static Modifiers ShiftStateToModifiers(ShiftState aShiftState);
  static bool IsAltGrIndex(uint8_t aIndex) {
    return (aIndex & STATE_CONTROL_ALT) == STATE_CONTROL_ALT;
  }

 private:
  union KeyShiftState {
    struct {
      char16_t Chars[4];
    } Normal;
    struct {
      const DeadKeyTable* Table;
      char16_t DeadChar;
    } DeadKey;
  };

  KeyShiftState mShiftStates[16];
  uint16_t mIsDeadKey;

  static uint8_t ToShiftStateIndex(ShiftState aShiftState) {
    if (!(aShiftState & STATE_ALTGRAPH)) {
      MOZ_ASSERT(aShiftState <= eAltGrShiftWithCapsLock);
      return static_cast<uint8_t>(aShiftState);
    }
    uint8_t index = aShiftState & ~STATE_ALTGRAPH;
    index |= (STATE_ALT | STATE_CONTROL);
    MOZ_ASSERT(index <= eAltGrShiftWithCapsLock);
    return index;
  }

  void SetDeadKey(ShiftState aShiftState, bool aIsDeadKey) {
    if (aIsDeadKey) {
      mIsDeadKey |= 1 << ToShiftStateIndex(aShiftState);
    } else {
      mIsDeadKey &= ~(1 << ToShiftStateIndex(aShiftState));
    }
  }

 public:
  static void FillKbdState(PBYTE aKbdState, const ShiftState aShiftState);

  bool IsDeadKey(ShiftState aShiftState) const {
    return (mIsDeadKey & (1 << ToShiftStateIndex(aShiftState))) != 0;
  }

  /**
   * IsChangedByAltGr() is useful to check if a key with AltGr produces
   * different character(s) from the key without AltGr.
   * Note that this is designed for checking if a keyboard layout has AltGr
   * key.  So, this result may not exactly correct for the key since it's
   * okay to fails in some edge cases when we check all keys which produce
   * character(s) in a layout.
   */
  bool IsChangedByAltGr(ShiftState aShiftState) const {
    MOZ_ASSERT(aShiftState == ToShiftStateIndex(aShiftState));
    MOZ_ASSERT(IsAltGrIndex(aShiftState));
    MOZ_ASSERT(IsDeadKey(aShiftState) ||
               mShiftStates[aShiftState].Normal.Chars[0]);
    const ShiftState kShiftStateWithoutAltGr =
        aShiftState - ShiftStateIndex::eAltGr;
    if (IsDeadKey(aShiftState) != IsDeadKey(kShiftStateWithoutAltGr)) {
      return false;
    }
    if (IsDeadKey(aShiftState)) {
      return mShiftStates[aShiftState].DeadKey.DeadChar !=
             mShiftStates[kShiftStateWithoutAltGr].DeadKey.DeadChar;
    }
    for (size_t i = 0; i < 4; i++) {
      if (mShiftStates[aShiftState].Normal.Chars[i] !=
          mShiftStates[kShiftStateWithoutAltGr].Normal.Chars[i]) {
        return true;
      }
      if (!mShiftStates[aShiftState].Normal.Chars[i] &&
          !mShiftStates[kShiftStateWithoutAltGr].Normal.Chars[i]) {
        return false;
      }
    }
    return false;
  }

  void AttachDeadKeyTable(ShiftState aShiftState,
                          const DeadKeyTable* aDeadKeyTable) {
    MOZ_ASSERT(aShiftState == ToShiftStateIndex(aShiftState));
    mShiftStates[aShiftState].DeadKey.Table = aDeadKeyTable;
  }

  void SetNormalChars(ShiftState aShiftState, const char16_t* aChars,
                      uint32_t aNumOfChars);
  void SetDeadChar(ShiftState aShiftState, char16_t aDeadChar);
  const DeadKeyTable* MatchingDeadKeyTable(const DeadKeyEntry* aDeadKeyArray,
                                           uint32_t aEntries) const;
  inline char16_t GetCompositeChar(ShiftState aShiftState,
                                   char16_t aBaseChar) const {
    return mShiftStates[ToShiftStateIndex(aShiftState)]
        .DeadKey.Table->GetCompositeChar(aBaseChar);
  }

  char16_t GetCompositeChar(const ModifierKeyState& aModKeyState,
                            char16_t aBaseChar) const {
    return GetCompositeChar(ModifierKeyStateToShiftState(aModKeyState),
                            aBaseChar);
  }

  /**
   * GetNativeUniChars() returns character(s) which is produced by the
   * key with given modifiers.  This does NOT return proper MODIFIER_ALTGRAPH
   * state because this is raw accessor of the database of this key.
   */
  UniCharsAndModifiers GetNativeUniChars(ShiftState aShiftState) const;
  UniCharsAndModifiers GetNativeUniChars(
      const ModifierKeyState& aModKeyState) const {
    return GetNativeUniChars(ModifierKeyStateToShiftState(aModKeyState));
  }

  /**
   * GetUniChars() returns characters and modifiers which are not consumed
   * to input the character.
   * For example, if you specify Ctrl key but the key produces no character
   * with Ctrl, this returns character(s) which is produced by the key
   * without Ctrl.  So, the result is useful to decide KeyboardEvent.key
   * value.
   * Another example is, if you specify Ctrl key and the key produces
   * different character(s) from the case without Ctrl key, this returns
   * the character(s) *without* MODIFIER_CONTROL.  This modifier information
   * is useful for eKeyPress since TextEditor does not treat eKeyPress events
   * whose modifier includes MODIFIER_ALT and/or MODIFIER_CONTROL.
   *
   * @param aShiftState         Modifiers which you want to retrieve
   *                            KeyboardEvent.key value for the key with.
   *                            If AltGr key is pressed, this should include
   *                            STATE_ALTGRAPH and should NOT include
   *                            STATE_ALT nor STATE_CONTROL.
   *                            If both Alt and Ctrl are pressed to emulate
   *                            AltGr, this should include both STATE_ALT and
   *                            STATE_CONTROL but should NOT include
   *                            MODIFIER_ALTGRAPH.
   *                            Then, this returns proper modifiers when
   *                            this key produces no character with AltGr.
   */
  UniCharsAndModifiers GetUniChars(ShiftState aShiftState) const;
  UniCharsAndModifiers GetUniChars(const ModifierKeyState& aModKeyState) const {
    return GetUniChars(ModifierKeyStateToShiftState(aModKeyState));
  }
};

class MOZ_STACK_CLASS NativeKey final {
  friend class KeyboardLayout;

 public:
  struct FakeCharMsg {
    UINT mCharCode;
    UINT mScanCode;
    bool mIsSysKey;
    bool mIsDeadKey;
    bool mConsumed;

    FakeCharMsg()
        : mCharCode(0),
          mScanCode(0),
          mIsSysKey(false),
          mIsDeadKey(false),
          mConsumed(false) {}

    MSG GetCharMsg(HWND aWnd) const {
      MSG msg;
      msg.hwnd = aWnd;
      msg.message = mIsDeadKey && mIsSysKey ? WM_SYSDEADCHAR
                    : mIsDeadKey            ? WM_DEADCHAR
                    : mIsSysKey             ? WM_SYSCHAR
                                            : WM_CHAR;
      msg.wParam = static_cast<WPARAM>(mCharCode);
      msg.lParam = static_cast<LPARAM>(mScanCode << 16);
      msg.time = 0;
      msg.pt.x = msg.pt.y = 0;
      return msg;
    }
  };

  NativeKey(nsWindowBase* aWidget, const MSG& aMessage,
            const ModifierKeyState& aModKeyState,
            HKL aOverrideKeyboardLayout = 0,
            nsTArray<FakeCharMsg>* aFakeCharMsgs = nullptr);

  ~NativeKey();

  /**
   * Handle WM_KEYDOWN message or WM_SYSKEYDOWN message.  The instance must be
   * initialized with WM_KEYDOWN or WM_SYSKEYDOWN.
   * Returns true if dispatched keydown event or keypress event is consumed.
   * Otherwise, false.
   */
  bool HandleKeyDownMessage(bool* aEventDispatched = nullptr) const;

  /**
   * Handles WM_CHAR message or WM_SYSCHAR message.  The instance must be
   * initialized with them.
   * Returns true if dispatched keypress event is consumed.  Otherwise, false.
   */
  bool HandleCharMessage(bool* aEventDispatched = nullptr) const;

  /**
   * Handles keyup message.  Returns true if the event is consumed.
   * Otherwise, false.
   */
  bool HandleKeyUpMessage(bool* aEventDispatched = nullptr) const;

  /**
   * Handles WM_APPCOMMAND message.  Returns true if the event is consumed.
   * Otherwise, false.
   */
  bool HandleAppCommandMessage() const;

  /**
   * Callback of TextEventDispatcherListener::WillDispatchKeyboardEvent().
   * This method sets alternative char codes of aKeyboardEvent.
   */
  void WillDispatchKeyboardEvent(WidgetKeyboardEvent& aKeyboardEvent,
                                 uint32_t aIndex);

  /**
   * Returns true if aChar is a control character which shouldn't be inputted
   * into focused text editor.
   */
  static bool IsControlChar(char16_t aChar);

  bool IsShift() const { return mModKeyState.IsShift(); }
  bool IsControl() const { return mModKeyState.IsControl(); }
  bool IsAlt() const { return mModKeyState.IsAlt(); }
  bool MaybeEmulatingAltGraph() const;
  Modifiers GetModifiers() const { return mModKeyState.GetModifiers(); }
  const ModifierKeyState& ModifierKeyStateRef() const { return mModKeyState; }
  VirtualKey::ShiftState GetShiftState() const {
    return VirtualKey::ModifierKeyStateToShiftState(mModKeyState);
  }

  /**
   * GenericVirtualKeyCode() returns virtual keycode which cannot distinguish
   * position of modifier keys.  E.g., VK_CONTROL for both ControlLeft and
   * ControlRight.
   */
  uint8_t GenericVirtualKeyCode() const { return mOriginalVirtualKeyCode; }

  /**
   * SpecificVirtualKeyCode() returns virtual keycode which can distinguish
   * position of modifier keys.  E.g., returns VK_LCONTROL or VK_RCONTROL
   * instead of VK_CONTROL.  If the key message is synthesized with not
   * enough information, this prefers left position's keycode.
   */
  uint8_t SpecificVirtualKeyCode() const { return mVirtualKeyCode; }

 private:
  NativeKey* mLastInstance;
  // mRemovingMsg is set at removing a char message from
  // GetFollowingCharMessage().
  MSG mRemovingMsg;
  // mReceivedMsg is set when another instance starts to handle the message
  // unexpectedly.
  MSG mReceivedMsg;
  RefPtr<nsWindowBase> mWidget;
  RefPtr<TextEventDispatcher> mDispatcher;
  HKL mKeyboardLayout;
  MSG mMsg;
  // mFollowingCharMsgs stores WM_CHAR, WM_SYSCHAR, WM_DEADCHAR or
  // WM_SYSDEADCHAR message which follows WM_KEYDOWN.
  // Note that the stored messaged are already removed from the queue.
  // FYI: 5 is enough number for usual keyboard layout handling.  On Windows,
  // a dead key sequence may cause inputting up to 5 characters per key press.
  AutoTArray<MSG, 5> mFollowingCharMsgs;
  // mRemovedOddCharMsgs stores WM_CHAR messages which are caused by ATOK or
  // WXG (they are Japanese IME) when the user tries to do "Kakutei-undo"
  // (it means "undo the last commit").
  nsTArray<MSG> mRemovedOddCharMsgs;
  // If dispatching eKeyDown or eKeyPress event causes focus change,
  // the instance shouldn't handle remaning char messages.  For checking it,
  // this should store first focused window.
  HWND mFocusedWndBeforeDispatch;

  uint32_t mDOMKeyCode;
  KeyNameIndex mKeyNameIndex;
  CodeNameIndex mCodeNameIndex;

  ModifierKeyState mModKeyState;

  // mVirtualKeyCode distinguishes left key or right key of modifier key.
  uint8_t mVirtualKeyCode;
  // mOriginalVirtualKeyCode doesn't distinguish left key or right key of
  // modifier key.  However, if the given keycode is VK_PROCESS, it's resolved
  // to a keycode before it's handled by IME.
  uint8_t mOriginalVirtualKeyCode;

  // mCommittedChars indicates the inputted characters which is committed by
  // the key.  If dead key fail to composite a character, mCommittedChars
  // indicates both the dead characters and the base characters.
  UniCharsAndModifiers mCommittedCharsAndModifiers;

  // Following strings are computed by
  // ComputeInputtingStringWithKeyboardLayout() which is typically called
  // before dispatching keydown event.
  // mInputtingStringAndModifiers's string is the string to be
  // inputted into the focused editor and its modifier state is proper
  // modifier state for inputting the string into the editor.
  UniCharsAndModifiers mInputtingStringAndModifiers;
  // mShiftedString is the string to be inputted into the editor with
  // current modifier state with active shift state.
  UniCharsAndModifiers mShiftedString;
  // mUnshiftedString is the string to be inputted into the editor with
  // current modifier state without shift state.
  UniCharsAndModifiers mUnshiftedString;
  // Following integers are computed by
  // ComputeInputtingStringWithKeyboardLayout() which is typically called
  // before dispatching keydown event.  The meaning of these values is same
  // as charCode.
  uint32_t mShiftedLatinChar;
  uint32_t mUnshiftedLatinChar;

  WORD mScanCode;
  bool mIsExtended;
  // mIsRepeat is true if the key message is caused by the auto-repeat
  // feature.
  bool mIsRepeat;
  bool mIsDeadKey;
  // mIsPrintableKey is true if the key may be a printable key without
  // any modifier keys.  Otherwise, false.
  // Please note that the event may not cause any text input even if this
  // is true.  E.g., it might be dead key state or Ctrl key may be pressed.
  bool mIsPrintableKey;
  // mIsSkippableInRemoteProcess is false if the key event shouldn't be
  // skipped in the remote process even if it's too old event.
  bool mIsSkippableInRemoteProcess;
  // mCharMessageHasGone is true if the message is a keydown message and
  // it's followed by at least one char message but it's gone at removing
  // from the queue.  This could occur if PeekMessage() or something is
  // hooked by odd tool.
  bool mCharMessageHasGone;
  // mIsOverridingKeyboardLayout is true if the instance temporarily overriding
  // keyboard layout with specified by the constructor.
  bool mIsOverridingKeyboardLayout;
  // mCanIgnoreModifierStateAtKeyPress is true if it's allowed to remove
  // Ctrl or Alt modifier state at dispatching eKeyPress.
  bool mCanIgnoreModifierStateAtKeyPress;

  nsTArray<FakeCharMsg>* mFakeCharMsgs;

  // When a keydown event is dispatched at handling WM_APPCOMMAND, the computed
  // virtual keycode is set to this.  Even if we consume WM_APPCOMMAND message,
  // Windows may send WM_KEYDOWN and WM_KEYUP message for them.
  // At that time, we should not dispatch key events for them.
  static uint8_t sDispatchedKeyOfAppCommand;

  NativeKey() {
    MOZ_CRASH("The default constructor of NativeKey isn't available");
  }

  void InitWithAppCommand();
  void InitWithKeyOrChar();

  /**
   * InitIsSkippableForKeyOrChar() initializes mIsSkippableInRemoteProcess with
   * mIsRepeat and previous key message information.  So, this must be called
   * after mIsRepeat is initialized.
   */
  void InitIsSkippableForKeyOrChar(const MSG& aLastKeyMSG);

  /**
   * InitCommittedCharsAndModifiersWithFollowingCharMessages() initializes
   * mCommittedCharsAndModifiers with mFollowingCharMsgs and mModKeyState.
   * If mFollowingCharMsgs includes non-printable char messages, they are
   * ignored (skipped).
   */
  void InitCommittedCharsAndModifiersWithFollowingCharMessages();

  UINT GetScanCodeWithExtendedFlag() const;

  // The result is one of eKeyLocation*.
  uint32_t GetKeyLocation() const;

  /**
   * RemoveFollowingOddCharMessages() removes odd WM_CHAR messages from the
   * queue when IsIMEDoingKakuteiUndo() returns true.
   */
  void RemoveFollowingOddCharMessages();

  /**
   * "Kakutei-Undo" of ATOK or WXG (both of them are Japanese IME) causes
   * strange WM_KEYDOWN/WM_KEYUP/WM_CHAR message pattern.  So, when this
   * returns true, the caller needs to be careful for processing the messages.
   */
  bool IsIMEDoingKakuteiUndo() const;

  /**
   * This returns true if user types a number key in numpad with Alt key
   * to input a Unicode character from its scalar value.
   * Note that inputting Unicode scalar value is available without NumLock.
   * Therefore, this returns true even if user presses a function key on
   * numpad without NumLock, but that may be intended to perform a shortcut
   * key like Alt + Home.
   */
  bool MaybeTypingUnicodeScalarValue() const {
    return !mIsExtended && IsSysKeyDownOrKeyUpMessage() && IsAlt() &&
           !IsControl() && !IsShift() &&
           ((mScanCode >= 0x004F && mScanCode <= 0x0052) ||  // Numpad0-3
            (mScanCode >= 0x004B && mScanCode <= 0x004D) ||  // Numpad4-6
            (mScanCode >= 0x0047 && mScanCode <= 0x0049));   // Numpad7-9
  }

  bool IsKeyDownMessage() const {
    return (mMsg.message == WM_KEYDOWN || mMsg.message == WM_SYSKEYDOWN ||
            mMsg.message == MOZ_WM_KEYDOWN);
  }
  bool IsKeyUpMessage() const {
    return (mMsg.message == WM_KEYUP || mMsg.message == WM_SYSKEYUP ||
            mMsg.message == MOZ_WM_KEYUP);
  }
  bool IsSysKeyDownOrKeyUpMessage() const {
    return mMsg.message == WM_SYSKEYDOWN || mMsg.message == WM_SYSKEYUP;
  }
  bool IsCharOrSysCharMessage(const MSG& aMSG) const {
    return IsCharOrSysCharMessage(aMSG.message);
  }
  bool IsCharOrSysCharMessage(UINT aMessage) const {
    return (aMessage == WM_CHAR || aMessage == WM_SYSCHAR);
  }
  bool IsCharMessage(const MSG& aMSG) const {
    return IsCharMessage(aMSG.message);
  }
  bool IsCharMessage(UINT aMessage) const {
    return (IsCharOrSysCharMessage(aMessage) || IsDeadCharMessage(aMessage));
  }
  bool IsDeadCharMessage(const MSG& aMSG) const {
    return IsDeadCharMessage(aMSG.message);
  }
  bool IsDeadCharMessage(UINT aMessage) const {
    return (aMessage == WM_DEADCHAR || aMessage == WM_SYSDEADCHAR);
  }
  bool IsSysCharMessage(const MSG& aMSG) const {
    return IsSysCharMessage(aMSG.message);
  }
  bool IsSysCharMessage(UINT aMessage) const {
    return (aMessage == WM_SYSCHAR || aMessage == WM_SYSDEADCHAR);
  }
  bool MayBeSameCharMessage(const MSG& aCharMsg1, const MSG& aCharMsg2) const;
  bool IsSamePhysicalKeyMessage(const MSG& aKeyOrCharMsg1,
                                const MSG& aKeyOrCharMsg2) const;
  bool IsFollowedByPrintableCharMessage() const;
  bool IsFollowedByPrintableCharOrSysCharMessage() const;
  bool IsFollowedByDeadCharMessage() const;
  bool IsKeyMessageOnPlugin() const {
    return (mMsg.message == MOZ_WM_KEYDOWN || mMsg.message == MOZ_WM_KEYUP);
  }
  bool IsPrintableCharMessage(const MSG& aMSG) const {
    return aMSG.message == WM_CHAR &&
           !IsControlChar(static_cast<char16_t>(aMSG.wParam));
  }
  bool IsEnterKeyPressCharMessage(const MSG& aMSG) const {
    return aMSG.message == WM_CHAR && aMSG.wParam == '\r';
  }
  bool IsPrintableCharOrSysCharMessage(const MSG& aMSG) const {
    return IsCharOrSysCharMessage(aMSG) &&
           !IsControlChar(static_cast<char16_t>(aMSG.wParam));
  }
  bool IsControlCharMessage(const MSG& aMSG) const {
    return IsCharMessage(aMSG.message) &&
           IsControlChar(static_cast<char16_t>(aMSG.wParam));
  }

  /**
   * IsReservedBySystem() returns true if the key combination is reserved by
   * the system.  Even if it's consumed by web apps, the message should be
   * sent to next wndproc.
   */
  bool IsReservedBySystem() const;

  /**
   * GetFollowingCharMessage() returns following char message of handling
   * keydown event.  If the message is found, this method returns true.
   * Otherwise, returns false.
   *
   * WARNING: Even if this returns true, aCharMsg may be WM_NULL or its
   *          hwnd may be different window.
   */
  bool GetFollowingCharMessage(MSG& aCharMsg);

  /**
   * Wraps MapVirtualKeyEx() with MAPVK_VSC_TO_VK.
   */
  uint8_t ComputeVirtualKeyCodeFromScanCode() const;

  /**
   * Wraps MapVirtualKeyEx() with MAPVK_VSC_TO_VK_EX.
   */
  uint8_t ComputeVirtualKeyCodeFromScanCodeEx() const;

  /**
   * Wraps MapVirtualKeyEx() with MAPVK_VK_TO_VSC_EX or MAPVK_VK_TO_VSC.
   */
  uint16_t ComputeScanCodeExFromVirtualKeyCode(UINT aVirtualKeyCode) const;

  /**
   * Wraps MapVirtualKeyEx() with MAPVK_VSC_TO_VK and MAPVK_VK_TO_CHAR.
   */
  char16_t ComputeUnicharFromScanCode() const;

  /**
   * Initializes the aKeyEvent with the information stored in the instance.
   */
  nsEventStatus InitKeyEvent(WidgetKeyboardEvent& aKeyEvent,
                             const ModifierKeyState& aModKeyState) const;
  nsEventStatus InitKeyEvent(WidgetKeyboardEvent& aKeyEvent) const;

  /**
   * Dispatches a command event for aEventCommand.
   * Returns true if the event is consumed.  Otherwise, false.
   */
  bool DispatchCommandEvent(uint32_t aEventCommand) const;

  /**
   * DispatchKeyPressEventsWithRetrievedCharMessages() dispatches keypress
   * event(s) with retrieved char messages.
   */
  bool DispatchKeyPressEventsWithRetrievedCharMessages() const;

  /**
   * DispatchKeyPressEventsWithoutCharMessage() dispatches keypress event(s)
   * without char messages.  So, this should be used only when there are no
   * following char messages.
   */
  bool DispatchKeyPressEventsWithoutCharMessage() const;

  /**
   * Checkes whether the key event down message is handled without following
   * WM_CHAR messages.  For example, if following WM_CHAR message indicates
   * control character input, the WM_CHAR message is unclear whether it's
   * caused by a printable key with Ctrl or just a function key such as Enter
   * or Backspace.
   */
  bool NeedsToHandleWithoutFollowingCharMessages() const;

  /**
   * ComputeInputtingStringWithKeyboardLayout() computes string to be inputted
   * with the key and the modifier state, without shift state and with shift
   * state.
   */
  void ComputeInputtingStringWithKeyboardLayout();

  /**
   * IsFocusedWindowChanged() returns true if focused window is changed
   * after the instance is created.
   */
  bool IsFocusedWindowChanged() const {
    return mFocusedWndBeforeDispatch != ::GetFocus();
  }

  /**
   * Handles WM_CHAR message or WM_SYSCHAR message.  The instance must be
   * initialized with WM_KEYDOWN, WM_SYSKEYDOWN or them.
   * Returns true if dispatched keypress event is consumed.  Otherwise, false.
   */
  bool HandleCharMessage(const MSG& aCharMsg,
                         bool* aEventDispatched = nullptr) const;

  // Calls of PeekMessage() from NativeKey might cause nested message handling
  // due to (perhaps) odd API hook.  NativeKey should do nothing if given
  // message is tried to be retrieved by another instance.

  /**
   * sLatestInstacne is a pointer to the newest instance of NativeKey which is
   * handling a key or char message(s).
   */
  static NativeKey* sLatestInstance;

  static const MSG sEmptyMSG;

  static MSG sLastKeyOrCharMSG;

  static MSG sLastKeyMSG;

  static bool IsEmptyMSG(const MSG& aMSG) {
    return !memcmp(&aMSG, &sEmptyMSG, sizeof(MSG));
  }

  bool IsAnotherInstanceRemovingCharMessage() const {
    return mLastInstance && !IsEmptyMSG(mLastInstance->mRemovingMsg);
  }

 public:
  /**
   * Returns last key or char MSG.  If no MSG has been received yet, the result
   * is empty MSG (i.e., .message is WM_NULL).
   */
  static const MSG& LastKeyOrCharMSG() { return sLastKeyOrCharMSG; }
};

class KeyboardLayout {
 public:
  static KeyboardLayout* GetInstance();
  static void Shutdown();
  static HKL GetActiveLayout();
  static nsCString GetActiveLayoutName();
  static void NotifyIdleServiceOfUserActivity();

  static bool IsPrintableCharKey(uint8_t aVirtualKey);

  /**
   * HasAltGr() returns true if the keyboard layout's AltRight key is AltGr
   * key.
   */
  bool HasAltGr() const { return mHasAltGr; }

  /**
   * IsDeadKey() returns true if aVirtualKey is a dead key with aModKeyState.
   * This method isn't stateful.
   */
  bool IsDeadKey(uint8_t aVirtualKey,
                 const ModifierKeyState& aModKeyState) const;
  bool IsDeadKey(const NativeKey& aNativeKey) const {
    return IsDeadKey(aNativeKey.GenericVirtualKeyCode(),
                     aNativeKey.ModifierKeyStateRef());
  }

  /**
   * IsInDeadKeySequence() returns true when it's in a dead key sequence.
   * It starts when a dead key is down and ends when another key down causes
   * inactivating the dead key state.
   */
  bool IsInDeadKeySequence() const { return !mActiveDeadKeys.IsEmpty(); }

  /**
   * IsSysKey() returns true if aVirtualKey with aModKeyState causes WM_SYSKEY*
   * or WM_SYS*CHAR messages.
   */
  bool IsSysKey(uint8_t aVirtualKey,
                const ModifierKeyState& aModKeyState) const;
  bool IsSysKey(const NativeKey& aNativeKey) const {
    return IsSysKey(aNativeKey.GenericVirtualKeyCode(),
                    aNativeKey.ModifierKeyStateRef());
  }

  /**
   * GetUniCharsAndModifiers() returns characters which are inputted by
   * aVirtualKey with aModKeyState.  This method isn't stateful.
   * Note that if the combination causes text input, the result's Ctrl and
   * Alt key state are never active.
   */
  UniCharsAndModifiers GetUniCharsAndModifiers(
      uint8_t aVirtualKey, const ModifierKeyState& aModKeyState) const {
    VirtualKey::ShiftState shiftState =
        VirtualKey::ModifierKeyStateToShiftState(aModKeyState);
    return GetUniCharsAndModifiers(aVirtualKey, shiftState);
  }
  UniCharsAndModifiers GetUniCharsAndModifiers(
      const NativeKey& aNativeKey) const {
    return GetUniCharsAndModifiers(aNativeKey.GenericVirtualKeyCode(),
                                   aNativeKey.GetShiftState());
  }

  /**
   * OnLayoutChange() must be called before the first keydown message is
   * received.  LoadLayout() changes the keyboard state, that causes breaking
   * dead key state.  Therefore, we need to load the layout before the first
   * keydown message.
   */
  void OnLayoutChange(HKL aKeyboardLayout) {
    MOZ_ASSERT(!mIsOverridden);
    LoadLayout(aKeyboardLayout);
  }

  /**
   * OverrideLayout() loads the specified keyboard layout.
   */
  void OverrideLayout(HKL aLayout) {
    mIsOverridden = true;
    LoadLayout(aLayout);
  }

  /**
   * RestoreLayout() loads the current keyboard layout of the thread.
   */
  void RestoreLayout() {
    mIsOverridden = false;
    mIsPendingToRestoreKeyboardLayout = true;
  }

  uint32_t ConvertNativeKeyCodeToDOMKeyCode(UINT aNativeKeyCode) const;

  /**
   * ConvertNativeKeyCodeToKeyNameIndex() returns KeyNameIndex value for
   * non-printable keys (except some special keys like space key).
   */
  KeyNameIndex ConvertNativeKeyCodeToKeyNameIndex(uint8_t aVirtualKey) const;

  /**
   * ConvertScanCodeToCodeNameIndex() returns CodeNameIndex value for
   * the given scan code.  aScanCode can be over 0xE000 since this method
   * doesn't use Windows API.
   */
  static CodeNameIndex ConvertScanCodeToCodeNameIndex(UINT aScanCode);

  HKL GetLayout() const {
    return mIsPendingToRestoreKeyboardLayout ? ::GetKeyboardLayout(0)
                                             : mKeyboardLayout;
  }

  /**
   * This wraps MapVirtualKeyEx() API with MAPVK_VK_TO_VSC.
   */
  WORD ComputeScanCodeForVirtualKeyCode(uint8_t aVirtualKeyCode) const;

  /**
   * Implementation of nsIWidget::SynthesizeNativeKeyEvent().
   */
  nsresult SynthesizeNativeKeyEvent(nsWindowBase* aWidget,
                                    int32_t aNativeKeyboardLayout,
                                    int32_t aNativeKeyCode,
                                    uint32_t aModifierFlags,
                                    const nsAString& aCharacters,
                                    const nsAString& aUnmodifiedCharacters);

 private:
  KeyboardLayout();
  ~KeyboardLayout();

  static KeyboardLayout* sInstance;
  static nsIUserIdleServiceInternal* sIdleService;

  struct DeadKeyTableListEntry {
    DeadKeyTableListEntry* next;
    uint8_t data[1];
  };

  HKL mKeyboardLayout;

  VirtualKey mVirtualKeys[NS_NUM_OF_KEYS];
  DeadKeyTableListEntry* mDeadKeyTableListHead;
  // When mActiveDeadKeys is empty, it's not in dead key sequence.
  // Otherwise, it contains virtual keycodes which are pressed in current
  // dead key sequence.
  nsTArray<uint8_t> mActiveDeadKeys;
  // mDeadKeyShiftStates is always same length as mActiveDeadKeys.
  // This stores shift states at pressing each dead key stored in
  // mActiveDeadKeys.
  nsTArray<VirtualKey::ShiftState> mDeadKeyShiftStates;

  bool mIsOverridden;
  bool mIsPendingToRestoreKeyboardLayout;
  bool mHasAltGr;

  static inline int32_t GetKeyIndex(uint8_t aVirtualKey);
  static int CompareDeadKeyEntries(const void* aArg1, const void* aArg2,
                                   void* aData);
  static bool AddDeadKeyEntry(char16_t aBaseChar, char16_t aCompositeChar,
                              DeadKeyEntry* aDeadKeyArray, uint32_t aEntries);
  bool EnsureDeadKeyActive(bool aIsActive, uint8_t aDeadKey,
                           const PBYTE aDeadKeyKbdState);
  uint32_t GetDeadKeyCombinations(uint8_t aDeadKey,
                                  const PBYTE aDeadKeyKbdState,
                                  uint16_t aShiftStatesWithBaseChars,
                                  DeadKeyEntry* aDeadKeyArray,
                                  uint32_t aMaxEntries);
  /**
   * Activates or deactivates dead key state.
   */
  void ActivateDeadKeyState(const NativeKey& aNativeKey);
  void DeactivateDeadKeyState();

  const DeadKeyTable* AddDeadKeyTable(const DeadKeyEntry* aDeadKeyArray,
                                      uint32_t aEntries);
  void ReleaseDeadKeyTables();

  /**
   * Loads the specified keyboard layout. This method always clear the dead key
   * state.
   */
  void LoadLayout(HKL aLayout);

  /**
   * Gets the keyboard layout name of aLayout.  Be careful, this may be too
   * slow to call at handling user input.
   */
  nsCString GetLayoutName(HKL aLayout) const;

  /**
   * InitNativeKey() must be called when actually widget receives WM_KEYDOWN or
   * WM_KEYUP.  This method is stateful.  This saves current dead key state at
   * WM_KEYDOWN.  Additionally, computes current inputted character(s) and set
   * them to the aNativeKey.
   */
  void InitNativeKey(NativeKey& aNativeKey);

  /**
   * MaybeInitNativeKeyAsDeadKey() initializes aNativeKey only when aNativeKey
   * is a dead key's event.
   * When it's not in a dead key sequence, this activates the dead key state.
   * When it's in a dead key sequence, this initializes aNativeKey with a
   * composite character or a preceding dead char and a dead char which should
   * be caused by aNativeKey.
   * Returns true when this initializes aNativeKey.  Otherwise, false.
   */
  bool MaybeInitNativeKeyAsDeadKey(NativeKey& aNativeKey);

  /**
   * MaybeInitNativeKeyWithCompositeChar() may initialize aNativeKey with
   * proper composite character when dead key produces a composite character.
   * Otherwise, just returns false.
   */
  bool MaybeInitNativeKeyWithCompositeChar(NativeKey& aNativeKey);

  /**
   * See the comment of GetUniCharsAndModifiers() below.
   */
  UniCharsAndModifiers GetUniCharsAndModifiers(
      uint8_t aVirtualKey, VirtualKey::ShiftState aShiftState) const;

  /**
   * GetDeadUniCharsAndModifiers() returns dead chars which are stored in
   * current dead key sequence.  So, this is stateful.
   */
  UniCharsAndModifiers GetDeadUniCharsAndModifiers() const;

  /**
   * GetCompositeChar() returns a composite character with dead character
   * caused by mActiveDeadKeys, mDeadKeyShiftStates and a base character
   * (aBaseChar).
   * If the combination of the dead character and the base character doesn't
   * cause a composite character, this returns 0.
   */
  char16_t GetCompositeChar(char16_t aBaseChar) const;

  // NativeKey class should access InitNativeKey() directly, but it shouldn't
  // be available outside of NativeKey.  So, let's make NativeKey a friend
  // class of this.
  friend class NativeKey;
};

class RedirectedKeyDownMessageManager {
 public:
  /*
   * If a window receives WM_KEYDOWN message or WM_SYSKEYDOWM message which is
   * a redirected message, NativeKey::DispatchKeyDownAndKeyPressEvent()
   * prevents to dispatch eKeyDown event because it has been dispatched
   * before the message was redirected.  However, in some cases, WM_*KEYDOWN
   * message handler may not handle actually.  Then, the message handler needs
   * to forget the redirected message and remove WM_CHAR message or WM_SYSCHAR
   * message for the redirected keydown message.  AutoFlusher class is a helper
   * class for doing it.  This must be created in the stack.
   */
  class MOZ_STACK_CLASS AutoFlusher final {
   public:
    AutoFlusher(nsWindowBase* aWidget, const MSG& aMsg)
        : mCancel(!RedirectedKeyDownMessageManager::IsRedirectedMessage(aMsg)),
          mWidget(aWidget),
          mMsg(aMsg) {}

    ~AutoFlusher() {
      if (mCancel) {
        return;
      }
      // Prevent unnecessary keypress event
      if (!mWidget->Destroyed()) {
        RedirectedKeyDownMessageManager::RemoveNextCharMessage(mMsg.hwnd);
      }
      // Foreget the redirected message
      RedirectedKeyDownMessageManager::Forget();
    }

    void Cancel() { mCancel = true; }

   private:
    bool mCancel;
    RefPtr<nsWindowBase> mWidget;
    const MSG& mMsg;
  };

  static void WillRedirect(const MSG& aMsg, bool aDefualtPrevented) {
    sRedirectedKeyDownMsg = aMsg;
    sDefaultPreventedOfRedirectedMsg = aDefualtPrevented;
  }

  static void Forget() { sRedirectedKeyDownMsg.message = WM_NULL; }

  static void PreventDefault() { sDefaultPreventedOfRedirectedMsg = true; }
  static bool DefaultPrevented() { return sDefaultPreventedOfRedirectedMsg; }

  static bool IsRedirectedMessage(const MSG& aMsg);

  /**
   * RemoveNextCharMessage() should be called by WM_KEYDOWN or WM_SYSKEYDOWM
   * message handler.  If there is no WM_(SYS)CHAR message for it, this
   * method does nothing.
   * NOTE: WM_(SYS)CHAR message is posted by TranslateMessage() API which is
   * called in message loop.  So, WM_(SYS)KEYDOWN message should have
   * WM_(SYS)CHAR message in the queue if the keydown event causes character
   * input.
   */
  static void RemoveNextCharMessage(HWND aWnd);

 private:
  // sRedirectedKeyDownMsg is WM_KEYDOWN message or WM_SYSKEYDOWN message which
  // is reirected with SendInput() API by
  // widget::NativeKey::DispatchKeyDownAndKeyPressEvent()
  static MSG sRedirectedKeyDownMsg;
  static bool sDefaultPreventedOfRedirectedMsg;
};

}  // namespace widget
}  // namespace mozilla

#endif