devshift / jnativehook

Automatically exported from code.google.com/p/jnativehook
0 stars 0 forks source link

Application freezes #19

Closed GoogleCodeExporter closed 9 years ago

GoogleCodeExporter commented 9 years ago
What version of the product are you using? On what operating system?
JNativeHook-1.1.0_rc3
Windows 7 x64
JDK 1.7

Please provide any additional information below.
I use modifier and Key in order to call my method like this:

if(e.getKeyCode() == NativeKeyEvent.VK_2 && e.getModifiers() == 1){
            loadIMG();
        }

But the problem persists when I call a method which calls for a Dialog windows 
like JFileChooser or JOPtionPane and etc.. What happens is once my method 
called PC freezes for a few secs , i cant use mouse and keyboard at that time. 
If I call my method thru a button and such everything is fine.

Original issue reported on code.google.com by TUHTA2...@gmail.com on 13 Aug 2012 at 9:51

GoogleCodeExporter commented 9 years ago
This should be fixed in the trunk as of r584 with the implementation of a java 
executor for native event dispatching.  Please build from the trunk and let me 
know if the problem persists.  You may also try using 
SwingUtilities.invokeLater() to work around the problem until the next 1.1 
build is ready.

Original comment by a...@1stleg.com on 13 Aug 2012 at 11:36

GoogleCodeExporter commented 9 years ago
Please test the attached copy.

Original comment by a...@1stleg.com on 14 Aug 2012 at 3:43

Attachments:

GoogleCodeExporter commented 9 years ago
[deleted comment]
GoogleCodeExporter commented 9 years ago
Seems like its better now it doesnt freeze, but another problem is that, when I 
actually do GlobalScreen.unregisterNativeHook(); and press the hotkeys and than 
I GlobalScreen.registerNativeHook(); again, and press once again the hotkeys, 
the Dialog windows pops up a lot of times. Here is the method I use:

if(e.getKeyCode() == NativeKeyEvent.VK_2 && e.getModifiers() == 1){
            if(enable_hotkeys_checkboxState){
                loadIMG();
            }
        }

And here is the method for unregisteringhooking and registerhooking that I use

/* Add Global key listener Method */
    private void addKeyListener(){
        try {
            GlobalScreen.registerNativeHook();
         }
         catch (NativeHookException ex) {
            System.err.println("There was a problem registering the native hook.");
            System.err.println(ex.getMessage());
            ex.printStackTrace();
         }

        //Construct the example object and initialze native hook.
        GlobalScreen.getInstance().addNativeKeyListener(this);
    }

    /* Remove Global key Listener Method */
    private void removeKeyListener(){
        GlobalScreen.unregisterNativeHook();
    }

The idea is I use checkbox for calling hooking and unhooking methods, maybe I 
use your libary wrong, However I followed the examples.

Original comment by TUHTA2...@gmail.com on 14 Aug 2012 at 8:26

GoogleCodeExporter commented 9 years ago
No donation page yet but a may put one up in a couple of days ;)  

Sounds like the queue is not getting drained after you stop the hook.  I am 
guessing that because the event delivery queue was blocked, events just queued 
up and then delivered after it was unblocked.  This is all a result of swing 
not supporting multiple threads and keyboard hooking requiring a native thread. 
 I will see if I can add a method of flushing the queue or try delivering 
events on the EDT to elevate problems like this.  Try running swing components 
(especially if they block) on the event dispatch thread. This would look a 
little something like this:

SwingUtilities.invokeLater(new Runnable() {
    public void run() {
        loadIMG();
    }
});

I will play around with the problem this week and see if I can figure out a 
better solution.  

P.S. e.getModifiers() == 1 can be written as e.getModifiers() == 
NativeKeyEvent.SHIFT_MASK

Original comment by a...@1stleg.com on 15 Aug 2012 at 3:08

GoogleCodeExporter commented 9 years ago
Ok this should be completely solved in the trunk.  I am currently out of town, 
but I will run a build to test when i get back at the end of this week.

Original comment by a...@1stleg.com on 20 Aug 2012 at 2:19

GoogleCodeExporter commented 9 years ago

Original comment by a...@1stleg.com on 20 Aug 2012 at 2:20

GoogleCodeExporter commented 9 years ago
Please test.

Original comment by a...@1stleg.com on 28 Aug 2012 at 6:54

Attachments:

GoogleCodeExporter commented 9 years ago
Very strange error , I just replace the Jars and Updates the Links in the 
Project thats it... and now it throws exception...

Exception in thread "main" java.lang.UnsatisfiedLinkError: 
C:\Users\Username\AppData\Local\Temp\JNativeHook788938508139073362dll: Can't 
find dependent libraries
    at java.lang.ClassLoader$NativeLibrary.load(Native Method)
    at java.lang.ClassLoader.loadLibrary0(Unknown Source)
    at java.lang.ClassLoader.loadLibrary(Unknown Source)
    at java.lang.Runtime.load0(Unknown Source)
    at java.lang.System.load(Unknown Source)
    at org.jnativehook.GlobalScreen.loadNativeLibrary(Unknown Source)
    at org.jnativehook.GlobalScreen.<init>(Unknown Source)
    at org.jnativehook.GlobalScreen.<clinit>(Unknown Source)
    at frmMain.addKeyListener(frmMain.java:702)
    at frmMain.enable_hotkeys_checkboxItemStateChanged(frmMain.java:260)
    at frmMain.access$4(frmMain.java:258)
    at frmMain$5.itemStateChanged(frmMain.java:460)
    at javax.swing.AbstractButton.fireItemStateChanged(Unknown Source)
    at javax.swing.AbstractButton$Handler.itemStateChanged(Unknown Source)
    at javax.swing.DefaultButtonModel.fireItemStateChanged(Unknown Source)
    at javax.swing.JToggleButton$ToggleButtonModel.setSelected(Unknown Source)
    at javax.swing.AbstractButton.setSelected(Unknown Source)
    at frmMain.<init>(frmMain.java:44)
    at start.main(start.java:6)

Original comment by TUHTA2...@gmail.com on 28 Aug 2012 at 9:10

GoogleCodeExporter commented 9 years ago
oops, I missed a period in the last patch.  I'll get that fixed.

Original comment by a...@1stleg.com on 28 Aug 2012 at 3:34

GoogleCodeExporter commented 9 years ago
Ok fixed the problems.  I botched an svn commit last night.  One day soon I 
will convert over my repositories.

Original comment by a...@1stleg.com on 28 Aug 2012 at 3:52

Attachments:

GoogleCodeExporter commented 9 years ago
Im very sorry but the problem still exists :(

It still opens the stuff many times... after I check/uncheck the check box :( 
here is the check box algo:

if(e.getStateChange() == 1){
            addKeyListener();
            enable_hotkeys_checkboxState = true;
        }
        else{
            removeKeyListener();
            enable_hotkeys_checkboxState = false;
        }

Here is the AddKeyListener method:

private void addKeyListener(){
        try {
            GlobalScreen.registerNativeHook();
         }
         catch (NativeHookException ex) {
            System.err.println("There was a problem registering the native hook.");
            System.err.println(ex.getMessage());
            ex.printStackTrace();
         }

        //Construct the example object and initialze native hook.
        GlobalScreen.getInstance().addNativeKeyListener(this);
    }

Here is the removeKeyListener method:

private void removeKeyListener(){
        GlobalScreen.unregisterNativeHook();
    }

I wish you could resolve the issue finally, I love your lib and hope you get it 
done :)

Original comment by TUHTA2...@gmail.com on 28 Aug 2012 at 8:19

GoogleCodeExporter commented 9 years ago
Hmm, I'm not sure what is causing the issue.  I will look into it tonight.  If 
you have time, can you attach a working example that illustrates the issue?  

Original comment by a...@1stleg.com on 28 Aug 2012 at 8:49

GoogleCodeExporter commented 9 years ago
No problem here is the example where you can see the error...

Please see the attachment!

Original comment by TUHTA2...@gmail.com on 28 Aug 2012 at 10:02

Attachments:

GoogleCodeExporter commented 9 years ago
I cannot duplicate the problem you are experiencing.  I am using the attached 
test code which registers and unregistered the native hook on button press.  
Events appear to stop being delivered immediately after unregisterNativeHook is 
called.  Please test the attached code and see if the problem continues for 
your machine.

I am leaning towards your issue being related to something in your code but I 
cant tell for sure because you didn't attach the source and I am unable to run 
the class files reliably.  If something needs to be changed in the attached 
test to duplicate the problem, please make the required changes and attached 
the modified file and outline what needs to be done to duplicate the problem.

Original comment by a...@1stleg.com on 29 Aug 2012 at 5:18

Attachments:

GoogleCodeExporter commented 9 years ago
Please see the attachment

I attached the SRC of the app that causes the error

Original comment by TUHTA2...@gmail.com on 29 Aug 2012 at 7:03

Attachments:

GoogleCodeExporter commented 9 years ago
The short answer is:  Its your code, wrap call_scan(), loadIMG(), your logging 
(System.out.println) and anything else that should be executed on the EDT with 
SwingUtilities.invokeLater.  If its not going to be executed on the EDT but you 
don't want to block the NDT, start another thread.

Ok well I wrote up a nice long explanation to your problem and magically lost 
it due to a browser navigation error... so you are going to get the abridge 
version.

Definitions:
EDT  Swing's Event Dispatch Thread.
NDT  JNativeHook's  Event Dispatch Thread.

Your problem is due to mixing threads with Swing.  Your calling said function 
call_scan from outside of the EDT, in this case its on JNativeHook's dispatch 
thread.  So in the case of call_scan you then call a Swing function, 
showMessageDialog from the NDT resulting in unsafe thread behavior.  This is 
further compounded by redirectOut taking information from stdout running on the 
NDT and redirecting it to a text area running on the EDT.  The events are 
getting hung up in the NDT because its blocked, but they get pushed through 
right after the dialog closes and before GlobalScreen.unregisterNativeHook() is 
called.  You need to wrap things that are going to run on the EDT with 
SwingUtilities.invokeLater to make it place nicely with Swing.  There is a lot 
of information out there to help you do this.  For Example: 
http://java.sun.com/products/jfc/tsc/articles/threads/threads1.html

I hope this helps.

Original comment by a...@1stleg.com on 30 Aug 2012 at 5:24

GoogleCodeExporter commented 9 years ago
how to create native lib for windows like (JNativeHook.dll) file.

Original comment by rathour....@gmail.com on 25 Nov 2013 at 2:08