Dismiss Notice
Join Physics Forums Today!
The friendliest, high quality science and math community on the planet! Everyone who loves science is here!

Detecting Clicks (Java)

  1. Jul 6, 2006 #1

    0rthodontist

    User Avatar
    Science Advisor

    I would like to be able to detect a click anywhere on the screen, not associated with any particular screen component, and in fact inside another program's window. I don't know if I can use the listener framework to do that--if I can, I don't understand how. Can I get a whole screen object to which I can attach a listener?
     
  2. jcsd
  3. Jul 6, 2006 #2
    What OS are you using?
     
  4. Jul 6, 2006 #3

    0rthodontist

    User Avatar
    Science Advisor

    Windows XP
     
  5. Jul 6, 2006 #4

    chroot

    User Avatar
    Staff Emeritus
    Science Advisor
    Gold Member

    AFAIK, there is no way to detect clicks anywhere on the screen, outside your Java program's windows. The Windows event system won't even send your program events which occur outside its windows.

    - Warren
     
  6. Jul 6, 2006 #5

    0rthodontist

    User Avatar
    Science Advisor

    Some hunting turns up java.awt.Toolkit.addAWTEventListener(AWTEventListener listener, long eventMask) which says it operates globally. However, Toolkit is an abstract class and it doesn't have any subclasses listed. Do I have to extend it? It has what looks like 20 or 30 abstract methods. How could it possibly be used for anything if it has no listed subclasses? Not that I object to writing 20 blank method definitions, but it seems strange that what looks like a critical class is abstract and never subclassed in the Java API.
     
  7. Jul 6, 2006 #6

    chroot

    User Avatar
    Staff Emeritus
    Science Advisor
    Gold Member

    Use Toolkit.getDefaultToolkit() to get the system's Toolkit. You cannot create your own Toolkit, as its subclasses are private (and platform-dependent, I believe).

    I remain skeptical that you'll be able to see events which are not in your own programs' windows, but I have not done any experiments to the effect.

    - Warren
     
  8. Jul 6, 2006 #7

    0rthodontist

    User Avatar
    Science Advisor

    I don't know. Earlier I found that it IS possible when using .NET framework in Java or Visual Basic (there are some example programs). The procedure in that case is to repeatedly poll the state of the mouse buttons. This program however does not work:

    Code (Text):

    import java.applet.Applet;
    import java.awt.AWTEvent;
    import java.awt.Toolkit;
    import java.awt.event.AWTEventListener;

    public class MouseKnow  extends Applet implements AWTEventListener
    {
        static int clicknum = 0;
       
        public void init()
        {
            Toolkit t = Toolkit.getDefaultToolkit();
            MouseKnow m = new MouseKnow();
            t.addAWTEventListener(m, Long.MAX_VALUE);
        }
       
         public void eventDispatched(AWTEvent e) {
             System.out.println("click " + ++clicknum);
         }
    }
     
    This should register all events because of the 111...111 mask, and it does register many events, but it does not register external clicks. In fact it does not register any event, i.e. key presses, that occur when the applet window is de-focused, although it does register them inside it.


    Maybe there is a way in non-.NET Java to poll the mouse buttons?
     
    Last edited: Jul 6, 2006
  9. Jul 6, 2006 #8

    chroot

    User Avatar
    Staff Emeritus
    Science Advisor
    Gold Member

    Like I keep saying, there's no way to do this from within Java itself. Java is platform-independent, and does not include any ability to gather events outside of its own windows. Gathering such events would be different on Windows, Mac, and X.

    If you want to get events from outside your own windows, your Java program is no longer platform-independent, and is no longer going to be pure Java.

    - Warren
     
  10. Jul 6, 2006 #9

    0rthodontist

    User Avatar
    Science Advisor

    Well, it apparently can be done in .NET framework. I just don't have .NET.

    I don't care whether it's "pure" java or not. I just want to get the job done. In fact, I would not object to using an entirely different language, so long as it is not too complicated and the language is free for download.

    Any kludge would work. If I could somehow (using some plugin) get the pointer to change color briefly when I click, that would be an acceptable solution. If worst comes to worst I could use the highlight-pointer option in Windows that draws a converging circle when I press ctrl--hard to manage and not exactly a click, but close enough.
     
    Last edited: Jul 6, 2006
  11. Jul 6, 2006 #10

    chroot

    User Avatar
    Staff Emeritus
    Science Advisor
    Gold Member

    Well, if it's not pure Java, it will only run on one kind of operating system. If you use .NET, it will only be usable on Windows. I don't know whether or not this matters to you, but most people would not choose to develop in Java unless platform independence is important.

    - Warren
     
  12. Jul 6, 2006 #11

    0rthodontist

    User Avatar
    Science Advisor

    Platform independence doesn't matter to me. I'm only making this for myself.
     
  13. Jul 6, 2006 #12

    0rthodontist

    User Avatar
    Science Advisor

    I found something that looks promising for use within Java (though it does use native Windows code, not that that matters to me):
    http://www.lwjgl.org/download.php

    This has a Mouse class that has a static method that it says should directly detect the state of the mouse buttons. Unfortunately I can't get this stuff to work--I can't seem to configure my library path appropriately.

    After I extract their 1.0beta release, I followed the instructions they say at the command line to test it:

    java -cp .;res;jar\lwjgl.jar;jar\lwjgl_test.jar;jar\lwjgl_util.jar;jar\lwjgl_fmod3.jar;jar\lwjgl_devil.jar;jar\jinput.jar; -Djava.library.path=native org.lwjgl.test.WindowCreationTest

    It starts to run but then gives me the error
    Code (Text):

    Exception in thread "main" java.lang.UnsatisfiedLinkError: no lwjgl in java.libr
    ary.path
            at java.lang.ClassLoader.loadLibrary(Unknown Source)
            at java.lang.Runtime.loadLibrary0(Unknown Source)
            at java.lang.System.loadLibrary(Unknown Source)
            at org.lwjgl.Sys$1.run(Sys.java:62)
            at java.security.AccessController.doPrivileged(Native Method)
            at org.lwjgl.Sys.loadLibrary(Sys.java:60)
            at org.lwjgl.Sys.<clinit>(Sys.java:74)
            at org.lwjgl.opengl.Display.<clinit>(Display.java:103)
            at org.lwjgl.test.WindowCreationTest.initialize(WindowCreationTest.java:
    80)
            at org.lwjgl.test.WindowCreationTest.main(WindowCreationTest.java:284)
     
    Also when I try to use their Mouse class in my own class in Eclipse, it will recognize the project path and compile but it gives me a similar error during runtime. In Eclipse, I did "add external archives" to my project and selected the four jars in their jars directory, but this doesn't make it work. What is likely to be the problem?
     
    Last edited: Jul 6, 2006
  14. Jul 6, 2006 #13

    NoTime

    User Avatar
    Science Advisor
    Homework Helper

    I know you can do this under C or VB.
    Don't remember the exact code, but one of the functions in Kernal or User Dll will allow you to register a global event handler.

    Note: If your code has a bug then its instant blue screen when activated.
     
  15. Jul 7, 2006 #14

    0rthodontist

    User Avatar
    Science Advisor

    Regarding that error about java.library.path I'm getting when I try to test the lwjgl libraries, I wrote a program consisting of
    Code (Text):
    System.out.println(System.getProperty("java.library.path"));
    and ran it in my lwjgl directory. It turned up some stuff--directories containing .dll files that various programs I have apparently use--but sure enough the lwjgl\native\win32 directory which appears to contain the .dll files for lwjgl was not one of them. I tried to set the property on the command line using
    Code (Text):
    java -D java.library.path=native\win32
    and various variations on that theme, but no luck--every time I tried that it turned up a NoClassDefFoundError and spit back whatever I had put after the -D, and turning all periods into forward slashes for some reason. For example, in response to the previous attempt, it said
    Code (Text):
    Exception in thread "main" java.lang.NoClassDefFoundError: java/library/path=native\win32
    Also, a web site I read said that the -D command will only temporarily set properties, not permanently.

    So how do I get the java.library.path property to include the lwjgl\native\win32 directory?
     
    Last edited: Jul 7, 2006
Know someone interested in this topic? Share this thread via Reddit, Google+, Twitter, or Facebook

Have something to add?



Similar Discussions: Detecting Clicks (Java)
  1. Java Resources (Replies: 4)

  2. [Java] PNGException (Replies: 2)

  3. JAVA GridLayout (Replies: 1)

  4. Java interpreter (Replies: 8)

  5. Java Cylinders (Replies: 2)

Loading...