currently am working on some sort of dynamic loading library plugins development. The plugin will be loaded by other application. The programming language is C++.
I need to detect the mouse button condition on Unix such as MacOSX and Linux durning runtime. Say whether or not the left mouse button is currently holding or released at the application. The application itself doesn’t provide that feature to allow a library to do so.
In Windows I use mouse hook to capture that thing, but I couldn’t find an answer for Unix system.
You haven’t said what method the host application is using to interact with the computer’s GUI. Raw X11? A toolkit like GTK+ (used by Gnome applications among others) or Qt (used by KDE applications among others)? In the MacOS case, is the host application using the native GUI or X11? Actually, it’s not even clear if you can assume that the host application has any GUI at all!
If the host application is using a toolkit and you know which toolkit it is, then you should use whatever facility is provided by the toolkit to grab mouse events.
If you cannot be sure what the host application is doing for its GUI or even if it has one, then you can always bypass X11 and access the mouse directly, but there is a large caveat with this: how will you know if you are accessing the correct mouse? Be aware that host host application might be running with a remote X11 display, or that it might be a non-GUI application running on some remote machine over an SSH connection. Is the mouse connected to the computer that is running the application (if there even is one!) really the mouse you want?
If you still want to continue with this approach, under Linux you can open the virtual device
/dev/input/mice… Or open a specific mouse using/dev/input/mouse0or/dev/input/mouse1, etc… Reading from this device will give you data in the PS/2 format.