Node.js 和 C++ 使用 WinAPI 监听 Ctrl 键按下获取鼠标选中的文件类型
以下是一个基本的示例代码,使用 Node.js 和 C++ 调用 WinAPI 来监视 Ctrl 键的按下,并获取鼠标选中的文件以及判断文件类型并打印出来。
C++ 代码:
#include <windows.h>
#include <shlwapi.h>
#include <iostream>
#pragma comment(lib, "shlwapi.lib")
HHOOK hook;
HWND hWnd;
LRESULT CALLBACK HookProc(int nCode, WPARAM wParam, LPARAM lParam) {
if (nCode == HC_ACTION) {
if ((GetAsyncKeyState(VK_CONTROL) & 0x8000) && (wParam == WM_LBUTTONUP)) {
POINT cursorPos;
GetCursorPos(&cursorPos);
char fileName[MAX_PATH] = { 0 };
SHFILEINFO fileInfo = { 0 };
SHGetFileInfo((LPCTSTR)fileName, FILE_ATTRIBUTE_NORMAL, &fileInfo, sizeof(fileInfo), SHGFI_TYPENAME | SHGFI_USEFILEATTRIBUTES | SHGFI_DISPLAYNAME);
std::cout << "Selected file: " << fileInfo.szDisplayName << std::endl;
std::cout << "File type: " << fileInfo.szTypeName << std::endl;
}
}
return CallNextHookEx(hook, nCode, wParam, lParam);
}
extern "C" __declspec(dllexport) void SetHook(HWND hwnd) {
hWnd = hwnd;
hook = SetWindowsHookEx(WH_MOUSE_LL, HookProc, NULL, 0);
}
extern "C" __declspec(dllexport) void Unhook() {
UnhookWindowsHookEx(hook);
}
Node.js 代码:
const ffi = require('ffi-napi');
const ref = require('ref-napi');
const int = ref.types.int;
const HWND = ref.refType(int);
const voidPtr = ref.refType(ref.types.void);
const user32 = ffi.Library('user32', {
'SetWindowsHookExA': ['pointer', ['int', 'pointer', 'pointer', 'int']],
'UnhookWindowsHookEx': ['bool', ['pointer']],
'CallNextHookEx': ['pointer', ['pointer', 'int', 'pointer', 'pointer']],
'GetCurrentThreadId': ['int', []],
'GetLastError': ['uint32', []],
'GetModuleHandleW': ['pointer', ['pointer']],
'PostMessageA': ['bool', ['pointer', 'uint32', 'uint32', 'uint32']],
});
const kernel32 = ffi.Library('kernel32', {
'GetLastError': ['uint32', []],
});
const setHook = (hwnd) => {
const hookProc = ffi.Callback('pointer', ['int', 'uint32', 'pointer'], function (nCode, wParam, lParam) {
if (nCode === 0) {
const ctrlPressed = user32.GetAsyncKeyState(0x11) & 0x8000;
const leftButtonUp = wParam === 0x202;
if (ctrlPressed && leftButtonUp) {
const point = ref.alloc('int', lParam.dword >> 0x10);
const fileName = Buffer.alloc(0x200);
user32.PostMessageA(hwnd, 0x112, 0xf100, point.deref());
user32.SHGetFileInfoW(fileName, 0x80, fileInfo.ref(), fileInfo.length, 0x100 | 0x400 | 0x200);
console.log('Selected file:', ref.readCString(fileInfo.display_name.buffer));
console.log('File type:', ref.readCString(fileInfo.type_name.buffer));
}
}
return user32.CallNextHookEx(hook, nCode, wParam, lParam);
});
const threadId = user32.GetCurrentThreadId();
const hModule = user32.GetModuleHandleW(null);
const hook = user32.SetWindowsHookExA(0x0D, hookProc, hModule, threadId);
if (hook.isNull()) {
throw new Error('Failed to set hook: ' + kernel32.GetLastError());
}
};
const unhook = () => {
const success = user32.UnhookWindowsHookEx(hook);
if (!success) {
throw new Error('Failed to unhook: ' + kernel32.GetLastError());
}
};
const fileInfo = ref.types.Struct({
icon: 'pointer',
icon_index: 'int',
attributes: 'uint32',
display_name: ref.types.CString,
type_name: ref.types.CString,
});
setHook(user32.GetActiveWindow());
此示例代码展示了如何使用 Node.js 和 C++ 结合 WinAPI 实现监听 Ctrl 键按下,并获取鼠标选中的文件和其类型。代码使用了 FFI 库来调用 WinAPI 函数,并使用 ref 库来处理数据类型。您可以根据需要进行修改和扩展。
**注意:**此示例代码需要在 Windows 操作系统上运行。
原文地址: https://www.cveoy.top/t/topic/oBj2 著作权归作者所有。请勿转载和采集!