如何在 flutter 中将 SetWindowsHookEx 与 ffi 一起使用?
我想使用 windows hook 监听键盘事件,我们可以使用 SetWindowsHookEx 使用 MethodChannel 来实现,但是我想使用 ffi 来使用 dart 代码来实现这一点,我编写了一些代码,但它在 flutter 中不起作用,我无法接收任何代码在我调用 SetWindowsHookEx 之后,来自 Windows 的消息,这是我的一些 dart 代码:
final _kernel32 = DynamicLibrary.open('kernel32.dll');
final _user32 = DynamicLibrary.open('user32.dll');
late final HHOOK _hhk;
int hookproc(int code, int wParam, int lParam) {
return _CallNextHookEx(_hhk, nCode, wParam, lParam);
}
void setWindowsHook() {
const idHook = WH_KEYBOARD_LL;
final lpfn = Pointer.fromFunction<LRESULT Function(Int32, WPARAM, LPARAM)>(
hookproc,
0,
);
final hmod = _GetModuleHandleW(nullptr);
const dwThreadId = 0;
_hhk = _SetWindowsHookExW(idHook, lpfn, nullptr, dwThreadId);
}
late final _GetModuleHandleW = _kernel32
.lookup<NativeFunction<HMODULE Function(LPCWSTR)>>('GetModuleHandleW')
.asFunction<HMODULE Function(LPCWSTR)>();
late final _SetWindowsHookExW = _user32
.lookup<NativeFunction<HHOOK Function(Int32, HOOKPROC, HINSTANCE, DWORD)>>(
'SetWindowsHookExW')
.asFunction<HHOOK Function(int, HOOKPROC, HINSTANCE, int)>();
late final _CallNextHookEx = _user32
.lookup<NativeFunction<LRESULT Function(HHOOK, Int32, WPARAM, LPARAM)>>(
'CallNextHookEx')
.asFunction<int Function(HHOOK, int, int, int)>();
late final _UnhookWindowsHookEx = _user32
.lookup<NativeFunction<BOOL Function(HHOOK)>>('UnhookWindowsHookEx')
.asFunction<int Function(HHOOK)>();
有人知道如何解决这个问题吗?
I want to listen keyboard events use windows hook, we can do it with SetWindowsHookEx use MethodChannel, but I want to do this with dart code use ffi, I write some code but it doesn't work in flutter, I can't receive any message from windows after I called SetWindowsHookEx, here is some of my dart code:
final _kernel32 = DynamicLibrary.open('kernel32.dll');
final _user32 = DynamicLibrary.open('user32.dll');
late final HHOOK _hhk;
int hookproc(int code, int wParam, int lParam) {
return _CallNextHookEx(_hhk, nCode, wParam, lParam);
}
void setWindowsHook() {
const idHook = WH_KEYBOARD_LL;
final lpfn = Pointer.fromFunction<LRESULT Function(Int32, WPARAM, LPARAM)>(
hookproc,
0,
);
final hmod = _GetModuleHandleW(nullptr);
const dwThreadId = 0;
_hhk = _SetWindowsHookExW(idHook, lpfn, nullptr, dwThreadId);
}
late final _GetModuleHandleW = _kernel32
.lookup<NativeFunction<HMODULE Function(LPCWSTR)>>('GetModuleHandleW')
.asFunction<HMODULE Function(LPCWSTR)>();
late final _SetWindowsHookExW = _user32
.lookup<NativeFunction<HHOOK Function(Int32, HOOKPROC, HINSTANCE, DWORD)>>(
'SetWindowsHookExW')
.asFunction<HHOOK Function(int, HOOKPROC, HINSTANCE, int)>();
late final _CallNextHookEx = _user32
.lookup<NativeFunction<LRESULT Function(HHOOK, Int32, WPARAM, LPARAM)>>(
'CallNextHookEx')
.asFunction<int Function(HHOOK, int, int, int)>();
late final _UnhookWindowsHookEx = _user32
.lookup<NativeFunction<BOOL Function(HHOOK)>>('UnhookWindowsHookEx')
.asFunction<int Function(HHOOK)>();
Does anybody know how to solve this?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论