当前位置: 首页 > 面试题库 >

Windows中的JNA键盘钩

田英卓
2023-03-14
问题内容

我整理了一个JNA代码,用于在Windows中安装键盘挂钩(使用JNA示例)。代码编译完毕,一切就绪,我安装了挂钩(成功获取了挂钩的句柄),也可以成功卸载了挂钩。但是,当我按键盘上的任意键时,永远不会调用该回调。这是我的代码(其中大部分是从JNA示例获得的类型定义,对于我来说,直接转到“ main”)

import com.sun.jna.IntegerType;
import com.sun.jna.Pointer;
import com.sun.jna.PointerType;
import com.sun.jna.Structure;
import com.sun.jna.FromNativeContext;
import com.sun.jna.ptr.IntByReference;
import com.sun.jna.ptr.PointerByReference;
import com.sun.jna.win32.StdCallLibrary;
import com.sun.jna.win32.StdCallLibrary.StdCallCallback;
import com.sun.jna.Native;
import com.sun.jna.Platform;
import com.sun.jna.Library;
import com.sun.jna.win32.W32APITypeMapper;
import com.sun.jna.win32.W32APIFunctionMapper;

import java.util.Map;
import java.util.HashMap;

public class HelloWorld {
    static Map UNICODE_OPTIONS = new HashMap() {
        {
            put("type-mapper", W32APITypeMapper.UNICODE);
            put("function-mapper", W32APIFunctionMapper.UNICODE);
        }
    };

    public static class LONG_PTR extends IntegerType {
        public LONG_PTR() { this(0); }
        public LONG_PTR(long value) { super(Pointer.SIZE, value); }
    }

    public static class UINT_PTR extends IntegerType {
        public UINT_PTR() { super(Pointer.SIZE); }
        public UINT_PTR(long value) { super(Pointer.SIZE, value); }
        public Pointer toPointer() { return Pointer.createConstant(longValue()); }
    }

    public static class ULONG_PTR extends IntegerType {
        public ULONG_PTR() { this(0); }
        public ULONG_PTR(long value) { super(Pointer.SIZE, value); }
    }

    public static class LRESULT extends LONG_PTR {
        public LRESULT() { this(0); }
        public LRESULT(long value) { super(value); }
    }

    public static class WPARAM extends UINT_PTR {
        public WPARAM() { this(0); }
        public WPARAM(long value) { super(value); }
    }

    public static class LPARAM extends LONG_PTR {
        public LPARAM() { this(0); }
        public LPARAM(long value) { super(value); }
    }

    public static class KBDLLHOOKSTRUCT extends Structure {
        public int vkCode;
        public int scanCode;
        public int flags;
        public int time;
        public ULONG_PTR dwExtraInfo;
    }

    static HANDLE INVALID_HANDLE_VALUE = new HANDLE() {
        { super.setPointer(Pointer.createConstant(-1)); }
        public void setPointer(Pointer p) {
            throw new UnsupportedOperationException("Immutable reference");
        }
    };

    public static class HANDLE extends PointerType {
        public Object fromNative(Object nativeValue, FromNativeContext context) {
            Object o = super.fromNative(nativeValue, context);
            if (INVALID_HANDLE_VALUE.equals(o))
                return INVALID_HANDLE_VALUE;
            return o;
        }
    }

    public static class HHOOK extends HANDLE { }
    public static class HINSTANCE extends HANDLE { }
    public static class HMODULE extends HINSTANCE { }

    public interface User32 extends StdCallLibrary  {
        User32 INSTANCE = (User32)Native.loadLibrary("user32", User32.class, UNICODE_OPTIONS);

        static final int WH_KEYBOARD_LL = 13;

        public static interface HOOKPROC extends StdCallCallback  {
            LRESULT callback(int nCode, WPARAM wParam, KBDLLHOOKSTRUCT lParam);
        }

        HHOOK SetWindowsHookEx(int idHook, HOOKPROC lpfn, HMODULE hMod, int dwThreadId);
        LRESULT CallNextHookEx(HHOOK idHook, int nCode, WPARAM wParam, LPARAM lParam);
        LRESULT CallNextHookEx(HHOOK idHook, int nCode, WPARAM wParam, Pointer lParam);

        boolean UnhookWindowsHookEx(HHOOK idHook);
    }

    public interface Kernel32 extends StdCallLibrary {
        Kernel32 INSTANCE = (Kernel32)Native.loadLibrary("kernel32", Kernel32.class, UNICODE_OPTIONS);

        HMODULE GetModuleHandle(String name);
    }

    public static HHOOK hHook;
    public static User32.HOOKPROC lpfn;
    public static volatile boolean quit = false;

    public static void main(String[] args) throws Exception {
        HMODULE hMod = Kernel32.INSTANCE.GetModuleHandle(null);
        System.out.println(hMod);

        lpfn = new User32.HOOKPROC() {
            public LRESULT callback(int nCode, WPARAM wParam, KBDLLHOOKSTRUCT lParam) {
                System.out.println("here");
                quit = true;
                return User32.INSTANCE.CallNextHookEx(hHook, nCode, wParam, lParam.getPointer());
            }
        };

        hHook = User32.INSTANCE.SetWindowsHookEx(User32.WH_KEYBOARD_LL, lpfn, hMod, 0);
        System.out.println(hHook);

        if(hHook != null)
            System.out.println("Keyboard hooked, type anything to quit");

        while(!quit) {
            Thread.sleep(100);
        }

        if(User32.INSTANCE.UnhookWindowsHookEx(hHook))
            System.out.println("Unhooked");

    }
}

过去,我同时使用C ++和C#完成了几次键盘/鼠标挂钩。这是我第一次尝试使用Java,只是不知道是否正确导入和映射了库。有任何想法吗?

谢谢。


问题答案:

看样子你需要打电话GetMessage或者PeekMessage,这是奇怪的-这是不是说明文档中提到的钩子或LowLevelKeyboardProc。我对API的这一部分了解不足,无法猜测原因。

我只是使用示例类:

import com.sun.jna.examples.win32.*;

public class Callback {
  public static User32.HHOOK hHook;
  public static User32.LowLevelKeyboardProc lpfn;
  public static volatile boolean quit = false;

  public static void main(String[] args) throws Exception {
    W32API.HMODULE hMod = Kernel32.INSTANCE.GetModuleHandle(null);
    lpfn = new User32.LowLevelKeyboardProc() {
      public W32API.LRESULT callback(int nCode, W32API.WPARAM wParam,
          User32.KBDLLHOOKSTRUCT lParam) {
        System.out.println("here");
        quit = true;
        return User32.INSTANCE.CallNextHookEx(hHook, nCode, wParam, lParam
            .getPointer());
      }
    };
    hHook = User32.INSTANCE.SetWindowsHookEx(User32.WH_KEYBOARD_LL, lpfn, hMod,
        0);
    if (hHook == null)
      return;
    User32.MSG msg = new User32.MSG();
    while (!quit) {
      User32.INSTANCE.PeekMessage(msg, null, 0, 0, 0);
      Thread.sleep(100);
    }
    if (User32.INSTANCE.UnhookWindowsHookEx(hHook))
      System.out.println("Unhooked");
  }
}


 类似资料:
  • 本文向大家介绍C ++中的2键键盘,包括了C ++中的2键键盘的使用技巧和注意事项,需要的朋友参考一下 假设我们在文本编辑器中只有一个字符“ A”。对于每个步骤,我们可以对该字母执行两项操作- 全部复制-我们可以复制记事本中显示的所有字符 粘贴-我们可以粘贴上次复制的字符。 现在假设我们有一个数字n。通过执行允许的最小步骤数,我们必须在记事本上精确得到n个“ A”。我们必须以最少的步数找到结果以获

  • 问题内容: 我想在一个按钮中创建一个事件。 当我单击按钮时,我想打开Windows虚拟键盘。 您能帮我提供代码吗? 谢谢您的合作。 最好的祝福。 问题答案: 我认为这就是这么简单:

  • null 但这三条不同的道路对我都不起作用。对于路径,以“无法找到指定的路径”失败。对于和路径,会出现“无法启动屏幕键盘”错误对话框。 我在这里找到了一个可能的解决办法,它比我想要的要复杂一点(二阳的帖子):http://social.msdn.microsoft.com/forums/en-us/netfx64bit/thread/10ebc62f-e6d7-4072-9fd1-ea3784a0

  • 鼠标驱动的工作是触发按键按下和释放事件,通常我们只需要绑定系统的按键事件然后转换成 LCUI 的按键事件对象即可。 如需了解更多,可参考现有的鼠标驱动代码: ​src/platform/linux/linux_keyboard.c​ ​src/platform/linux/linux_keyboard.c​ ​src/platform/windows/windows_keyboard.c​ ​sr

  • 在Microsoft Windows 98中,键盘和鼠标是两个标准的使用者输入来源,在一些连贯操作中常产生互补作用。当然,鼠标在今天的应用程序中比十年前使用得更为广泛。甚至在一些应用程序中,我们更习惯于使用鼠标,例如在游戏、画图程序、音乐程序以及Web浏览器等程序中就是这样。然而,我们可以不使用鼠标,但绝对不能从一般的PC中把键盘拆掉。 相对于个人计算机的其它组件,键盘有非常久远的历史,它起源于1

  • jd.hideKeyboard(Object object) 在input、textarea等focus拉起键盘之后,手动调用此接口收起键盘 参数 Object object 属性 类型 必填 说明 success function 否 接口调用成功的回调函数 fail function 否 接口调用失败的回调函数 complete function 否 接口调用结束的回调函数(调用成功、失败都会