Flutter Engine
key_event_handler.cc
Go to the documentation of this file.
1 // Copyright 2013 The Flutter Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
4 
5 #include "flutter/shell/platform/windows/key_event_handler.h"
6 
7 #include <windows.h>
8 
9 #include <iostream>
10 
11 #include "flutter/shell/platform/common/cpp/json_message_codec.h"
12 
13 static constexpr char kChannelName[] = "flutter/keyevent";
14 
15 static constexpr char kKeyCodeKey[] = "keyCode";
16 static constexpr char kScanCodeKey[] = "scanCode";
17 static constexpr char kCharacterCodePointKey[] = "characterCodePoint";
18 static constexpr char kModifiersKey[] = "modifiers";
19 static constexpr char kKeyMapKey[] = "keymap";
20 static constexpr char kTypeKey[] = "type";
21 
22 static constexpr char kWindowsKeyMap[] = "windows";
23 static constexpr char kKeyUp[] = "keyup";
24 static constexpr char kKeyDown[] = "keydown";
25 
26 namespace flutter {
27 
28 // Re-definition of the modifiers for compatibility with the Flutter framework.
29 // These have to be in sync with the framework's RawKeyEventDataWindows
30 // modifiers definition.
31 // https://github.com/flutter/flutter/blob/19ff596979e407c484a32f4071420fca4f4c885f/packages/flutter/lib/src/services/raw_keyboard_windows.dart#L203
32 const int kShift = 1 << 0;
33 const int kShiftLeft = 1 << 1;
34 const int kShiftRight = 1 << 2;
35 const int kControl = 1 << 3;
36 const int kControlLeft = 1 << 4;
37 const int kControlRight = 1 << 5;
38 const int kAlt = 1 << 6;
39 const int kAltLeft = 1 << 7;
40 const int kAltRight = 1 << 8;
41 const int kWinLeft = 1 << 9;
42 const int kWinRight = 1 << 10;
43 const int kCapsLock = 1 << 11;
44 const int kNumLock = 1 << 12;
45 const int kScrollLock = 1 << 13;
46 
47 namespace {
48 /// Calls GetKeyState() an all modifier keys and packs the result in an int,
49 /// with the re-defined values declared above for compatibility with the Flutter
50 /// framework.
51 int GetModsForKeyState() {
52  int mods = 0;
53 
54  if (GetKeyState(VK_SHIFT) < 0)
55  mods |= kShift;
56  if (GetKeyState(VK_LSHIFT) < 0)
57  mods |= kShiftLeft;
58  if (GetKeyState(VK_RSHIFT) < 0)
59  mods |= kShiftRight;
60  if (GetKeyState(VK_CONTROL) < 0)
61  mods |= kControl;
62  if (GetKeyState(VK_LCONTROL) < 0)
63  mods |= kControlLeft;
64  if (GetKeyState(VK_RCONTROL) < 0)
65  mods |= kControlRight;
66  if (GetKeyState(VK_MENU) < 0)
67  mods |= kAlt;
68  if (GetKeyState(VK_LMENU) < 0)
69  mods |= kAltLeft;
70  if (GetKeyState(VK_RMENU) < 0)
71  mods |= kAltRight;
72  if (GetKeyState(VK_LWIN) < 0)
73  mods |= kWinLeft;
74  if (GetKeyState(VK_RWIN) < 0)
75  mods |= kWinRight;
76  if (GetKeyState(VK_CAPITAL) < 0)
77  mods |= kCapsLock;
78  if (GetKeyState(VK_NUMLOCK) < 0)
79  mods |= kNumLock;
80  if (GetKeyState(VK_SCROLL) < 0)
81  mods |= kScrollLock;
82  return mods;
83 }
84 } // namespace
85 
87  : channel_(
89  messenger,
92 
94 
96  const std::u16string& code_point) {}
97 
99  int key,
100  int scancode,
101  int action,
102  char32_t character) {
103  // TODO: Translate to a cross-platform key code system rather than passing
104  // the native key code.
105  rapidjson::Document event(rapidjson::kObjectType);
106  auto& allocator = event.GetAllocator();
107  event.AddMember(kKeyCodeKey, key, allocator);
108  event.AddMember(kScanCodeKey, scancode, allocator);
109  event.AddMember(kCharacterCodePointKey, character, allocator);
110  event.AddMember(kKeyMapKey, kWindowsKeyMap, allocator);
111  event.AddMember(kModifiersKey, GetModsForKeyState(), allocator);
112 
113  switch (action) {
114  case WM_KEYDOWN:
115  event.AddMember(kTypeKey, kKeyDown, allocator);
116  break;
117  case WM_KEYUP:
118  event.AddMember(kTypeKey, kKeyUp, allocator);
119  break;
120  default:
121  std::cerr << "Unknown key event action: " << action << std::endl;
122  return;
123  }
124  channel_->Send(event);
125 }
126 
127 } // namespace flutter
const int kControl
const int kControlRight
const int kCapsLock
const int kWinRight
static constexpr char kCharacterCodePointKey[]
const int kNumLock
const int kScrollLock
static constexpr char kKeyUp[]
static constexpr char kModifiersKey[]
const int kAltRight
const int kShift
SemanticsAction action
static constexpr char kScanCodeKey[]
static constexpr char kKeyMapKey[]
static constexpr char kWindowsKeyMap[]
static const JsonMessageCodec & GetInstance()
GdkEventButton * event
Definition: fl_view.cc:62
const int kControlLeft
static constexpr char kKeyDown[]
static constexpr char kChannelName[]
void KeyboardHook(GLFWwindow *window, int key, int scancode, int action, int mods) override
const int kShiftLeft
const int kShiftRight
static constexpr char kTypeKey[]
const int kAlt
KeyEventHandler(flutter::BinaryMessenger *messenger)
void TextHook(FlutterWindowsView *window, const std::u16string &text) override
constexpr char kTypeKey[]
Definition: shell.cc:39
static constexpr char kKeyCodeKey[]
const int kWinLeft
const int kAltLeft