Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
1.2k views
in Technique[技术] by (71.8m points)

is it is possible to disable the windows keys using java

is it is possible to disable the windows keys and alt+tab using java ...

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

You can use JNA to achieve this.

Include jna.jar and platform.jar on the classpath and create the following class. This class disables left windows key (0x5B) and right windows key (0x5C). So you can add other codes at the switch case statement.

Call KeyHook.blockWindowsKey() as soon as possible when your application starts. On the other side, call unblockWindowsKey() at app shutdown.

Since the code is only executed when isWindows() is true, you can always call KeyHook.blockWindowsKey(), even when running on another OS.



    import com.sun.jna.platform.win32.Kernel32;
    import com.sun.jna.platform.win32.User32;
    import com.sun.jna.platform.win32.WinDef.HMODULE;
    import com.sun.jna.platform.win32.WinDef.LRESULT;
    import com.sun.jna.platform.win32.WinDef.WPARAM;
    import com.sun.jna.platform.win32.WinUser.HHOOK;
    import com.sun.jna.platform.win32.WinUser.KBDLLHOOKSTRUCT;
    import com.sun.jna.platform.win32.WinUser.LowLevelKeyboardProc;
    import com.sun.jna.platform.win32.WinUser.MSG;


    public class KeyHook {
        private static HHOOK hhk;
        private static LowLevelKeyboardProc keyboardHook;
        private static User32 lib;

        public static void blockWindowsKey() {
            if (isWindows()) {
                new Thread(new Runnable() {

                    @Override
                    public void run() {
                        lib = User32.INSTANCE;
                        HMODULE hMod = Kernel32.INSTANCE.GetModuleHandle(null);
                        keyboardHook = new LowLevelKeyboardProc() {
                            public LRESULT callback(int nCode, WPARAM wParam, KBDLLHOOKSTRUCT info) {
                                if (nCode >= 0) {
                                    switch (info.vkCode){
                                        case 0x5B:
                                        case 0x5C:
                                            return new LRESULT(1);
                                        default: //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);
                    }
                }).start();
            }
        }

        public static void unblockWindowsKey() {
            if (isWindows() && lib != null) {
                lib.UnhookWindowsHookEx(hhk);
            }
        }

        public static boolean isWindows(){
            String os = System.getProperty("os.name").toLowerCase();
            return (os.indexOf( "win" ) >= 0);
        }
    }



与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...