mirror of
https://github.com/kunkundi/crossdesk.git
synced 2026-08-01 22:47:36 +08:00
[fix] fix macOS remote keyboard modifier injection, fixes #81
This commit is contained in:
@@ -269,10 +269,30 @@ inline bool IsFunctionKey(int key_code) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
CGEventFlags ToCGEventFlags(uint32_t injected_flags) {
|
||||||
|
CGEventFlags flags = 0;
|
||||||
|
if ((injected_flags & kMacInjectedModifierShift) != 0) {
|
||||||
|
flags |= kCGEventFlagMaskShift;
|
||||||
|
}
|
||||||
|
if ((injected_flags & kMacInjectedModifierControl) != 0) {
|
||||||
|
flags |= kCGEventFlagMaskControl;
|
||||||
|
}
|
||||||
|
if ((injected_flags & kMacInjectedModifierOption) != 0) {
|
||||||
|
flags |= kCGEventFlagMaskAlternate;
|
||||||
|
}
|
||||||
|
if ((injected_flags & kMacInjectedModifierCommand) != 0) {
|
||||||
|
flags |= kCGEventFlagMaskCommand;
|
||||||
|
}
|
||||||
|
return flags;
|
||||||
|
}
|
||||||
|
|
||||||
int KeyboardCapturer::SendKeyboardCommand(int key_code, bool is_down,
|
int KeyboardCapturer::SendKeyboardCommand(int key_code, bool is_down,
|
||||||
uint32_t scan_code, bool extended) {
|
uint32_t scan_code, bool extended) {
|
||||||
(void)scan_code;
|
(void)scan_code;
|
||||||
(void)extended;
|
(void)extended;
|
||||||
|
const uint32_t injected_flags =
|
||||||
|
injected_modifier_state_.Update(key_code, is_down);
|
||||||
|
|
||||||
if (vkCodeToCGKeyCode.find(key_code) != vkCodeToCGKeyCode.end()) {
|
if (vkCodeToCGKeyCode.find(key_code) != vkCodeToCGKeyCode.end()) {
|
||||||
CGKeyCode cg_key_code = vkCodeToCGKeyCode[key_code];
|
CGKeyCode cg_key_code = vkCodeToCGKeyCode[key_code];
|
||||||
CGEventRef event = CGEventCreateKeyboardEvent(NULL, cg_key_code, is_down);
|
CGEventRef event = CGEventCreateKeyboardEvent(NULL, cg_key_code, is_down);
|
||||||
@@ -281,7 +301,7 @@ int KeyboardCapturer::SendKeyboardCommand(int key_code, bool is_down,
|
|||||||
return -1;
|
return -1;
|
||||||
}
|
}
|
||||||
|
|
||||||
CGEventSetFlags(event, 0);
|
CGEventSetFlags(event, ToCGEventFlags(injected_flags));
|
||||||
CGEventPost(kCGHIDEventTap, event);
|
CGEventPost(kCGHIDEventTap, event);
|
||||||
CFRelease(event);
|
CFRelease(event);
|
||||||
|
|
||||||
|
|||||||
@@ -10,6 +10,7 @@
|
|||||||
#include <ApplicationServices/ApplicationServices.h>
|
#include <ApplicationServices/ApplicationServices.h>
|
||||||
|
|
||||||
#include "device_controller.h"
|
#include "device_controller.h"
|
||||||
|
#include "macos_keyboard_modifier_state.h"
|
||||||
|
|
||||||
namespace crossdesk {
|
namespace crossdesk {
|
||||||
|
|
||||||
@@ -36,6 +37,7 @@ class KeyboardCapturer : public DeviceController {
|
|||||||
bool option_flag_ = false;
|
bool option_flag_ = false;
|
||||||
bool command_flag_ = false;
|
bool command_flag_ = false;
|
||||||
int fn_key_code_ = 0x3F;
|
int fn_key_code_ = 0x3F;
|
||||||
|
MacKeyboardModifierState injected_modifier_state_;
|
||||||
};
|
};
|
||||||
} // namespace crossdesk
|
} // namespace crossdesk
|
||||||
#endif
|
#endif
|
||||||
|
|||||||
@@ -0,0 +1,93 @@
|
|||||||
|
/*
|
||||||
|
* @Author: DI JUNKUN
|
||||||
|
* @Date: 2026-05-21
|
||||||
|
* Copyright (c) 2026 by DI JUNKUN, All Rights Reserved.
|
||||||
|
*/
|
||||||
|
|
||||||
|
#ifndef _MACOS_KEYBOARD_MODIFIER_STATE_H_
|
||||||
|
#define _MACOS_KEYBOARD_MODIFIER_STATE_H_
|
||||||
|
|
||||||
|
#include <cstdint>
|
||||||
|
|
||||||
|
namespace crossdesk {
|
||||||
|
|
||||||
|
inline constexpr uint32_t kMacInjectedModifierShift = 1u << 0;
|
||||||
|
inline constexpr uint32_t kMacInjectedModifierControl = 1u << 1;
|
||||||
|
inline constexpr uint32_t kMacInjectedModifierOption = 1u << 2;
|
||||||
|
inline constexpr uint32_t kMacInjectedModifierCommand = 1u << 3;
|
||||||
|
|
||||||
|
class MacKeyboardModifierState {
|
||||||
|
public:
|
||||||
|
uint32_t Update(int key_code, bool is_down) {
|
||||||
|
bool* state = MutableStateForVk(key_code);
|
||||||
|
if (state != nullptr) {
|
||||||
|
*state = is_down;
|
||||||
|
}
|
||||||
|
return flags();
|
||||||
|
}
|
||||||
|
|
||||||
|
uint32_t flags() const {
|
||||||
|
uint32_t result = 0;
|
||||||
|
if (left_shift_down_ || right_shift_down_) {
|
||||||
|
result |= kMacInjectedModifierShift;
|
||||||
|
}
|
||||||
|
if (left_control_down_ || right_control_down_) {
|
||||||
|
result |= kMacInjectedModifierControl;
|
||||||
|
}
|
||||||
|
if (left_option_down_ || right_option_down_) {
|
||||||
|
result |= kMacInjectedModifierOption;
|
||||||
|
}
|
||||||
|
if (left_command_down_ || right_command_down_) {
|
||||||
|
result |= kMacInjectedModifierCommand;
|
||||||
|
}
|
||||||
|
return result;
|
||||||
|
}
|
||||||
|
|
||||||
|
void Clear() {
|
||||||
|
left_shift_down_ = false;
|
||||||
|
right_shift_down_ = false;
|
||||||
|
left_control_down_ = false;
|
||||||
|
right_control_down_ = false;
|
||||||
|
left_option_down_ = false;
|
||||||
|
right_option_down_ = false;
|
||||||
|
left_command_down_ = false;
|
||||||
|
right_command_down_ = false;
|
||||||
|
}
|
||||||
|
|
||||||
|
private:
|
||||||
|
bool* MutableStateForVk(int key_code) {
|
||||||
|
switch (key_code) {
|
||||||
|
case 0xA0: // VK_LSHIFT
|
||||||
|
return &left_shift_down_;
|
||||||
|
case 0xA1: // VK_RSHIFT
|
||||||
|
return &right_shift_down_;
|
||||||
|
case 0xA2: // VK_LCONTROL
|
||||||
|
return &left_control_down_;
|
||||||
|
case 0xA3: // VK_RCONTROL
|
||||||
|
return &right_control_down_;
|
||||||
|
case 0xA4: // VK_LMENU / left Option
|
||||||
|
return &left_option_down_;
|
||||||
|
case 0xA5: // VK_RMENU / right Option
|
||||||
|
return &right_option_down_;
|
||||||
|
case 0x5B: // VK_LWIN / left Command
|
||||||
|
return &left_command_down_;
|
||||||
|
case 0x5C: // VK_RWIN / right Command
|
||||||
|
return &right_command_down_;
|
||||||
|
default:
|
||||||
|
return nullptr;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
bool left_shift_down_ = false;
|
||||||
|
bool right_shift_down_ = false;
|
||||||
|
bool left_control_down_ = false;
|
||||||
|
bool right_control_down_ = false;
|
||||||
|
bool left_option_down_ = false;
|
||||||
|
bool right_option_down_ = false;
|
||||||
|
bool left_command_down_ = false;
|
||||||
|
bool right_command_down_ = false;
|
||||||
|
};
|
||||||
|
|
||||||
|
} // namespace crossdesk
|
||||||
|
|
||||||
|
#endif
|
||||||
@@ -0,0 +1,60 @@
|
|||||||
|
#include "macos_keyboard_modifier_state.h"
|
||||||
|
|
||||||
|
#include <cstdint>
|
||||||
|
#include <iostream>
|
||||||
|
|
||||||
|
namespace {
|
||||||
|
|
||||||
|
bool ExpectEqual(const char* name, uint32_t actual, uint32_t expected) {
|
||||||
|
if (actual == expected) {
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
std::cerr << name << " mismatch\n"
|
||||||
|
<< " expected: " << expected << "\n"
|
||||||
|
<< " actual: " << actual << "\n";
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
} // namespace
|
||||||
|
|
||||||
|
int main() {
|
||||||
|
crossdesk::MacKeyboardModifierState state;
|
||||||
|
|
||||||
|
bool ok = true;
|
||||||
|
ok &= ExpectEqual("initial flags", state.flags(), 0);
|
||||||
|
ok &= ExpectEqual("left shift down", state.Update(0xA0, true),
|
||||||
|
crossdesk::kMacInjectedModifierShift);
|
||||||
|
ok &= ExpectEqual("shifted semicolon keeps shift",
|
||||||
|
state.Update(0xBA, true),
|
||||||
|
crossdesk::kMacInjectedModifierShift);
|
||||||
|
ok &= ExpectEqual("semicolon up keeps shift", state.Update(0xBA, false),
|
||||||
|
crossdesk::kMacInjectedModifierShift);
|
||||||
|
ok &= ExpectEqual("right shift down while left held",
|
||||||
|
state.Update(0xA1, true),
|
||||||
|
crossdesk::kMacInjectedModifierShift);
|
||||||
|
ok &= ExpectEqual("left shift up while right held", state.Update(0xA0, false),
|
||||||
|
crossdesk::kMacInjectedModifierShift);
|
||||||
|
ok &= ExpectEqual("right shift up clears shift", state.Update(0xA1, false),
|
||||||
|
0);
|
||||||
|
|
||||||
|
ok &= ExpectEqual("left control down", state.Update(0xA2, true),
|
||||||
|
crossdesk::kMacInjectedModifierControl);
|
||||||
|
ok &= ExpectEqual("right alt adds option", state.Update(0xA5, true),
|
||||||
|
crossdesk::kMacInjectedModifierControl |
|
||||||
|
crossdesk::kMacInjectedModifierOption);
|
||||||
|
ok &= ExpectEqual("left command adds command", state.Update(0x5B, true),
|
||||||
|
crossdesk::kMacInjectedModifierControl |
|
||||||
|
crossdesk::kMacInjectedModifierOption |
|
||||||
|
crossdesk::kMacInjectedModifierCommand);
|
||||||
|
ok &= ExpectEqual("left control up leaves option command",
|
||||||
|
state.Update(0xA2, false),
|
||||||
|
crossdesk::kMacInjectedModifierOption |
|
||||||
|
crossdesk::kMacInjectedModifierCommand);
|
||||||
|
ok &= ExpectEqual("right alt up leaves command", state.Update(0xA5, false),
|
||||||
|
crossdesk::kMacInjectedModifierCommand);
|
||||||
|
ok &= ExpectEqual("left command up clears all", state.Update(0x5B, false),
|
||||||
|
0);
|
||||||
|
|
||||||
|
return ok ? 0 : 1;
|
||||||
|
}
|
||||||
@@ -33,6 +33,12 @@ function setup_targets()
|
|||||||
add_files("tests/path_manager_portable_test.cpp",
|
add_files("tests/path_manager_portable_test.cpp",
|
||||||
"src/path_manager/path_manager.cpp")
|
"src/path_manager/path_manager.cpp")
|
||||||
|
|
||||||
|
target("macos_keyboard_modifier_state_test")
|
||||||
|
set_kind("binary")
|
||||||
|
set_default(false)
|
||||||
|
add_includedirs("src/device_controller")
|
||||||
|
add_files("tests/macos_keyboard_modifier_state_test.cpp")
|
||||||
|
|
||||||
target("screen_capturer")
|
target("screen_capturer")
|
||||||
set_kind("object")
|
set_kind("object")
|
||||||
add_deps("rd_log", "common")
|
add_deps("rd_log", "common")
|
||||||
|
|||||||
Reference in New Issue
Block a user