-
Notifications
You must be signed in to change notification settings - Fork 610
[linux] Adds raw key event handler #163
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
14ad12b
5345b29
5b537ba
8a0a72e
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,65 @@ | ||
// Copyright 2018 Google LLC | ||
// | ||
// Licensed under the Apache License, Version 2.0 (the "License"); | ||
// you may not use this file except in compliance with the License. | ||
// You may obtain a copy of the License at | ||
// | ||
// http://www.apache.org/licenses/LICENSE-2.0 | ||
// | ||
// Unless required by applicable law or agreed to in writing, software | ||
// distributed under the License is distributed on an "AS IS" BASIS, | ||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
// See the License for the specific language governing permissions and | ||
// limitations under the License. | ||
#include "library/linux/src/internal/key_event_handler.h" | ||
|
||
#include <json/json.h> | ||
#include <iostream> | ||
|
||
#include "library/linux/src/internal/json_message_codec.h" | ||
|
||
static constexpr char kChannelName[] = "flutter/keyevent"; | ||
|
||
static constexpr char kKeyCodeKey[] = "keyCode"; | ||
static constexpr char kKeyMapKey[] = "keymap"; | ||
static constexpr char kTypeKey[] = "type"; | ||
|
||
static constexpr char kAndroidKeyMap[] = "android"; | ||
static constexpr char kKeyUp[] = "keyup"; | ||
static constexpr char kKeyDown[] = "keydown"; | ||
// TODO: This event is not supported by Flutter. Add once it's implemented. | ||
static constexpr char kRepeat[] = "repeat"; | ||
|
||
namespace flutter_desktop_embedding { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Be consistent about horizontal spacing; if the namespace is going to have a blank line at the end, put one at the start as well. (Same for header.) |
||
|
||
KeyEventHandler::KeyEventHandler(const BinaryMessenger *messenger) | ||
: messenger_(messenger), channel_(kChannelName) {} | ||
|
||
KeyEventHandler::~KeyEventHandler() {} | ||
|
||
void KeyEventHandler::CharHook(GLFWwindow *window, unsigned int code_point) {} | ||
|
||
void KeyEventHandler::KeyboardHook(GLFWwindow *window, int key, int scancode, | ||
int action, int mods) { | ||
// TODO: Translate to a cross-platform key code system rather than passing | ||
// the native key code. | ||
Json::Value args; | ||
args[kKeyCodeKey] = key; | ||
args[kKeyMapKey] = kAndroidKeyMap; | ||
|
||
switch (action) { | ||
case GLFW_PRESS: | ||
args[kTypeKey] = kKeyDown; | ||
break; | ||
case GLFW_RELEASE: | ||
args[kTypeKey] = kKeyUp; | ||
break; | ||
default: | ||
std::cerr << "Unknown key event action: " << action << std::endl; | ||
return; | ||
} | ||
auto message = JsonMessageCodec::GetInstance().EncodeMessage(args); | ||
messenger_->Send(channel_, message->data(), message->size()); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. If you don't make messenger a constructor argument, you should check that it's not null before using it. |
||
} | ||
|
||
} // namespace flutter_desktop_embedding |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
// Copyright 2018 Google LLC | ||
// | ||
// Licensed under the Apache License, Version 2.0 (the "License"); | ||
// you may not use this file except in compliance with the License. | ||
// You may obtain a copy of the License at | ||
// | ||
// http://www.apache.org/licenses/LICENSE-2.0 | ||
// | ||
// Unless required by applicable law or agreed to in writing, software | ||
// distributed under the License is distributed on an "AS IS" BASIS, | ||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
// See the License for the specific language governing permissions and | ||
// limitations under the License. | ||
#ifndef LIBRARY_LINUX_SRC_INTERNAL_KEY_EVENT_HANDLER_H_ | ||
#define LIBRARY_LINUX_SRC_INTERNAL_kEY_EVENT_HANDLER_H_ | ||
|
||
#include "library/linux/include/flutter_desktop_embedding/binary_messenger.h" | ||
#include "library/linux/src/internal/keyboard_hook_handler.h" | ||
|
||
namespace flutter_desktop_embedding { | ||
|
||
// Implements a KeyboardHookHandler | ||
// | ||
// Handles key events and forwards them to the Flutter engine. | ||
class KeyEventHandler : public KeyboardHookHandler { | ||
public: | ||
explicit KeyEventHandler(const BinaryMessenger *messenger); | ||
virtual ~KeyEventHandler(); | ||
|
||
// KeyboardHookHandler. | ||
void KeyboardHook(GLFWwindow *window, int key, int scancode, int action, | ||
int mods) override; | ||
void CharHook(GLFWwindow *window, unsigned int code_point) override; | ||
|
||
private: | ||
// Binds this plugin to the given caller-owned binary messenger. It must | ||
// remain valid for the life of the plugin. | ||
const BinaryMessenger *messenger_; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. If you don't make this constructor-initialized, you should initialize it to nullptr here. |
||
std::string channel_; | ||
}; | ||
|
||
} // namespace flutter_desktop_embedding | ||
|
||
#endif // LIBRARY_LINUX_SRC_INTERNAL_KEY_EVENT_HANDLER_H_ |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Please add a quick comment explaining what this is, as you would with a class ivar.