gpt4 book ai didi

node.js - 从nodejs调用Windows SetWinEventHook

转载 作者:太空宇宙 更新时间:2023-11-03 23:17:19 25 4
gpt4 key购买 nike

我正在尝试调用SetWinEventHook如上所述here for C#但是来自nodejs。

我正在使用ffi-napi绑定(bind)到该函数。到目前为止,这是我的代码:

const ffi = require("ffi-napi")

const user32 = ffi.Library("user32", {
SetWinEventHook: ["int", ["int", "int", "pointer", "pointer", "int", "int", "int"]]
})

const pfnWinEventProc = ffi.Callback("void", ["pointer", "int", "pointer", "long", "long", "int", "int"],
function (hWinEventHook, event, hwnd, idObject, idChild, idEventThread, dwmsEventTime) {
console.log("Callback!")
console.log(arguments)
})

const EVENT_SYSTEM_FOREGROUND = 3
const WINEVENT_OUTOFCONTEXT = 0
const WINEVENT_SKPIOWNPROCESS = 2

user32.SetWinEventHook(EVENT_SYSTEM_FOREGROUND, EVENT_SYSTEM_FOREGROUND, null, pfnWinEventProc,
0, 0, WINEVENT_OUTOFCONTEXT | WINEVENT_SKPIOWNPROCESS)

setInterval(function () {
// keep the script alive
}, 500)

process.on("exit", function () {
console.log("Exiting")
pfnWinEventProc
})

问题只是我的回调没有被调用。每当焦点窗口更改时都应该调用它。

我也没有收到任何错误,所以我很迷失自己在这里做错了什么。

代码是here如果您想查看的话也可以。

最佳答案

我终于使用 cluster module 的组合让它工作了(如果您不想用消息循环阻止事件循环)并查看 Windows part of the active-win package .

完整代码为here .

const ffi = require("ffi-napi")
const cluster = require("cluster")
const ref = require("ref")
const wchar = require("ref-wchar")

if (cluster.isMaster) {
console.log("Main code here...")
cluster.fork()
} else {
const msgType = ref.types.void
const msgPtr = ref.refType(msgType)
const EVENT_SYSTEM_FOREGROUND = 3
const WINEVENT_OUTOFCONTEXT = 0
const WINEVENT_SKPIOWNPROCESS = 2

const user32 = ffi.Library("user32", {
SetWinEventHook: ["int", ["int", "int", "pointer", "pointer", "int", "int", "int"]],
GetWindowTextW: ["int", ["pointer", "pointer", "int"]],
GetWindowTextLengthW: ["int", ["pointer"]],
GetMessageA: ["bool", [msgPtr, "int", "uint", "uint"]]
})

function getMessage() {
return user32.GetMessageA(ref.alloc(msgPtr), null, 0, 0)
}

const pfnWinEventProc = ffi.Callback("void", ["pointer", "int", "pointer", "long", "long", "int", "int"],
function (hWinEventHook, event, hwnd, idObject, idChild, idEventThread, dwmsEventTime) {
const windowTitleLength = user32.GetWindowTextLengthW(hwnd)
const bufferSize = windowTitleLength * 2 + 4
const titleBuffer = Buffer.alloc(bufferSize)
user32.GetWindowTextW(hwnd, titleBuffer, bufferSize)
const titleText = ref.reinterpretUntilZeros(titleBuffer, wchar.size)
const finallyWindowTitle = wchar.toString(titleText)
console.log(finallyWindowTitle)
}
)

user32.SetWinEventHook(EVENT_SYSTEM_FOREGROUND, EVENT_SYSTEM_FOREGROUND, null, pfnWinEventProc,
0, 0, WINEVENT_OUTOFCONTEXT | WINEVENT_SKPIOWNPROCESS)

let res = getMessage()
while(res != 0) {
switch (res) {
case -1:
console.log("Invalid GetMessageA arguments or something!");
break
default:
console.log("Got a message!")
}
res = getMessage()
}
}

关于node.js - 从nodejs调用Windows SetWinEventHook,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53772437/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com