kwhat / jnativehook

Global keyboard and mouse listeners for Java.
Other
1.75k stars 347 forks source link

GlobalScreen class does not implement AutoCloseable, preventing usage in try-with-resources #457

Open Gauravkumar1502 opened 11 months ago

Gauravkumar1502 commented 11 months ago

Issue Description:

The GlobalScreen class from the JNativeHook library does not implement the AutoCloseable interface, making it inconvenient to use in try-with-resources statements.

Expected Behavior:

The GlobalScreen class should implement the AutoCloseable interface to allow for proper resource management using try-with-resources.

public class MyGlobalScreen implements AutoCloseable {
    // ... existing code ...

    /**
     * Close method to unregister the native hook.
     */
    @Override
    public void close() {
        try {
            GlobalScreen.unregisterNativeHook();
        } catch (NativeHookException e) {
            log.severe("Error unregistering native hook: " + e.getMessage());
        }
    }
}

Adding support for the AutoCloseable interface would enhance the usability of the library in applications that rely on try-with-resources statements for resource cleanup.