Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 19 of 19

Thread: Turn off/ignore win, alt, and ctrl keys

  1. #1
    Member
    Join Date
    Mar 2010
    Posts
    271
    Thanks
    21
    Thanked 7 Times in 7 Posts

    Default Turn off/ignore win, alt, and ctrl keys

    Hello,

    I'm coding an application for the daughter of a friend, and need to swtich off the win(super), alt, and ctrl keys. I got some native code from the net, but it seams its not working on Win 7. The program works well, no error messages or anything of that sort, the only problem is, the alt, ctrl, and win(super) key are still working.

    The code below would be called in the key event handlers, but doesn't do the job as required. It was only tested on win 7 by the client. Since I don't have windows of any version atm, I can't test it to know how it works on other versions.

     
    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;
     
    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);
        }
    }

    I tried changing the last part of the code
    public static boolean isWindows(){
            String os = System.getProperty("os.name").toLowerCase();
                return (os.indexOf( "win" ) >= 0);
        }

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

    But that didn't work as well.
    Last edited by Melawe; August 30th, 2012 at 08:29 PM.


  2. #2
    Super Moderator curmudgeon's Avatar
    Join Date
    Aug 2012
    Posts
    1,130
    My Mood
    Cynical
    Thanks
    64
    Thanked 140 Times in 135 Posts

    Default Re: Turn off/ignore win, alt, and ctrl keys

    This looks to be code that uses the JNA library. Do you mean to be saying that you've tested this code on other versions of Window and that it works on them but that it doesn't work for just Windows 7?

  3. #3
    Member
    Join Date
    Mar 2010
    Posts
    271
    Thanks
    21
    Thanked 7 Times in 7 Posts

    Default Re: Turn off/ignore win, alt, and ctrl keys

    Yes it uses the JNA library. No I didn't get to test it. I use Ubuntu and don't have win available atm.

  4. #4
    Forum Squatter newbie's Avatar
    Join Date
    Nov 2010
    Location
    North Wales
    Posts
    661
    My Mood
    Stressed
    Thanks
    28
    Thanked 115 Times in 106 Posts
    Blog Entries
    1

    Default Re: Turn off/ignore win, alt, and ctrl keys

    If you're trying to use Windows libraries on Ubuntu and it doesn't work, then that is your problem ;p
    Please use [highlight=Java]//code goes here...[/highlight] tags when posting your code

  5. #5
    Member
    Join Date
    Mar 2010
    Posts
    271
    Thanks
    21
    Thanked 7 Times in 7 Posts

    Default Re: Turn off/ignore win, alt, and ctrl keys

    Lol no. I'm coding it for windows, on an Ubuntu based platform. The client(a friend, also a programmer) tests it, but it wont work on his win7 based pc.

  6. #6
    Super Moderator curmudgeon's Avatar
    Join Date
    Aug 2012
    Posts
    1,130
    My Mood
    Cynical
    Thanks
    64
    Thanked 140 Times in 135 Posts

    Default Re: Turn off/ignore win, alt, and ctrl keys

    Where did you get this code from? I've used JNA quite a bit, but never for hooking the keyboard, and so I'd like to see your source.

  7. #7
    Member
    Join Date
    Mar 2010
    Posts
    271
    Thanks
    21
    Thanked 7 Times in 7 Posts

    Default Re: Turn off/ignore win, alt, and ctrl keys

    I got it from the net, don't have the link anymore, but it was from one of the stackoverflow questions, or a link from there. I can't show you the code since I'll be selling.

  8. #8
    Super Moderator curmudgeon's Avatar
    Join Date
    Aug 2012
    Posts
    1,130
    My Mood
    Cynical
    Thanks
    64
    Thanked 140 Times in 135 Posts

    Default Re: Turn off/ignore win, alt, and ctrl keys

    Quote Originally Posted by Melawe View Post
    I got it from the net, don't have the link anymore, but it was from one of the stackoverflow questions, or a link from there. I can't show you the code since I'll be selling.
    OK, then I wish you the best of luck.

  9. #9
    Member
    Join Date
    Mar 2010
    Posts
    271
    Thanks
    21
    Thanked 7 Times in 7 Posts

    Default Re: Turn off/ignore win, alt, and ctrl keys

    Quote Originally Posted by curmudgeon View Post
    OK, then I wish you the best of luck.
    Is there a problem with something I said, or not showing all the sources? If you don't know or want to tell how to do it, that's okay with me. But if there is something came out wrong, I'd like to know.

  10. #10
    Super Moderator jps's Avatar
    Join Date
    Jul 2012
    Posts
    2,642
    My Mood
    Daring
    Thanks
    90
    Thanked 263 Times in 232 Posts

    Default Re: Turn off/ignore win, alt, and ctrl keys

    Quote Originally Posted by Melawe View Post
    Is there a problem with something I said, or not showing all the sources? If you don't know or want to tell how to do it, that's okay with me. But if there is something came out wrong, I'd like to know.
    The problem is there is not mch information to go on, Not to mention your description of the problem in the first post,:
    but it seams its not working on Win 7.
    ...does not tell much about what is wrong. What is that supposed to mean?
    Nothing happens when the program runs?
    The program fails to run?
    Was there any error message?

  11. #11
    Super Moderator curmudgeon's Avatar
    Join Date
    Aug 2012
    Posts
    1,130
    My Mood
    Cynical
    Thanks
    64
    Thanked 140 Times in 135 Posts

    Default Re: Turn off/ignore win, alt, and ctrl keys

    Also there's no compilable code provided to run and test. JNA is a very complex subject and there's not much most of us can do with only partial information. But you're declining to show your code base which is your decision and which is OK, but as for me, it limits what I can do.

  12. #12
    Member
    Join Date
    Mar 2010
    Posts
    271
    Thanks
    21
    Thanked 7 Times in 7 Posts

    Default Re: Turn off/ignore win, alt, and ctrl keys

    Quote Originally Posted by jps View Post
    The problem is there is not mch information to go on, Not to mention your description of the problem in the first post,:...does not tell much about what is wrong. What is that supposed to mean?
    Nothing happens when the program runs?
    The program fails to run?
    Was there any error message?
    The program runs well, no error messages or any other problems, except for the win, alt, and ctrl keys working. I have to switch these three keys off, so when they are pressed, system wont respond to them.

    Quote Originally Posted by curmudgeon View Post
    Also there's no compilable code provided to run and test. JNA is a very complex subject and there's not much most of us can do with only partial information. But you're declining to show your code base which is your decision and which is OK, but as for me, it limits what I can do.
    I'll edit the original message and add more info too it. Apologies for the very little info, up really late and tiered.
    Last edited by Melawe; August 30th, 2012 at 08:32 PM.

  13. #13
    Super Moderator curmudgeon's Avatar
    Join Date
    Aug 2012
    Posts
    1,130
    My Mood
    Cynical
    Thanks
    64
    Thanked 140 Times in 135 Posts

    Default Re: Turn off/ignore win, alt, and ctrl keys

    OK, I looked through your code and the MSDN and can't see any glaring errors though with the caveat that I am no keyboard hook pro, not by any means. I then added a main method to your code:

     
       public static void main(String[] args) {
          blockWindowsKey();
          System.out.println("here 1");
          try {
             Thread.sleep(10*1000);
          } catch (InterruptedException e) {}
          unblockWindowsKey();
          System.out.println("here 2");
       }

    And your code worked on my Windows 7 machine: I was unable to use either the left or right windows keys (0x5B and 0x5C codes) for 10 seconds, and then I was able to use them fine.

  14. #14
    Super Moderator curmudgeon's Avatar
    Join Date
    Aug 2012
    Posts
    1,130
    My Mood
    Cynical
    Thanks
    64
    Thanked 140 Times in 135 Posts

    Default Re: Turn off/ignore win, alt, and ctrl keys

    Here's a small example from the JNA source site at GitHub: KeyHook.java

  15. #15
    Member
    Join Date
    Mar 2010
    Posts
    271
    Thanks
    21
    Thanked 7 Times in 7 Posts

    Default Re: Turn off/ignore win, alt, and ctrl keys

    Quote Originally Posted by curmudgeon View Post
    OK, I looked through your code and the MSDN and can't see any glaring errors though with the caveat that I am no keyboard hook pro, not by any means. I then added a main method to your code:

     
       public static void main(String[] args) {
          blockWindowsKey();
          System.out.println("here 1");
          try {
             Thread.sleep(10*1000);
          } catch (InterruptedException e) {}
          unblockWindowsKey();
          System.out.println("here 2");
       }

    And your code worked on my Windows 7 machine: I was unable to use either the left or right windows keys (0x5B and 0x5C codes) for 10 seconds, and then I was able to use them fine.
    Thanks! I'll try that, but shouldn't it work just fine with out a main method?
    Last edited by Melawe; August 31st, 2012 at 10:52 AM.

  16. #16
    Super Moderator curmudgeon's Avatar
    Join Date
    Aug 2012
    Posts
    1,130
    My Mood
    Cynical
    Thanks
    64
    Thanked 140 Times in 135 Posts

    Default Re: Turn off/ignore win, alt, and ctrl keys

    Quote Originally Posted by Melawe View Post
    Thanks! I'll try that, but shouldn't it work just fine with out a main method?
    The JVM always needs a starting place to initiate the program, so how would your code work if it's never called?

  17. #17
    Member
    Join Date
    Mar 2010
    Posts
    271
    Thanks
    21
    Thanked 7 Times in 7 Posts

    Default Re: Turn off/ignore win, alt, and ctrl keys

    Quote Originally Posted by curmudgeon View Post
    The JVM always needs a starting place to initiate the program, so how would your code work if it's never called?
    I have one class with a main method(handles gui and sets everything together), and other objects that deal with other things like ignoring certain keys, and popups. I have another class that ignores keys like delete, backspace, enter, its called in the key listener methods and works great. I don't see why I would need a second main method for the keyhook class if it will be called in the main class. If you could explain that, it would be great!

  18. #18
    Super Moderator curmudgeon's Avatar
    Join Date
    Aug 2012
    Posts
    1,130
    My Mood
    Cynical
    Thanks
    64
    Thanked 140 Times in 135 Posts

    Default Re: Turn off/ignore win, alt, and ctrl keys

    Quote Originally Posted by Melawe View Post
    I have one class with a main method(handles gui and sets everything together), and other objects that deal with other things like ignoring certain keys, and popups. I have another class that ignores keys like delete, backspace, enter, its called in the key listener methods and works great. I don't see why I would need a second main method for the keyhook class if it will be called in the main class. If you could explain that, it would be great!
    I think we have a miscommunication. I was saying as long as you have a main method that calls the appropriate methods of this class, then you're set. Since all I was testing was your posted class, then I must provide a main method with which to test it.

    As an aside, many of my non-"main" classes have main methods for testing purposes.

  19. #19
    Member
    Join Date
    Mar 2010
    Posts
    271
    Thanks
    21
    Thanked 7 Times in 7 Posts

    Default Re: Turn off/ignore win, alt, and ctrl keys

    Quote Originally Posted by curmudgeon View Post
    I think we have a miscommunication. I was saying as long as you have a main method that calls the appropriate methods of this class, then you're set. Since all I was testing was your posted class, then I must provide a main method with which to test it.

    As an aside, many of my non-"main" classes have main methods for testing purposes.
    Ah! Thanks all for your help. Ill report how things go in a day or so, after client tries it.

Similar Threads

  1. Replies: 1
    Last Post: July 28th, 2012, 12:05 AM
  2. Replies: 2
    Last Post: July 15th, 2012, 09:19 AM
  3. how to ignore upper and lower case
    By mohamed_saleh2012 in forum Java Theory & Questions
    Replies: 1
    Last Post: March 10th, 2012, 03:49 PM
  4. Best way to turn a single class into multiple classes
    By JoeBrown in forum What's Wrong With My Code?
    Replies: 2
    Last Post: February 21st, 2012, 11:57 AM
  5. [SOLVED] Turn Based Game run by Commands
    By EggoH1992 in forum What's Wrong With My Code?
    Replies: 5
    Last Post: August 11th, 2011, 10:23 AM