'How can I use SetWindowsHookEx with ffi in flutter?
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?
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|
