|
| 1 | +#include <cstring> |
| 2 | +#include <iostream> |
| 3 | +#include <string> |
| 4 | + |
| 5 | +#include "keyboard_monitor.h" |
| 6 | + |
| 7 | +// Import Cocoa headers |
| 8 | +#import <Cocoa/Cocoa.h> |
| 9 | +#import <CoreGraphics/CoreGraphics.h> |
| 10 | + |
| 11 | +namespace nativeapi { |
| 12 | + |
| 13 | +class KeyboardMonitor::Impl { |
| 14 | + public: |
| 15 | + Impl(KeyboardMonitor* monitor) : monitor_(monitor) {} |
| 16 | + |
| 17 | + CFMachPortRef eventTap = nullptr; |
| 18 | + CFRunLoopSourceRef runLoopSource = nullptr; |
| 19 | + KeyboardMonitor* monitor_; |
| 20 | +}; |
| 21 | + |
| 22 | +KeyboardMonitor::KeyboardMonitor() : impl_(std::make_unique<Impl>(this)), event_handler_(nullptr) {} |
| 23 | + |
| 24 | +KeyboardMonitor::~KeyboardMonitor() { |
| 25 | + Stop(); |
| 26 | +} |
| 27 | + |
| 28 | +// Callback function for keyboard events |
| 29 | +static CGEventRef keyboardEventCallback(CGEventTapProxy proxy, |
| 30 | + CGEventType type, |
| 31 | + CGEventRef event, |
| 32 | + void* refcon) { |
| 33 | + auto* monitor = static_cast<KeyboardMonitor*>(refcon); |
| 34 | + if (!monitor) |
| 35 | + return event; |
| 36 | + |
| 37 | + auto* eventHandler = monitor->GetEventHandler(); |
| 38 | + if (!eventHandler) |
| 39 | + return event; |
| 40 | + |
| 41 | + // Get the key code |
| 42 | + CGKeyCode keyCode = (CGKeyCode)CGEventGetIntegerValueField(event, kCGKeyboardEventKeycode); |
| 43 | + |
| 44 | + // Convert key code to string representation |
| 45 | + UniCharCount actualStringLength = 0; |
| 46 | + UniChar unicodeString[4]; |
| 47 | + CGEventKeyboardGetUnicodeString(event, 4, &actualStringLength, unicodeString); |
| 48 | + |
| 49 | + if (actualStringLength > 0) { |
| 50 | + std::string keyStr(actualStringLength, 0); |
| 51 | + for (UniCharCount i = 0; i < actualStringLength; ++i) { |
| 52 | + keyStr[i] = static_cast<char>(unicodeString[i]); |
| 53 | + } |
| 54 | + |
| 55 | + if (type == kCGEventKeyDown) { |
| 56 | + eventHandler->OnKeyPressed(keyStr); |
| 57 | + } else if (type == kCGEventKeyUp) { |
| 58 | + eventHandler->OnKeyReleased(keyStr); |
| 59 | + } |
| 60 | + } |
| 61 | + |
| 62 | + return event; |
| 63 | +} |
| 64 | + |
| 65 | +void KeyboardMonitor::Start() { |
| 66 | + if (impl_->eventTap != nullptr) { |
| 67 | + return; // Already started |
| 68 | + } |
| 69 | + |
| 70 | + // Create event tap for keyboard events |
| 71 | + impl_->eventTap = |
| 72 | + CGEventTapCreate(kCGSessionEventTap, // Monitor session-wide events |
| 73 | + kCGHeadInsertEventTap, // Insert at the head of the event queue |
| 74 | + kCGEventTapOptionDefault, // Default options |
| 75 | + CGEventMaskBit(kCGEventKeyDown) | |
| 76 | + CGEventMaskBit(kCGEventKeyUp), // Monitor key down and up events |
| 77 | + keyboardEventCallback, |
| 78 | + this); // Pass this pointer as user data |
| 79 | + |
| 80 | + if (impl_->eventTap == nullptr) { |
| 81 | + std::cerr << "Failed to create event tap" << std::endl; |
| 82 | + return; |
| 83 | + } |
| 84 | + |
| 85 | + // Create a run loop source |
| 86 | + impl_->runLoopSource = CFMachPortCreateRunLoopSource(kCFAllocatorDefault, impl_->eventTap, 0); |
| 87 | + |
| 88 | + // Add to the current run loop |
| 89 | + CFRunLoopAddSource(CFRunLoopGetCurrent(), impl_->runLoopSource, kCFRunLoopCommonModes); |
| 90 | + |
| 91 | + // Enable the event tap |
| 92 | + CGEventTapEnable(impl_->eventTap, true); |
| 93 | +} |
| 94 | + |
| 95 | +void KeyboardMonitor::Stop() { |
| 96 | + if (impl_->eventTap == nullptr) { |
| 97 | + return; // Already stopped |
| 98 | + } |
| 99 | + |
| 100 | + // Disable the event tap |
| 101 | + CGEventTapEnable(impl_->eventTap, false); |
| 102 | + |
| 103 | + // Remove from run loop |
| 104 | + if (impl_->runLoopSource != nullptr) { |
| 105 | + CFRunLoopRemoveSource(CFRunLoopGetCurrent(), impl_->runLoopSource, kCFRunLoopCommonModes); |
| 106 | + CFRelease(impl_->runLoopSource); |
| 107 | + impl_->runLoopSource = nullptr; |
| 108 | + } |
| 109 | + |
| 110 | + // Release the event tap |
| 111 | + CFRelease(impl_->eventTap); |
| 112 | + impl_->eventTap = nullptr; |
| 113 | +} |
| 114 | + |
| 115 | +bool KeyboardMonitor::IsMonitoring() const { |
| 116 | + return impl_->eventTap != nullptr; |
| 117 | +} |
| 118 | + |
| 119 | +} // namespace nativeapi |
0 commit comments