View Javadoc

1   /*
2    * @(#)$Id: MouseUtils.java 689 2009-07-22 00:10:27Z bsigner $
3    *
4    * Author       :   Croci Michele, mcroci@gmail.com
5    *
6    * Purpose      :  Provide access to API for managing a mouse
7    *
8    * -----------------------------------------------------------------------
9    *
10   * Revision Information:
11   *
12   * Date             Who         Reason
13   *
14   * Nov 14, 2007    crocimi     Initial Release
15   *
16   * -----------------------------------------------------------------------
17   *
18   * Copyright 1999-2009 ETH Zurich. All Rights Reserved.
19   *
20   * This software is the proprietary information of ETH Zurich.
21   * Use is subject to license terms.
22   * 
23   */
24  
25  
26  package org.ximtec.igesture.io.mouse;
27  
28  import java.util.EnumSet;
29  
30  import org.ximtec.igesture.io.win32.User32;
31  import org.ximtec.igesture.io.win32.Win32;
32  import org.ximtec.igesture.io.win32.User32.MSG;
33  import org.ximtec.igesture.io.win32.User32.POINT;
34  import org.ximtec.igesture.io.win32.W32API.HANDLE;
35  import org.ximtec.igesture.io.win32.W32API.HINSTANCE;
36  import org.ximtec.igesture.io.win32.W32API.LPARAM;
37  import org.ximtec.igesture.io.win32.W32API.LRESULT;
38  import org.ximtec.igesture.io.win32.W32API.WPARAM;
39  
40  import com.sun.jna.win32.StdCallLibrary.StdCallCallback;
41  
42  
43  /**
44   * 
45   * @author Michele Croci, mcroci@gmail.com
46   * @author Ueli Kurmann, ueli.kurmann@bbv.ch
47   * @version 1.0
48   */
49  public class MouseUtils implements Runnable {
50  
51     private static final User32 USER32 = Win32.USER32_INSTANCE;
52     
53     private HINSTANCE hinst;
54     private MouseProc mouseListener;
55     private HANDLE handle;
56  
57     private final MouseHandler mouseCallback;
58  
59     public enum MouseButton {
60        LEFT(0x01), RIGHT(0X02), MIDDLE(0X04);
61  
62        private int buttonId;
63  
64  
65        private MouseButton(int i) {
66           this.buttonId = i;
67        }
68  
69  
70        public int getButtonId() {
71           return buttonId;
72        }
73     }
74  
75  
76     public MouseUtils(MouseHandler mouseCallback) {
77        this.mouseCallback = mouseCallback;     
78     }
79  
80  
81     /**
82      * Initialization
83      */
84     public void init() {
85        hinst = Win32.KERNEL32_INSTANCE.GetModuleHandle(null);
86        mouseListener = new MouseProc() {
87  
88           public LRESULT callback(int code, WPARAM wParam, LPARAM lParam) {
89  
90              POINT p = new POINT();
91              USER32.GetCursorPos(p);
92  
93              EnumSet<MouseButton> buttons = EnumSet.noneOf(MouseButton.class);
94              for (MouseButton button : MouseButton.values()) {
95                 if (USER32.GetAsyncKeyState(button.buttonId)) {
96                    buttons.add(button);
97                 }
98              }
99              
100             mouseCallback.handleMouseEvent(p.x, p.y, buttons);
101 
102             return USER32.CallNextHookEx(handle, code, wParam, lParam);
103          }
104       };
105       handle = USER32.SetWindowsHookExW(User32.WH_MOUSE_LL, mouseListener,
106             hinst, 0);
107    }
108 
109 
110   
111    /**
112     * Finalize thread
113     */
114    public interface MouseProc extends StdCallCallback {
115 
116       LRESULT callback(int code, WPARAM wParam, LPARAM lParam);
117    }
118 
119    /*
120     * (non-Javadoc)
121     * @see java.lang.Runnable#run()
122     */
123    @Override
124    public void run() {
125       init();
126       registerHook();
127       MSG msg = new MSG();
128       while ((USER32.GetMessageW(msg, null, 0, 0) != 0)) {
129          
130       }
131    }
132 
133    /**
134     * Register callback function
135     */
136    public void registerHook() {
137       handle = USER32.SetWindowsHookExW(User32.WH_KEYBOARD_LL, mouseListener,
138             hinst, 0);
139    }
140 
141 
142    /**
143     * Unregister callback function
144     */
145    public void unregisterHook() {
146       USER32.UnhookWindowsHookExW(handle);
147    }
148    
149    /**
150     * Finalize thread
151     */
152    protected void finalize() throws Throwable {
153       unregisterHook();
154       super.finalize();
155    }
156 
157 
158 }