[feat] keyboard control supported on MacOSX

This commit is contained in:
dijunkun
2024-11-22 18:05:13 +08:00
parent 8414a57a5b
commit ba3edcc02a
2 changed files with 88 additions and 6 deletions

View File

@@ -1,5 +1,81 @@
#include "keyboard_capturer.h"
#include <ApplicationServices/ApplicationServices.h>
#include <unordered_map>
#include "rd_log.h"
// vkCode 到 CGKeyCode 的映射表
std::unordered_map<int, CGKeyCode> vkCodeToCGKeyCode = {
// 字母键
{0x41, 0x00}, // A
{0x42, 0x0B}, // B
{0x43, 0x08}, // C
{0x44, 0x02}, // D
{0x45, 0x0E}, // E
{0x46, 0x03}, // F
{0x47, 0x05}, // G
{0x48, 0x04}, // H
{0x49, 0x22}, // I
{0x4A, 0x26}, // J
{0x4B, 0x28}, // K
{0x4C, 0x25}, // L
{0x4D, 0x2E}, // M
{0x4E, 0x2D}, // N
{0x4F, 0x1F}, // O
{0x50, 0x23}, // P
{0x51, 0x0C}, // Q
{0x52, 0x0F}, // R
{0x53, 0x01}, // S
{0x54, 0x11}, // T
{0x55, 0x20}, // U
{0x56, 0x09}, // V
{0x57, 0x0D}, // W
{0x58, 0x07}, // X
{0x59, 0x10}, // Y
{0x5A, 0x06}, // Z
// 数字键
{0x30, 0x1D}, // 0
{0x31, 0x12}, // 1
{0x32, 0x13}, // 2
{0x33, 0x14}, // 3
{0x34, 0x15}, // 4
{0x35, 0x17}, // 5
{0x36, 0x16}, // 6
{0x37, 0x1A}, // 7
{0x38, 0x1C}, // 8
{0x39, 0x19}, // 9
// 功能键
{0x20, 0x31}, // 空格
{0x0D, 0x24}, // 回车
{0x08, 0x33}, // 退格
{0x1B, 0x35}, // Esc
{0x2E, 0x75}, // Delete
// 箭头键
{0x25, 0x7B}, // 左
{0x27, 0x7C}, // 右
{0x26, 0x7E}, // 上
{0x28, 0x7D}, // 下
// 功能键 F1-F12
{0x70, 0x7A}, // F1
{0x71, 0x78}, // F2
{0x72, 0x63}, // F3
{0x73, 0x76}, // F4
{0x74, 0x60}, // F5
{0x75, 0x61}, // F6
{0x76, 0x62}, // F7
{0x77, 0x64}, // F8
{0x78, 0x65}, // F9
{0x79, 0x6D}, // F10
{0x7A, 0x67}, // F11
{0x7B, 0x6F}, // F12
};
KeyboardCapturer::KeyboardCapturer() {}
KeyboardCapturer::~KeyboardCapturer() {}
@@ -11,5 +87,12 @@ int KeyboardCapturer::Hook(OnKeyAction on_key_action, void *user_ptr) {
int KeyboardCapturer::Unhook() { return 0; }
int KeyboardCapturer::SendKeyboardCommand(int key_code, bool is_down) {
if (vkCodeToCGKeyCode.find(key_code) != vkCodeToCGKeyCode.end()) {
CGKeyCode cg_key_code = vkCodeToCGKeyCode[key_code];
CGEventRef event = CGEventCreateKeyboardEvent(NULL, cg_key_code, is_down);
CGEventPost(kCGHIDEventTap, event);
CFRelease(event);
}
return 0;
}

View File

@@ -295,12 +295,11 @@ int Render::StopMouseController() {
}
int Render::StartKeyboardCapturer() {
if (!device_controller_factory_) {
LOG_INFO("Device controller factory is nullptr");
if (!keyboard_capturer_) {
LOG_INFO("keyboard capturer is nullptr");
return -1;
}
keyboard_capturer_ = (KeyboardCapturer*)device_controller_factory_->Create(
DeviceControllerFactory::Device::Keyboard);
int keyboard_capturer_init_ret = keyboard_capturer_->Hook(
[](int key_code, bool is_down, void* user_ptr) {
if (user_ptr) {
@@ -323,8 +322,6 @@ int Render::StartKeyboardCapturer() {
int Render::StopKeyboardCapturer() {
if (keyboard_capturer_) {
keyboard_capturer_->Unhook();
delete keyboard_capturer_;
keyboard_capturer_ = nullptr;
}
return 0;
}
@@ -789,6 +786,8 @@ int Render::Run() {
// mouse control/keyboard capturer
device_controller_factory_ = new DeviceControllerFactory();
keyboard_capturer_ = (KeyboardCapturer*)device_controller_factory_->Create(
DeviceControllerFactory::Device::Keyboard);
// RTC
CreateConnectionPeer();