I am complete beginner with C#, but advanced user with autohotkey.
If I have this script, how could I call that from C#?
ins::suspend
SendMode Input
Lbutton::
Loop
{
GetKeyState, state, Lbutton, P
if state=U
break
Sendinput {Click down}
Sleep 25
Sendinput {Click up}
Sleep 25
}
return
Could you show me simple example, so I can get to understand how to do it.
This is a may be reached through AutoHotkey.dll (that has COM Interface).
You to need download this library, move in c:\Windows\System32
.
And register for the system (Run, % "regsvr32.exe AutoHotkey.dll"
, % "c:\Windows\System32")
.
Then in VS create a console application project, and choose Project tab/Add reference.
In opened window find AutoHotkey library, click on "Add" button, then close the window.
So now you have connected this library in your project, and this you'll see in reference folder.
Select all in Program.cs and replace on this code:
using System.Threading;
using AutoHotkey;
namespace work_with_AHK_object
{
class Program
{
static void Main()
{
/// write content for ahk script (thread)
string scriptContent=""
//+"#NoTrayIcon\n"
+"#KeyHistory, 0\n"
+"#NoEnv\n"
//+"ListLines, Off\n"
//+"DetectHiddenWindows, On\n"
//+"Process, Priority,, High\n"
+"SetBatchLines, -1\n"
+"SetMouseDelay, 25\n"
//+"Menu, Tray, Icon, % \"shell32.dll\", -153\n"
//+"WinSet, AlwaysOnTop, On, % \"ahk_id\"A_ScriptHwnd\n"
//+"WinSet, Style, -0xC00000, % \"ahk_id\"A_ScriptHwnd\n"
//+"WinMove, % \"ahk_id\"A_ScriptHwnd,, 888, 110, 914, 812\n"
//+"ListLines\n"
//+"ListLines, On\n"
+"TrayTip,, % \"Ready to use!\"\n" /// some notice
+""
+"Ins::\n"
+" Suspend\n"
+" Loop, % A_IsSuspended ? 1:2\n"
+" SoundBeep, 12500, 50\n"
+" KeyWait, % A_ThisHotkey\n"
+" Return\n"
+""
+"LButton::\n"
+" Loop\n"
+" Send, {Click}\n"
+" Until, !GetKeyState(\"LButton\", \"P\")\n"
+" Return\n"
+""
+"Space::\n"
+" Suspend, Off\n"
+" ExitApp";
/// initialize instance
CoCOMServer ahkThread=new CoCOMServer();
/// launch a script in a separate thread
ahkThread.ahktextdll(scriptContent);
/// wait for exit
while (ahkThread.ahkReady()!=0) Thread.Sleep(1000);
}
}
}
Open project property, in Application tab change it Output type to Windows Application.
I know this is quite an old post but I just had this issue myself and struggled quite a bit to find a solution, since I couldn´t use any of the available wrapper projects for AHK in C#.
If it´s an issue for you as well to register a the dll or to use a wrapper you can use the approach from this post on the ahk forums by basi.
Essentially you only need to place the dll in your project folder, include it into the project and set "Copy to Output Directory" to "Copy if newer" in the properties.
Then import the dll functions like this:
[DllImport(
"AutoHotkey.dll",
CallingConvention = CallingConvention.Cdecl,
CharSet = CharSet.Unicode,
EntryPoint = "ahkdll")]
private static extern int ahkdll(
[MarshalAs(UnmanagedType.LPWStr)] string scriptFilePath,
[MarshalAs(UnmanagedType.LPWStr)] string parameters = "",
[MarshalAs(UnmanagedType.LPWStr)] string title = "");
[DllImport(
"AutoHotkey.dll",
CallingConvention = CallingConvention.Cdecl,
CharSet = CharSet.Unicode,
EntryPoint = "ahktextdll")]
private static extern int ahktextdll(
[MarshalAs(UnmanagedType.LPWStr)] string script,
[MarshalAs(UnmanagedType.LPWStr)] string parameters = "",
[MarshalAs(UnmanagedType.LPWStr)] string title = "");
ahkdll allows to run a script from a file, ahktextdll allows to directly insert a script as a string.
I only tested this with the v1 dll from HotKeyIt (i used the one in the win32w folder).
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With