在 Node.js 中获取鼠标选中的内容并不是一个直接的操作,需要通过调用系统 API 实现。以下是使用 C++ 扩展实现的代码:

#include <node.h>
#include <windows.h>
#include <string>

namespace mouse_selection {

    using v8::FunctionCallbackInfo;
    using v8::Isolate;
    using v8::Local;
    using v8::Object;
    using v8::String;
    using v8::Value;

    void GetSelection(const FunctionCallbackInfo<Value>& args) {
        Isolate* isolate = args.GetIsolate();
        HWND hwnd = GetFocus(); // 获取当前有焦点的窗口句柄
        if (hwnd == NULL) {
            isolate->ThrowException(
                String::NewFromUtf8(isolate, 'No window has focus'));
            return;
        }
        DWORD process_id;
        DWORD thread_id = GetWindowThreadProcessId(hwnd, &process_id); // 获取窗口所属进程的 ID
        if (thread_id == 0) {
            isolate->ThrowException(
                String::NewFromUtf8(isolate, 'Failed to get thread ID'));
            return;
        }
        HKL keyboard_layout = GetKeyboardLayout(thread_id); // 获取键盘布局
        if (keyboard_layout == NULL) {
            isolate->ThrowException(
                String::NewFromUtf8(isolate, 'Failed to get keyboard layout'));
            return;
        }
        // 模拟按下 Ctrl+C,复制选中的内容到剪贴板
        INPUT input[4];
        ZeroMemory(input, sizeof(input));
        input[0].type = INPUT_KEYBOARD;
        input[0].ki.wVk = VK_CONTROL;
        input[1].type = INPUT_KEYBOARD;
        input[1].ki.wVk = 'C';
        input[2].type = INPUT_KEYBOARD;
        input[2].ki.wVk = 'C';
        input[2].ki.dwFlags = KEYEVENTF_KEYUP;
        input[3].type = INPUT_KEYBOARD;
        input[3].ki.wVk = VK_CONTROL;
        input[3].ki.dwFlags = KEYEVENTF_KEYUP;
        SendInput(4, input, sizeof(INPUT));
        // 从剪贴板中获取选中的内容
        if (!OpenClipboard(NULL)) {
            isolate->ThrowException(
                String::NewFromUtf8(isolate, 'Failed to open clipboard'));
            return;
        }
        HANDLE clipboard_data = GetClipboardData(CF_UNICODETEXT);
        if (clipboard_data == NULL) {
            CloseClipboard();
            isolate->ThrowException(
                String::NewFromUtf8(isolate, 'Failed to get clipboard data'));
            return;
        }
        LPCWSTR text = (LPCWSTR)GlobalLock(clipboard_data);
        if (text == NULL) {
            CloseClipboard();
            isolate->ThrowException(
                String::NewFromUtf8(isolate, 'Failed to lock clipboard memory'));
            return;
        }
        std::wstring wstr(text);
        std::string str(wstr.begin(), wstr.end());
        GlobalUnlock(clipboard_data);
        CloseClipboard();
        args.GetReturnValue().Set(String::NewFromUtf8(isolate, str.c_str()));
    }

    void Initialize(Local<Object> exports) {
        NODE_SET_METHOD(exports, 'getSelection', GetSelection);
    }

    NODE_MODULE(NODE_GYP_MODULE_NAME, Initialize)

}

在 GetSelection 函数中,首先通过 GetFocus 函数获取当前有焦点的窗口句柄,然后调用 GetWindowThreadProcessId 函数获取窗口所属进程的 ID,再通过 GetKeyboardLayout 函数获取键盘布局。接着,使用 SendInput 函数模拟按下 Ctrl+C,将选中的内容复制到剪贴板中,最后使用 GetClipboardData 函数获取剪贴板中的数据,将 Unicode 字符串转换为 UTF-8 编码的 C++ 字符串返回给 Node.js。

Node.js 获取鼠标选中的文本内容(非复制内容) - C++ 扩展实现