This site requires JavaScript, please enable it in your browser!
Greenfoot back
CooliMC
CooliMC wrote ...

2015/3/1

Call Dll in Java

CooliMC CooliMC

2015/3/1

#
Hey Guys Yesterday i've started a new project. I want to disable mouse and keyboard for 20 sec so I google a lot and find out that a need the user32.dll . Load it is not the problem but i dont know how to set a Boolean from it in Java or how to call a function from this dll in java ?
lordhershey lordhershey

2015/3/1

#
You will most likely have to look up JNI (Java Native Interface) , you typically cannot use this in an applet without doing some thing to the applet security policy, and most people would not let web applets directly access the machine.
CooliMC CooliMC

2015/3/2

#
No it's for a client to Client programmm but pls can you explain how to call a methode of the dll through JNI or how to implement it ?
lordhershey lordhershey

2015/3/2

#
I have not done JNI for a long time, but there are many example out there - what is the method you have to call? I will have to look around and try it out - it has been a long long time.
danpost danpost

2015/3/2

#
Could you not just add an abstract class to apply a delay timer to those mouse and keyboard actions? Something that starts like this:
public abstract class LimitedAction
{
    /** to hold a time value (methods are inactive while time is a future time) */
    private static long time;
    
    /**
     * sets an inactive state for the specified duration of time
     * @param millis the duration of inactive time in milliseconds */
     */
    public static setDelay(long millis)
    {
        time = System.currentTimeMillis()+millis; // sets time to a future time
    }
    
    /** ensures an active state (or cancels an inactive state) */
    public static setActive()
    {
        time = 0; // sets time to a past time
    }
    
    /** returns the active state */
    public static boolean isActive()
    {
         return time <= System.currentTimeMillis();
    }
Then follow that with the methods being delayed. For example, if delaying some Greenfoot methods (importing the appropriate classes), you could start with this:
public static String getKey()
{
    if (! isActive()) return null;
    return Greenfoot.getKey();
}

public static boolean isKeyDown(String key)
{
    if (! isActive()) return false;
    return Greenfoot.isKeyDown(key);
}

public static boolean mouseClicked(Object obj)
{
    if (! isActive()) return false;
    return Greenfoot.mouseClicked(obj);
}
Then use these methods instead of the ones that they add the delay to.
lordhershey lordhershey

2015/3/2

#
found this link http://electrofriends.com/articles/jni/part-2-jni-visual-studio-setup-dll-project/ You can get a free version of MSVC that will allow you to do this, and you are allowed to use it if you are a solo developer or a small team.
danpost danpost

2015/3/2

#
Corrections made:
public abstract class LimitedAction
{
    /** to hold a time value (methods are inactive while time is a future time) */
    private static long time;
     
    /**
     * sets an inactive state for the specified duration of time
     * @param millis the duration of inactive time in milliseconds
     */
    public static void setDelay(long millis)
    {
        time = System.currentTimeMillis()+millis; // sets time to a future time
    }
     
    /** ensures an active state (or cancels an inactive state) */
    public static void setActive()
    {
        time = 0; // sets time to a past time
    }
     
    /** returns the active state */
    public static boolean isActive()
    {
         return time <= System.currentTimeMillis();
    }
Tested and found to be okay.
CooliMC CooliMC

2015/3/16

#
thx to both but i need to delay the input for the whole system not only greenfoot so i need to call a varible from the dll but thx to both :D
You need to login to post a reply.