• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 803
  • Last Modified:

How to implement following class in java project (Eclipse)?

I am trying to use following class in new project with standard Java libraries.  User32 is not recognized.

public class CWMouseHook {

      final User32 USER32INST;
    private static LowLevelMouseProc mouseHook;

    public CWMouseHook() {
        if(!Platform.isWindows()) {
            throw new UnsupportedOperationException("Not supported on this platform.");
        }
        USER32INST = User32.INSTANCE;
        mouseHook = hookTheMouse();
        Native.setProtected(true);
    }
 
    private HHOOK hhk;
    private boolean isHooked = false;
 
    public static final int WM_LBUTTONDOWN = 513;
    public static final int WM_LBUTTONUP = 514;
    public static final int WM_RBUTTONDOWN = 516;
    public static final int WM_RBUTTONUP = 517;
    public static final int WM_MBUTTONDOWN = 519;
    public static final int WM_MBUTTONUP = 520;
 
    public void unsetMouseHook() {
        USER32INST.UnhookWindowsHookEx(hhk);
        isHooked = false;
        System.out.println("Mouse hook is unset.");
    }
 
    public boolean isIsHooked() {
        return isHooked;
    }
 
    public void setMouseHook() {
        Thread mousehook_thread = new Thread(new Runnable() {
            @Override
            public void run()  {
                try {
                    if(!isHooked) {
                        hhk = USER32INST.SetWindowsHookEx(
                                User32.WH_MOUSE_LL,
                                mouseHook,
                                Kernel32.INSTANCE.GetModuleHandle(null),
                                0);
 
                        isHooked = true;
 
                        System.out.println("Mouse hook is set. Click anywhere.");
                        // message dispatch loop (message pump)
                        MSG msg = new MSG();
                        while ((USER32INST.GetMessage(msg, null, 0, 0)) != 0) {
                            USER32INST.TranslateMessage(msg);
                            USER32INST.DispatchMessage(msg);
                            if (!isHooked)
                                break;
                        }
                    } else
                        System.out.println("The Hook is already installed.");
                } catch (Exception e) { System.err.println("Caught exception in MouseHook!"); }
            }
        });
        mousehook_thread.start();
    }
 
    private interface LowLevelMouseProc extends HOOKPROC {
        LRESULT callback(int nCode, WPARAM wParam, MOUSEHOOKSTRUCT lParam);
    }
 
    private LowLevelMouseProc hookTheMouse() {
        return new LowLevelMouseProc() {
            @Override
            public LRESULT callback(int nCode, WPARAM wParam, MOUSEHOOKSTRUCT info) {
                if (nCode >= 0) {
                    switch(wParam.intValue()) {
                        case CWMouseHook.WM_LBUTTONDOWN:
                            System.out.println("L ["+info.pt.x+","+info.pt.y+"]");
                            break;
                        case CWMouseHook.WM_RBUTTONDOWN:
                            System.out.println("R ["+info.pt.x+","+info.pt.y+"]");
                            break;
                        case CWMouseHook.WM_MBUTTONDOWN:
                            System.out.println("M ["+info.pt.x+","+info.pt.y+"]");
                            break;
                        default:
                            break;
                    }
                }
                return USER32INST.CallNextHookEx(hhk, nCode, wParam, info.getPointer());
            }
        };
    }
 
    public class Point extends Structure {
        public class ByReference extends Point implements Structure.ByReference {};
        public NativeLong x;
        public NativeLong y;
    }
 
    public static class MOUSEHOOKSTRUCT extends Structure {
        public static class ByReference extends MOUSEHOOKSTRUCT implements Structure.ByReference {};
 
        public POINT pt;
        public HWND hwnd;
        public int wHitTestCode;
        public ULONG_PTR dwExtraInfo;
    }
}
0
Ashok
Asked:
Ashok
  • 4
  • 4
2 Solutions
 
for_yanCommented:
I don't think this is the standard java library - you probably want to use some
microsft package - you should treat it as external library and find ocrresponding jar
(hope the links below could help):

http://jcs.mobile-utopia.com/jcs/55085_User32.java

http://javadevelopersdiary.blogspot.com/2008/03/call-findwindow-method-of-user32dll.html
0
 
CEHJCommented:
Looks like you need JNA for that

http://jna.java.net
0
 
AshokAuthor Commented:
CEHJ,

You are right.  I tried to find the link for it and got some wrong link with VIRUS ATTACK.
The link you provided is safe.
I will try this.

Thanks
0
Cloud Class® Course: Ruby Fundamentals

This course will introduce you to Ruby, as well as teach you about classes, methods, variables, data structures, loops, enumerable methods, and finishing touches.

 
AshokAuthor Commented:
CEJH,

I cannot (figure out or) remove all errors after adding both JAR files in the project and trying to add necessary imports.

Thanks
0
 
CEHJCommented:
a. what ide?
b. which jars?
c.  what are the urls of the jar downloads?
0
 
AshokAuthor Commented:
for_yan,

How do I download "com.ms.win32"?

Thanks
0
 
AshokAuthor Commented:
a) Eclipse IDE
b) jna & platform
c) http://jna.java.net
0
 
CEHJCommented:
You're missing additional sources. Something like the following:

http://stackoverflow.com/questions/3078646/jna-keyboard-hook-in-windows
0
 
CEHJCommented:
:)
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Upgrade your Question Security!

Your question, your audience. Choose who sees your identity—and your question—with question security.

  • 4
  • 4
Tackle projects and never again get stuck behind a technical roadblock.
Join Now