AutoHotKeyDLL; use .NET variables in AHK Script Topic is solved

Ask for help, how to use AHK_H, etc.
burque505
Posts: 1736
Joined: 22 Jan 2017, 19:37

AutoHotKeyDLL; use .NET variables in AHK Script

22 Jan 2017, 19:58

This code works in a WinForm as is. What I want to do is get that string "str" (which is a VB.NET string) into an AHK variable.
How can I do that? Sorry if this is old hat, but this is my first day with AHK/AHK.dll. Thanks in advance.

Sub SendAHKeys()
Dim ahk As Object
Dim str As String = "Here goes nothing"
ahk = CreateObject("AutoHotkey.Script")
ahk.ahktextdll("SetTitleMatchMode, 2" & vbLf &
"Run, notepad.exe" & vbLf &
"Sleep, 50" & vbLf &
"WinGet, rid , ,Notepad, , , " & vbLf &
"WinActivate, ahk_id %rid%" & vbLf &
"Send, {F5}")
End Sub

Private Sub btnSend_Click(sender As Object, e As EventArgs) Handles btnSend.Click
SendAHKeys()
End Sub
burque505
Posts: 1736
Joined: 22 Jan 2017, 19:37

Re: AutoHotKeyDLL; use .NET variables in AHK Script  Topic is solved

24 Jan 2017, 19:21

:clap: Thanks so much, and thanks for your most excellent work! I am in awe. I only had to change "var :=" to "var =" and it works great. Here's the code now:

Public Class Form1
Dim vbString As String = "Hello AHK World!"
Sub SendAHKeys()
Dim ahk As Object
ahk = CreateObject("AutoHotkey.Script")
ahk.ahktextdll("SetTitleMatchMode, 2" & vbLf &
"var = " & vbString & vbLf &
"Run, notepad.exe" & vbLf &
"Sleep, 50" & vbLf &
"WinGet, rid , ,Notepad, , , " & vbLf &
"WinActivate, ahk_id %rid%" & vbLf &
"Send, %var%")

End Sub

Private Sub btnSend_Click(sender As Object, e As EventArgs) Handles btnSend.Click
SendAHKeys()
End Sub
End Class

In another post I'll give some code that builds on Amazing-Andrew's AutoHotKey.Interop, which of course builds on yours.
Thanks again, you are very kind indeed.
burque505
Posts: 1736
Joined: 22 Jan 2017, 19:37

Re: AutoHotKeyDLL; use .NET variables in AHK Script

24 Jan 2017, 19:31

And here's the code with a few of Amazing-Andrew's samples from https://github.com/amazing-andrew/AutoHotkey.Interop, converted to VB.NET.
It's just a form with one button, like the last one.
I just built the DLL in VS2015 and dropped it in the project, as advertised.
(I haven't gotten to all of the samples, as you can see, and a few are still beyond me.) Once again, a million thanks to HotKeyIt and Amazing-Andrew.

Imports AutoHotkey.Interop
Public Class Form1
Public ahk = AutoHotkeyEngine.Instance
Public vbString As String = ""

Private Sub btnTest_Click(sender As Object, e As EventArgs) Handles btnTest.Click
ahk.ExecRaw("MsgBox, Hello World, from AHK!")
'programmatically set variables
ahk.SetVar("x", "1")
ahk.SetVar("y", "4")
ahk.SetVar("ahkstr", "this value to be passed to .NET")
vbString = ahk.GetVar("ahkstr")

MessageBox.Show("From the AHK engine I got " & vbString)

'execute statements
ahk.ExecRaw("z:=x+y")

'return variables back from ahk
Dim zValue As Integer = ahk.GetVar("z")
MessageBox.Show("Value of z is " & zValue.ToString) ' "Value of z is 5", and I don't have the interpolation as in the example


End Sub

End Class

Return to “Ask for Help”

Who is online

Users browsing this forum: No registered users and 49 guests