1
+ // Copyright 2018 Google LLC
2
+ //
3
+ // Licensed under the Apache License, Version 2.0 (the "License");
4
+ // you may not use this file except in compliance with the License.
5
+ // You may obtain a copy of the License at
6
+ //
7
+ // http://www.apache.org/licenses/LICENSE-2.0
8
+ //
9
+ // Unless required by applicable law or agreed to in writing, software
10
+ // distributed under the License is distributed on an "AS IS" BASIS,
11
+ // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12
+ // See the License for the specific language governing permissions and
13
+ // limitations under the License.
14
+ #include " library/linux/src/internal/key_event_handler.h"
15
+
16
+ #include < json/json.h>
17
+ #include < iostream>
18
+
19
+ #include " library/linux/src/internal/json_message_codec.h"
20
+
21
+ static constexpr char kChannelName [] = " flutter/keyevent" ;
22
+
23
+ static constexpr char kKeyCodeKey [] = " keyCode" ;
24
+ static constexpr char kKeyMapKey [] = " keymap" ;
25
+ static constexpr char kTypeKey [] = " type" ;
26
+
27
+ static constexpr char kAndroidKeyMap [] = " android" ;
28
+ static constexpr char kKeyUp [] = " keyup" ;
29
+ static constexpr char kKeyDown [] = " keydown" ;
30
+ // TODO: This event is not supported by Flutter. Add once it's implemented.
31
+ static constexpr char kRepeat [] = " repeat" ;
32
+
33
+ namespace flutter_desktop_embedding {
34
+
35
+ KeyEventHandler::KeyEventHandler (const BinaryMessenger *messenger)
36
+ : messenger_(messenger), channel_(kChannelName ) {}
37
+
38
+ KeyEventHandler::~KeyEventHandler () {}
39
+
40
+ void KeyEventHandler::CharHook (GLFWwindow *window, unsigned int code_point) {}
41
+
42
+ void KeyEventHandler::KeyboardHook (GLFWwindow *window, int key, int scancode,
43
+ int action, int mods) {
44
+ // TODO: Translate to a cross-platform key code system rather than passing
45
+ // the native key code.
46
+ Json::Value args;
47
+ args[kKeyCodeKey ] = key;
48
+ args[kKeyMapKey ] = kAndroidKeyMap ;
49
+
50
+ switch (action) {
51
+ case GLFW_PRESS:
52
+ args[kTypeKey ] = kKeyDown ;
53
+ break ;
54
+ case GLFW_RELEASE:
55
+ args[kTypeKey ] = kKeyUp ;
56
+ break ;
57
+ default :
58
+ std::cerr << " Unknown key event action: " << action << std::endl;
59
+ return ;
60
+ }
61
+ auto message = JsonMessageCodec::GetInstance ().EncodeMessage (args);
62
+ messenger_->Send (channel_, message->data (), message->size ());
63
+ }
64
+
65
+ } // namespace flutter_desktop_embedding
0 commit comments