Is there an example C# program that uses the AutoHotkey.dll? I'm trying to figure out how to use some of the AutoHotkey features in my app but can't figure it out.
Also, will the dll be statically linked into my compiled build or will the end user need to install AutoHotkey?
Thanks!
AutoHotkey.dll and C#
Re: AutoHotkey.dll and C#
Have a look here.
End user will need AutoHotkey.dll, or you use something like MemoryModule (see source code) to load the dll from memory/resources.
End user will need AutoHotkey.dll, or you use something like MemoryModule (see source code) to load the dll from memory/resources.
Re: AutoHotkey.dll and C#
OK, thanks.
-
- Posts: 1
- Joined: 14 Oct 2014, 15:13
Re: AutoHotkey.dll and C#
I needed to use this on a project I was working on. I did a pretty nice wrapper around it and posted it up on github.
https://github.com/amazing-andrew/AutoHotkey.Interop
https://github.com/amazing-andrew/AutoHotkey.Interop
Re: AutoHotkey.dll and C#
well done. this should be linked somewhereamazingandrew wrote:I needed to use this on a project I was working on. I did a pretty nice wrapper around it and posted it up on github.
https://github.com/amazing-andrew/AutoHotkey.Interop
Re: AutoHotkey.dll and C#
Thank you for sharing this and for making it open source.amazingandrew wrote:I needed to use this on a project I was working on. I did a pretty nice wrapper around it and posted it up on github.
https://github.com/amazing-andrew/AutoHotkey.Interop
I gave it a star in GitHub and I encourage others to do so too.
Re: AutoHotkey.dll and C#
As some of you may be aware, I have been beavering away learning how to implement all kinds of input detection in C# - I have managed to wrap DirectInput, XInput and RawInput into C# DLLs that can be accessed from AHK
The idea is to provide one API that can be loaded from any language that supports CLR (Like AHK) and it provides the ability to subscribe to inputs (eg Joysticks, mice, keyboards, VR controllers, whatever underlying API you want) and drive outputs (Keyboard, mouse, vJoy etc) - all via one standardized set of API calls.
So my next Proof of Concept is to make a C# DLL that loads AutoHotkey.dll and uses it to declare hotkeys and use AHK's Send command etc.
So the flow would be this:
AHK script loads the C# DLL via CLR and passes it a function object (The callback you wish fired when the hotkey is pressed) and some options (eg the hotkey to be bound).
The C# DLL uses AutoHotkey.dll to create the requested hotkey.
Now at this point, I would rather that the C# code was the first thing called when the hotkey fired, so that things like repeat suppression (An option I add to hotkeys to stop the down event repeating when you hold the key) can be done in C#
But whatever, end result should be that when the hotkey is pressed, the autohotkey.dll code fires and ultimately the callback function in the main AHK script gets the event.
Yes, it's a little perverse, ultimately someone using this library for an AHK script would be loading C# from AHK, which would then be spinning up AutoHotkey.dll to provide support for hotkeys, but I do not want to limit this to just working with AHK.
So...
At one point I got a message from someone on one of these projects ?amazingandrew? asking if I wanted to help, but I was too busy with my own stuff.
I tried messing around with this library a while back, but got no joy. Not sure if I was being a doofus or what.
If anyone else is active tinkering in this area, would appreciate any pointers as to how best to proceed.
Gonna give working with this library a try probably this weekend, but I thought it worth at least sending a shout out to see if there was any activity in this area.
The idea is to provide one API that can be loaded from any language that supports CLR (Like AHK) and it provides the ability to subscribe to inputs (eg Joysticks, mice, keyboards, VR controllers, whatever underlying API you want) and drive outputs (Keyboard, mouse, vJoy etc) - all via one standardized set of API calls.
So my next Proof of Concept is to make a C# DLL that loads AutoHotkey.dll and uses it to declare hotkeys and use AHK's Send command etc.
So the flow would be this:
AHK script loads the C# DLL via CLR and passes it a function object (The callback you wish fired when the hotkey is pressed) and some options (eg the hotkey to be bound).
The C# DLL uses AutoHotkey.dll to create the requested hotkey.
Now at this point, I would rather that the C# code was the first thing called when the hotkey fired, so that things like repeat suppression (An option I add to hotkeys to stop the down event repeating when you hold the key) can be done in C#
But whatever, end result should be that when the hotkey is pressed, the autohotkey.dll code fires and ultimately the callback function in the main AHK script gets the event.
Yes, it's a little perverse, ultimately someone using this library for an AHK script would be loading C# from AHK, which would then be spinning up AutoHotkey.dll to provide support for hotkeys, but I do not want to limit this to just working with AHK.
So...
At one point I got a message from someone on one of these projects ?amazingandrew? asking if I wanted to help, but I was too busy with my own stuff.
I tried messing around with this library a while back, but got no joy. Not sure if I was being a doofus or what.
If anyone else is active tinkering in this area, would appreciate any pointers as to how best to proceed.
Gonna give working with this library a try probably this weekend, but I thought it worth at least sending a shout out to see if there was any activity in this area.
Re: AutoHotkey.dll and C#
Well I can create hotkeys, but it is extremely unreliable.
https://github.com/amazing-andrew/AutoH ... p/issues/9
Does anyone have any idea what I am doing wrong? This seems utterly unusable.
Edit: It seems that SendPipeMessage is not reliable when used in a hotkey function
https://github.com/amazing-andrew/AutoH ... p/issues/9
Does anyone have any idea what I am doing wrong? This seems utterly unusable.
Edit: It seems that SendPipeMessage is not reliable when used in a hotkey function
Re: AutoHotkey.dll and C#
Very interesting!
I had some starting problems, but now I was able to include AHK inside of a C# script.
It is basically some external GUI software, where I assigned a C# script to a button, which uses the AHK library.
The good news: It works!
The bad news: If I click 2 times on a button (I'm not speaking about a double-click),
I always receive the error message: "Error. Duplicate function definition", but the function still gets executed properly (after clicking on OK).
This is the AHK script, which contains the function that I call:
And this is the C# script which is used in an external software:
I assign the numbers (nummer) to buttons directly in the GUI software.
What's the reason for this error?
I've never seen it before anyway.
Cheers!
I had some starting problems, but now I was able to include AHK inside of a C# script.
It is basically some external GUI software, where I assigned a C# script to a button, which uses the AHK library.
The good news: It works!
The bad news: If I click 2 times on a button (I'm not speaking about a double-click),
I always receive the error message: "Error. Duplicate function definition", but the function still gets executed properly (after clicking on OK).
This is the AHK script, which contains the function that I call:
Code: Select all
#Warn All, Off ; Doesn't seem to affect the dialog box
MyFunction(nummer)
{
if nummer = 1
{
SendLevel, 2
Send {F1}
}
if nummer = 2
{
SendLevel, 2
Send {F2}
}
}
Code: Select all
using AutoHotkey.Interop;
public class AutoHotKeyTest
{
public Object button; // Zugriff auf einen Button
public string nummer; // Button kann eigene Nummer ausführen
public void Ahk_Test()
{
var ahk = AutoHotkeyEngine.Instance; // Grab a copy of the AutoHotkey singleton instance
ahk.LoadFile("functions.ahk"); // Load a library or exec scripts in a file
ahk.ExecFunction("MyFunction", nummer); // Execute a specific function (found in functions.ahk), with 1 parameter
}
}
What's the reason for this error?
I've never seen it before anyway.
Cheers!
Re: AutoHotkey.dll and C#
The problem is that you are loading the same file (functions.ahk) again while the script is still running.
Best is to load your script when creating the instance and only call the functions afterwards.
Best is to load your script when creating the instance and only call the functions afterwards.
Re: AutoHotkey.dll and C#
Thanks for your answer.
Well, it sounds logical, but I must honestly admit I don't know how to write it.
Can you give me an example, please?
Well, it sounds logical, but I must honestly admit I don't know how to write it.
Can you give me an example, please?
Re: AutoHotkey.dll and C#
Scr1pter, I would respectfully echo the request for an example, as I'm running into exactly the same trouble with C# macros in G1ANT. If AutoHotkey.Interop had a call for ahkReady(), I think this would be easier in general.
HOWEVER: You might try this (just added 'ahkReset();' as the last call):
I don't think that 'ahk.Reset()' command is documented on the AutoHotkey.Interop page, but I did find it in the source code. One problem may be solved, but another may crop up. Consider this code, which is not using AutoHotkey.Interop, but AutoHotkey.dll via its COM interface:
What happens SOMETIMES with C# (and I bet always if you use the COM interface) is that your code will need this ahkReady() call to keep your script from terminating before AHK gets a chance to run.
I'm probably rambling, but take a look at this code that works either with or without the ahk.Reset(). It's scripted and not compiled, taken from a G1ANT script, you'll see sort of what's happening with dlls and namespaces, but that's not really important here. Everything between '⊂⊃' is C#. If you put the delimiters on separate lines it's like an AH continuation section, I suppose). It's more or less one of the examples from the AutoHotkey.Interop github page. But then look at this: Without the System.Windows.Forms.MessageBox() call, which stops the thread, the whole C# script bails before the AHK runs. Take it out and see.
@HotKeyIt, could you please suggest a way to provide the same functionality as but using just AutoHotkey.Interop?
(I'm going to try to add a wrapped call to ahkReady() in a recompiled AutoHotkey.Interop, but frankly, I don't like my chances.)
A million thanks in advance,
burque505
HOWEVER: You might try this (just added 'ahkReset();' as the last call):
Code: Select all
using AutoHotkey.Interop;
public class AutoHotKeyTest
{
public Object button; // Zugriff auf einen Button
public string nummer; // Button kann eigene Nummer ausführen
public void Ahk_Test()
{
var ahk = AutoHotkeyEngine.Instance; // Grab a copy of the AutoHotkey singleton instance
ahk.LoadFile("functions.ahk"); // Load a library or exec scripts in a file
ahk.ExecFunction("MyFunction", nummer); // Execute a specific function (found in functions.ahk), with 1 parameter
ahk.Reset();
}
}
Code: Select all
ahkdll := ComObjCreate("AutoHotkey.Script")
ahkdll.ahktextdll("msgbox, Hello")
While ahkdll.ahkReady()
Sleep, 100
I'm probably rambling, but take a look at this code that works either with or without the ahk.Reset(). It's scripted and not compiled, taken from a G1ANT script, you'll see sort of what's happening with dlls and namespaces, but that's not really important here. Everything between '⊂⊃' is C#. If you put the delimiters on separate lines it's like an AH continuation section, I suppose).
Code: Select all
addon core version 4.100.19170.929
addon language version 4.100.19170.929
-dialog ♥macrodlls
♥macrodlls = System.dll,System.Drawing.dll,System.Windows.Forms.dll,AutoHotkey.Interop.dll
-dialog ♥macrodlls
♥macronamespaces = System,AutoHotkey.Interop,System.Windows.Forms
♥hellogiant = "Hello "
⊂
var ahk = AutoHotkeyEngine.Instance;
ahk.ExecRaw("MsgBox, Hello World!");
ahk.SetVar("x", ♥hellogiant);
ahk.SetVar("y", "World, from AHK and G1ANT!");
ahk.ExecRaw("z:=x . y");
string zValue = ahk.GetVar("z");
System.Windows.Forms.MessageBox.Show(zValue);;
ahk.SetVar("z", zValue);
ahk.ExecRaw("Msgbox, I can say %z%");
ahk.Reset();
⊃
Code: Select all
addon core version 4.100.19170.929
addon language version 4.100.19170.929
-dialog ♥macrodlls
♥macrodlls = System.dll,System.Drawing.dll,System.Windows.Forms.dll,AutoHotkey.Interop.dll,System.Runtime.InteropServices.dll,System.Reflection.dll,Microsoft.CSharp.dll,System.Threading.Thread.dll
-dialog ♥macrodlls
♥macronamespaces = System,AutoHotkey.Interop,System.Windows.Forms,System.Reflection,System.Threading
-text.read C:\G1ANT\ie_com3.ahk result ♥ahkIn
♥ahkIn = ‴C:\G1ANT\ie_com4.ahk‴
⊂
var ahk = AutoHotkeyEngine.Instance;
//Load a library or exec scripts in a file
ahk.LoadFile(♥ahkIn);
MessageBox.Show("Stop thread");
ahk.Reset();
⊃
@HotKeyIt, could you please suggest a way to provide the same functionality as
Code: Select all
While ahkdll.ahkReady()
Sleep, 100
(I'm going to try to add a wrapped call to ahkReady() in a recompiled AutoHotkey.Interop, but frankly, I don't like my chances.)
A million thanks in advance,
burque505
Re: AutoHotkey.dll and C#
Well, I'll be hornswoggled - it worked. I was able to add a call to ahkReady() in a recompiled AutoHotkey.Interop. I will try to fork it, but my github skills are nearly non-existent.
This code now works in G1ANT (I will try removing references one-by-one until I don't have so many dependencies, but that's probably immaterial to most people. Unless everybody and his pet duck starts using G1ANT all of a sudden - could happen
.
EDIT
: Forked AutoHotkey.Interop is here.
For those (if any) who are interested in this, but can't wait till I get this fork up on github, I just added this code at the end of AutoHotkeyEngine.cs:
I believe this would work as well, as this is what Dnspy reports it compiles to anyway:
Regards,
burque505
This code now works in G1ANT (I will try removing references one-by-one until I don't have so many dependencies, but that's probably immaterial to most people. Unless everybody and his pet duck starts using G1ANT all of a sudden - could happen
![Wink ;)](./images/smilies/icon_e_wink.gif)
EDIT
![Smile :)](./images/smilies/icon_e_smile.gif)
Code: Select all
addon core version 4.100.19170.929
addon language version 4.100.19170.929
-dialog ♥macrodlls
♥macrodlls = System.dll,System.Drawing.dll,System.Windows.Forms.dll,AutoHotkey.Interop.dll,System.Runtime.InteropServices.dll,System.Reflection.dll,Microsoft.CSharp.dll,System.Threading.Thread.dll
-dialog ♥macrodlls
♥macronamespaces = System,AutoHotkey.Interop,System.Windows.Forms,System.Reflection,System.Threading
-text.read C:\G1ANT\ie_com3.ahk result ♥ahkIn
♥ahkIn = ‴C:\G1ANT\ie_com4.ahk‴
⊂
var ahk = AutoHotkeyEngine.Instance;
//Load a library or exec scripts in a file
while (ahk.IsReady()==false) {
Thread.Sleep(100);
}
ahk.LoadFile(♥ahkIn);
//MessageBox.Show("Stop thread");
while (ahk.IsReady()==true) {
Thread.Sleep(100);
}
ahk.Reset();
⊃
Code: Select all
/// <summary>
/// Determines whether state is "Ready" or not
/// </summary>
/// <param>No parameters</param>
/// <returns>Returns true if the state is "Ready", otherwise false</returns>
public bool IsReady()
{
if (AutoHotkeyDll.ahkReady() == true) {
return true;
}
else {
return false;
}
}
Code: Select all
/// <summary>
/// Determines whether state is "Ready" or not
/// </summary>
/// <param>No parameters</param>
/// <returns>Returns true if the state is "Ready", otherwise false</returns>
public bool IsReady()
{
return AutoHotkeyDll.ahkReady();
}
}
burque505
Who is online
Users browsing this forum: No registered users and 49 guests