如何在 flutter 中将 SetWindowsHookEx 与 ffi 一起使用?

发布于 2025-01-10 19:43:30 字数 1640 浏览 0 评论 0原文

我想使用 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文