Jump to content

Sky Slate Blueberry Blackcurrant Watermelon Strawberry Orange Banana Apple Emerald Chocolate
Photo

Using autohotkey.dll with java


  • Please log in to reply
7 replies to this topic
Eildosa
  • Members
  • 19 posts
  • Last active: Apr 28 2014 08:31 AM
  • Joined: 18 Aug 2013

Hello,

I finnaly figured out how to use this with java. this tutorial has been made with eclipse 4.3.1 and the jdk 1.7x64.

 

go on this page http://www.autohotke...-autohotkeydll/ and click on
"* AutoHotkey_H is HotkeyIt's custom build (most uptodate)." to download the last package.

unrar the package, we are only interested by autohotkey.dll present in the folder 'x64w'
you do not need the other files and autohotkey does not need to be installed.

Create a new java project, you get this :

 

img1.png

 

create a new package in the src folder and a new java class in the previously created package
 

img2.png

 

Download the JNA library : https://github.com/twall/jna
direct download link : https://maven.java.n...0/jna-4.1.0.jar

right clic on the project>new>folder name it "libs", drag and drop jna inside
rignt clic on the project>new>folder name it "dlls", drag and drop autohotkey.dll inside

 

now go to the build path windows :

 

img3_build_path.png

rignt clic on the project>properties>java Build Path clic on "Add JARs..."
select jna-4.1.0.jar present in the folder "libs" and press "OK"

Clic on "Add Class Folder" (in the java build path windows in case you already closed it)
select/check the "libs" folder, press "OK"

then close the java build path windows by pressing "OK"

 

Copy this piece of code inside DllController.java

package fr.mssb.autohotkey;

import com.sun.jna.Library;
import com.sun.jna.Native;

public class DllController {

	public interface autoHotKeyDll extends Library {
		public void ahkExec(char[] s);
		public void ahktextdll(char[] s);
	}

	public static void main(String args[]) throws InterruptedException {

		System.out.println("running in " + System.getProperty("sun.arch.data.model"));

		System.out.println("Loading dll");
		autoHotKeyDll lib = (autoHotKeyDll) Native.loadLibrary("AutoHotkey", autoHotKeyDll.class);

		System.out.println("initialisation");
		lib.ahktextdll("#Persistent".toCharArray());
		Thread.sleep(100);

		System.out.println("displaying cbBox");
		lib.ahkExec("msgbox,,Hellow!,Hellow World!".toCharArray());
	}
}

And you should get this :

 

img4.png

 

Enjoy



quartxz
  • Members
  • 11 posts
  • Last active: Feb 08 2015 03:05 PM
  • Joined: 27 Dec 2013

How to get variable in autohotkey to java? example get mouse click position. Pass x,y to java

Thanks



philu
  • Members
  • 15 posts
  • Last active: Jan 21 2016 06:34 PM
  • Joined: 28 Aug 2012

awesome, thank you a lot!!



alvitawa
  • Members
  • 98 posts
  • Last active: Oct 14 2017 02:16 PM
  • Joined: 07 Feb 2013

Did everything and works great, untill I want to do other more complicate things:

 

While attempting to use "ahkdll" and "addFile" I continuosly got errors that the file could not be loaded, that looked like this:

 

2kA4d.png

 

And some other weird errors with a lot of chinese(or japanese?) characters. I fugured out this must be because a char array (char[]) is being used, and the dll expects an LPTSTR(Large Pointer To STRing), wich is essentially an array of wide characters. Java does not support this but fortunately jna does: WString.

 

That fixes the chinese characters, and now the file is added "succesfully". Runs OK, ahkExec works but the file is not in fact loaded, anything I put in it is ingored, if I initialize the thread with ahkdll(file) the thread terminates instantly(presumably because there is no code to execute) and any ahkExec call after that doesn't work. I haven't been able to figure out jet how to get a return value(or even call) because of this^

 

Below code:

 

Main.java

package com.company;

import com.sun.jna.*;

public class Main {


    public interface autoHotKeyDll extends Library {
        public void ahkExec(WString s);
        public void ahkdll(WString s);
        public void addFile(WString s, int a, int b);
        public void ahktextdll(WString s);
        public Pointer ahkFunction(WString s);
    }

    public static void main(String args[]) throws InterruptedException {

        System.out.println("running in " + System.getProperty("sun.arch.data.model"));

        System.out.println("Loading dll");
        autoHotKeyDll lib = (autoHotKeyDll) Native.loadLibrary("AutoHotkey", autoHotKeyDll.class);


        System.out.println("initialisation");
        lib.ahktextdll(new WString(""));
        lib.addFile(new WString(System.getProperty("user.dir") + "\\lib.ahk"), 1, 0);
        Thread.sleep(1000);

        System.out.println("function call");
        System.out.println("return:" + lib.ahkFunction(new WString("function")));
        Thread.sleep(1000);

        System.out.println("displaying cbBox");
        lib.ahkExec(new WString("msgbox,,Hellow!,Hellow World!"));
        Thread.sleep(1000);
    }
}

lib.ahk

#persistent
MsgBox, hey

function(){
hey func
return 55
}


Any ideas?



HotKeyIt
  • Moderators
  • 7439 posts
  • Last active: Jun 22 2016 09:14 PM
  • Joined: 18 Jun 2008

Did you use latest version?

Try:

    public interface autoHotKeyDll extends Library {
        public void ahkExec(WString s);
        public void ahkdll(WString s,WString o,WString p);
        public void addFile(WString s, int a, int b);
        public void ahktextdll(WString s,WString o,WString p);
        public Pointer ahkFunction(WString f,WString p1,WString p2,WString p3,WString p4,WString p5,WString p6,WString p7,WString p8,WString p9,WString p10);
    }


alvitawa
  • Members
  • 98 posts
  • Last active: Oct 14 2017 02:16 PM
  • Joined: 07 Feb 2013

Re-downloaded AutoHotkey.dll, no succes with the suggested code :(

 

As a side note, changing the lib.ahk path to a non-existent file(lib1.ahk) does give an error message that the file could not be loaded. Also tried AutoHotkeyMini and Multi-Threaded dll's.



HotKeyIt
  • Moderators
  • 7439 posts
  • Last active: Jun 22 2016 09:14 PM
  • Joined: 18 Jun 2008

This works for me:

package autohotkey;

import com.sun.jna.*;

public class Main {

    public interface autoHotKeyDll extends Library {
        public void ahkExec(WString s);
        public void ahkdll(WString s,WString o,WString p);
        public void addFile(WString s, int a);
        public void ahktextdll(WString s,WString o,WString p);
        public Pointer ahkFunction(WString f,WString p1,WString p2,WString p3,WString p4,WString p5,WString p6,WString p7,WString p8,WString p9,WString p10);
    }

    public static void main(String args[]) throws InterruptedException {
    	Pointer pointer;
        System.out.println("running in " + System.getProperty("sun.arch.data.model"));

        System.out.println("Loading dll");
        autoHotKeyDll lib = (autoHotKeyDll) Native.loadLibrary("AutoHotkey.dll", autoHotKeyDll.class);


        System.out.println("initialisation");
        lib.ahktextdll(new WString(""),new WString(""),new WString(""));
        Thread.sleep(100);
        lib.addFile(new WString(System.getProperty("user.dir") + "\\lib.ahk"), 1);
        Thread.sleep(1000);

        System.out.println("function call");
        System.out.println("return:" + lib.ahkFunction(new WString("function"),new WString(""),new WString(""),new WString(""),new WString(""),new WString(""),new WString(""),new WString(""),new WString(""),new WString(""),new WString("")).getString(0));
        Thread.sleep(1000);

        System.out.println("displaying cbBox");
        lib.ahkExec(new WString("msgbox,,Hellow!,Hellow World!"));
        Thread.sleep(1000);
    }
}


Nicolasgomezk
  • Members
  • 10 posts
  • Last active: Aug 05 2015 06:01 PM
  • Joined: 02 Aug 2015

Nice thanks bro!