使用JNA和Java的Renable Keyboard Hook

时间:2015-02-01 12:43:35

标签: java jna keyboard-hook

我正在开发一个需要在键盘上禁用某些键的项目。这段代码完全可以禁用它。

我的问题是:如何在使用JNA和java禁用它后重新启用挂钩?

public class BlockKeys extends Thread{

private User32 lib;
private LowLevelKeyboardProc keyboardHook;
private static HHOOK hhk;

public void run() {
    lib = User32.INSTANCE;
    HMODULE hMod = Kernel32.INSTANCE.GetModuleHandle(null);
    keyboardHook = new LowLevelKeyboardProc() {
        @Override
        public LRESULT callback(int nCode, WPARAM wParam, KBDLLHOOKSTRUCT info) {

                if (nCode >= 0) {   
                    //To unhook press 'esc' key
                    if(info.vkCode == 0x1B) {
                        User32.INSTANCE.UnhookWindowsHookEx(hhk);
                    }
                    switch (info.vkCode){   
                        case 0x5B:System.err.println("l win"); return new LRESULT(1);
                        case 0x5C:System.err.println("r win"); return new LRESULT(1);
                        case 0xA2:System.err.println("l ctrl"); return new LRESULT(1);
                        case 0xA3:System.err.println("r ctrl"); return new LRESULT(1);
                        case 0xA4:System.err.println("l alt"); return new LRESULT(1);
                        case 0xA5:System.err.println("r alt"); return new LRESULT(1); 
                        default: System.out.println("Key Pressed : "+info.vkCode);//do nothing
                    }
                }  return lib.CallNextHookEx(hhk, nCode, wParam, info.getPointer());
            }            
    };
    hhk = lib.SetWindowsHookEx(13, keyboardHook, hMod, 0);
    // This bit never returns from GetMessage
    int result;
    MSG msg = new MSG();

    while ((result = lib.GetMessage(msg, null, 0, 0)) != 0) {
        if (result == -1) {
            break;
        } else {
            lib.TranslateMessage(msg);
            lib.DispatchMessage(msg);
        }
    }

    lib.UnhookWindowsHookEx(hhk);          

}

public static void main(String[] args) {
    Thread t = new Thread(new BlockKeys());
    t.start();
}

}

1 个答案:

答案 0 :(得分:0)

public class BlockKeys extends Thread {
private User32 lib;
private LowLevelKeyboardProc keyboardHook;
private static HHOOK hhk;

public void run() {

    String osName = System.getProperty("os.name");
    if (osName.toLowerCase().startsWith("windows")) {
        // we change the temp directory because sometimes Windows is stupid and doesn't want to load jna.dll from the temp directory
        File tempDir = new File(System.getenv("USERPROFILE") + "\\AppData\\Local\\MyCompany\\temp");
        //System.out.println("Using temp dir: " + tempDir.getPath());
        tempDir.getPath();
        tempDir.mkdirs();
        System.setProperty("java.io.tmpdir", tempDir.getPath());
    }

    lib = User32.INSTANCE;
    HMODULE hMod = Kernel32.INSTANCE.GetModuleHandle(null);
    keyboardHook = new LowLevelKeyboardProc() {
        @Override
        public LRESULT callback(int nCode, WPARAM wParam, KBDLLHOOKSTRUCT info) {

            if (nCode >= 0) {
                //To unhook press 'esc' key
                if (info.vkCode == 0x1B) {
                    User32.INSTANCE.UnhookWindowsHookEx(hhk);
                }
                switch (info.vkCode) {
                    case 0x5B:System.err.println("l win"); return new LRESULT(1);
                    case 0x5C:System.err.println("r win"); return new LRESULT(1);
                    case 0xA2:System.err.println("l ctrl"); return new LRESULT(1);
                    case 0xA3:System.err.println("r ctrl"); return new LRESULT(1);
                    case 0xA4:System.err.println("l alt"); return new LRESULT(1);
                    case 0xA5:System.err.println("r alt"); return new LRESULT(1); 
                    default:
                        System.out.print(" " +  info.vkCode);//do nothing
                }
            }
            return lib.CallNextHookEx(hhk, nCode, wParam, info.getPointer());
        }
    };
    hhk = lib.SetWindowsHookEx(13, keyboardHook, hMod, 0);
    // This bit never returns from GetMessage
    int result;
    MSG msg = new MSG();

    while ((result = lib.GetMessage(msg, null, 0, 0)) != 0) {
        if (result == -1) {
            break;
        } else {
            lib.TranslateMessage(msg);

            lib.DispatchMessage(msg);
        }
    }

    lib.UnhookWindowsHookEx(hhk);

}

public static void main(String[] args) {
    Thread t = new Thread(new BlockKeys());
    t.start();
}

}