how to simulate the key send as the ahk L do in cpp? Topic is solved

Get help with using AutoHotkey and its commands and hotkeys
woshichuanqilz72
Posts: 113
Joined: 05 Oct 2015, 21:23

how to simulate the key send as the ahk L do in cpp?

13 Feb 2019, 03:55

Hi, I am going to simulate key press in my cpp code directly. I use sendinput func in cpp, but is not work well. I don't know why sendinput just not worked in some window, even when I try the instructions mentioned on Stack Overflow to use the scancode.

But when I try ahk exe the key send normally, so I want to simulate the ahk l way to send key stroke. I checked the code of ahk_l on github already,

iit's a huge project, I don't know where to start.

Anyone can give me some advice about the source code of ahk _L ?

thx ~
woshichuanqilz72
Posts: 113
Joined: 05 Oct 2015, 21:23

Re: how to simulate the key send as the ahk L do in cpp?

14 Feb 2019, 01:33

evilC wrote:
13 Feb 2019, 07:36
Maybe a simpler solution would be to use AutoHotkey.dll
You could load that libary from your C code, then simply call AHK's send function from your C code.
https://hotkeyit.github.io/v2/docs/AutoHotkeyDll.htm
Don't worry about the V2 stuff, there is a dll for V1 too
thx for your timely reply, but I get some problem when call the dll from cpp.
Here is my code, I copy it from this link : https://autohotkey.com/board/topic/39588-autohotkeydll/://autohotkey.com/board/topic/39588-autohotkeydll/page-28?&#entry418330, written by hotkeyit.

and download the dll from : https://codeload.github.com/HotKeyIt/ahkdll-v2-release/zip/master.

When I run the code I get :

Image. Empty msgbox without text "ahk".

If I change the msgbox ahk to "send test", nothing happens. So how to call the dll properly? Tnx.

Code: Select all

#include <windows.h> 

int main(void) 
{ 
   typedef BOOL (*pahkReady)(void); //Typedef the functions
   typedef BOOL (*pahkExec)(LPTSTR script); 
   typedef UINT (*pahkdll)(LPTSTR script,LPTSTR p1,LPTSTR p2); 
    
   HINSTANCE hdll = LoadLibrary("[color=red]C:\\Windows\\System32\\AutoHotkey.dll[/color]"); // Load Library
    
   pahkdll ahkdll = (pahkdll)GetProcAddress(hdll, "ahkdll"); // assign function pointers
   pahkReady ahkReady = (pahkReady)GetProcAddress(hdll, "ahkReady"); 
   pahkExec ahkExec = (pahkExec)GetProcAddress(hdll, "ahkExec"); 
    
   ahkdll("","",""); // start fresh AutoHotkey in #Persistent mode
   while (!ahkReady()) // wait for AutoHotkey
      Sleep(10);
   ahkExec("Msgbox AHK"); //execute a script

    return 0; 
}
swagfag
Posts: 2768
Joined: 11 Jan 2017, 17:59

Re: how to simulate the key send as the ahk L do in cpp?

14 Feb 2019, 02:05

ahkExec("Msgbox AHK");

Msgbox AHK string literals need to be quoted in v2. AHK refers to an empty variable in this context, hence the empty msgbox. wrap it in quotes or download the v1.dll if u wanna keep writing it that way
woshichuanqilz72
Posts: 113
Joined: 05 Oct 2015, 21:23

Re: how to simulate the key send as the ahk L do in cpp?

14 Feb 2019, 03:40

swagfag wrote:
14 Feb 2019, 02:05
ahkExec("Msgbox AHK");

Msgbox AHK string literals need to be quoted in v2. AHK refers to an empty variable in this context, hence the empty msgbox. wrap it in quotes or download the v1.dll if u wanna keep writing it that way
tnx my bro, the code worked.

Code: Select all

ahkExec(TEXT("Msgbox \"AHK\"")); //execute a script
// same goes with the send command
ahkExec(TEXT("Send \"{lalt down}\""));

How do you know the difference where can I get the manual of how to use it. V2 is better than V1?

Any examples for V2? Thank you again.
woshichuanqilz72
Posts: 113
Joined: 05 Oct 2015, 21:23

Re: how to simulate the key send as the ahk L do in cpp?

15 Feb 2019, 09:22

evilC wrote:
14 Feb 2019, 05:10
If all you want to do is send keys, then v1 or v2 will make no real difference I don't think.
Hi ~
what should I do if I want to use the wingetpos function, how to get the outputvar?

Any tutorial on it?
woshichuanqilz72
Posts: 113
Joined: 05 Oct 2015, 21:23

Re: how to simulate the key send as the ahk L do in cpp?

16 Feb 2019, 21:04

evilC wrote:
15 Feb 2019, 10:44
Use AhkGetVar to retrieve the variable value from the script
https://hotkeyit.github.io/v2/docs/commands/ahkgetvar.htm
sorry to asking you so many questions but these things really tough to get through google.

One more question, how to make two ahk stament in a dll call.
For example :

Code: Select all

CoordMode mouse, screen
click, 100, 100
Because I need to set the coordMode first then click the mouse. For example,
The coordmode is default set to current window,

I try to call the dll twice like this, but the coordmode won't changed when click the mouse.

Code: Select all

ahkExec("coordmode \"mouse, screen\"");
ahkExec("click \"100,100\"");
So how to make the click effected by the coordmode? Thx
User avatar
evilC
Posts: 4722
Joined: 27 Feb 2014, 12:30

Re: how to simulate the key send as the ahk L do in cpp?

17 Feb 2019, 08:25

AHKExec is more for executing one-off code. It may well be fine for what you want - to run multiple commands, you probably just have to add newlines.
If you want to start a proper AHK thread, you probably want ahkThread.
If you want to see some usage examples for that, see here: https://github.com/evilC/UCR/blob/master/Classes/UCRMain.ahk#L869
Here, AHK is the calling language, but the way you initialize and communicate with the thread should basically be the same.

Return to “Ask For Help”

Who is online

Users browsing this forum: Odlanir, Rafaews and 48 guests