From 611ec18fe4098c3448a2aefaf832b5c737e0b675 Mon Sep 17 00:00:00 2001 From: onlyless Date: Sat, 27 Jun 2026 17:43:11 +0800 Subject: [PATCH] fix: keep macOS cursor visible after keyboard input --- src/platform/macos/input.cpp | 29 +++++++++++++++++++++++++++++ src/platform/macos/misc.mm | 21 ++++++++++++++++++++- 2 files changed, 49 insertions(+), 1 deletion(-) diff --git a/src/platform/macos/input.cpp b/src/platform/macos/input.cpp index fe17a9b35a1..7698601637d 100644 --- a/src/platform/macos/input.cpp +++ b/src/platform/macos/input.cpp @@ -327,6 +327,8 @@ const KeyCodeMap kKeyCodesMap[] = { } } + void reveal_cursor(input_t &input); + void keyboard_update(input_t &input, uint16_t modcode, bool release, uint8_t flags) { auto key = keysym(modcode); @@ -370,6 +372,7 @@ const KeyCodeMap kKeyCodesMap[] = { CGEventSetFlags(event, macos_input->kb_flags); CGEventPost(kCGSessionEventTap, event); CFRelease(event); + reveal_cursor(input); } void unicode(input_t &input, char *utf8, int size) { @@ -402,6 +405,32 @@ const KeyCodeMap kKeyCodesMap[] = { }; } + /** + * @brief Nudge macOS into making the cursor visible after keyboard input. + * + * @param input Platform input context. + */ + void reveal_cursor(input_t &input) { + auto macos_input = static_cast(input.get()); + auto current = get_mouse_loc(input); + auto location = CGPoint {current.x, current.y}; + + CGAssociateMouseAndMouseCursorPosition(true); + CGDisplayShowCursor(macos_input->display); + + auto event = CGEventCreateMouseEvent(macos_input->source, kCGEventMouseMoved, location, kCGMouseButtonLeft); + if (!event) { + return; + } + + CGEventSetDoubleValueField(event, kCGMouseEventDeltaX, 0); + CGEventSetDoubleValueField(event, kCGMouseEventDeltaY, 0); + CGEventSetFlags(event, macos_input->kb_flags); + CGEventPost(kCGHIDEventTap, event); + CFRelease(event); + CGWarpMouseCursorPosition(location); + } + /** * @brief Post a mouse event at the clamped display location. * diff --git a/src/platform/macos/misc.mm b/src/platform/macos/misc.mm index 56fca134028..f67cc61771e 100644 --- a/src/platform/macos/misc.mm +++ b/src/platform/macos/misc.mm @@ -19,6 +19,7 @@ // platform includes #include #include +#include #include #include #include @@ -258,7 +259,25 @@ void set_thread_name(const std::string &name) { } void enable_mouse_keys() { - // Unimplemented + if (CGAssociateMouseAndMouseCursorPosition(true) != kCGErrorSuccess) { + BOOST_LOG(debug) << "Unable to associate mouse movement with cursor position"sv; + } + + uint32_t display_count = 0; + if (CGGetOnlineDisplayList(0, nullptr, &display_count) != kCGErrorSuccess || display_count == 0) { + CGDisplayShowCursor(kCGNullDirectDisplay); + return; + } + + std::vector displays(display_count); + if (CGGetOnlineDisplayList(display_count, displays.data(), &display_count) != kCGErrorSuccess) { + CGDisplayShowCursor(kCGNullDirectDisplay); + return; + } + + for (uint32_t i = 0; i < display_count; ++i) { + CGDisplayShowCursor(displays[i]); + } } void streaming_will_start() {