As a developer I want to Create a console application which create a global Windows OS mouse hook, to collect the following information on every click done across any application running and print the following info on console:
- Mouse coordinates in pixel
- Exe of the application running behind the cursor
- Get the focused element type on the running behind application.
This application captures the any mouse click events from any windows application and shows below output
explorer.exe: {X=1108, Y=1063}:MSTaskSwWClass
notepad++.exe: {X=592, Y=-1045}:Notepad++
notepad++.exe: {X=635, Y=-840}:Menu Item
notepad++.exe: {X=959, Y=-366}:Button
Key Pressed: 91
EXCEL.EXE: {X=651, Y=-741}:EDTBX
The code has been made expandable and also added provision for more events like keyboard events with example.
When designing a utility to capture mouse and keyboard events from the Windows operating system, desided to consider using the Observer design pattern.
The Observer pattern is a behavioral design pattern where an object, known as the subject, maintains a list of its dependents, called observers, that are notified of state changes.
Here's how applied the Observer pattern in this context:
- Subject (Event Broadcaster): This could be a class responsible for capturing mouse and keyboard events. It maintains a list of registered observers and notifies them when events occur. There can be multiple observers for one bradcaster. There are two Event broadcaster classes are implemented and can be exteded for multiple classes
- KeyboardEventBroadcaster
- MouseEventBroadcaster
- Observer (Event Listeners): These are classes that are interested in receiving notifications about mouse and keyboard events. They register themselves with the subject and get notifications about events. There are two Event Listeners classes are implemented and can be exteded for multiple classes
- KeyboardEventLogger
- MouseEventLogger
Observer classes names given as logger in this application context, here responsibility of class is to listen the event and log events on to the console.