problem with macro execution when starting ahk window

Get help with using AutoHotkey (v1.1 and older) and its commands and hotkeys
XtiiGmA
Posts: 26
Joined: 21 Apr 2021, 10:22

problem with macro execution when starting ahk window

10 May 2021, 10:45

Hi I have actually 2 problems.

I made a simple box where it saves and reads at the beginning the hotkey that is introduced and that when pressing the Save button and pressing the hotkey, it writes for example in notepad, "hello world". all good until there.

The first problem is that I have to press the Save button so that it sends "hello world" with any key that is entered in the hotkey.
Is it possible that only when you start the window again and press the hotkey, it says "hello world"? Without having to press the "Save" button.

The second problem is optional, I really don't think it can be because I've already tried it in many ways.
As you can see there is a checkbox called "SpaceKey"
Is it possible that when setting the checkbox to 1, the hotkey is just the space bar?

Code: Select all

#SingleInstance, force

Gui, Add, Checkbox,vcheck1 gCheck_1,SpaceKey
Gui, Add, Hotkey,vKey,
Gui, Add, Button,,Save
IniRead, read1, %A_Desktop%\initest.ini, Test10, key1
GuiControl,,Key, %read1%
Gui,Show,,Test
return

Check_1:
Gui, Submit, NoHide
if (check1=1)
{
	GuiControl, disable, Key
}
else
{
	GuiControl, enable, Key
}
return
ButtonSave:
	Gui, Submit, NoHide
	IniWrite, %key%, %A_Desktop%\initest.ini, Test10, key1
	HotKey, $%Key%, SayHello
	return
	
SayHello:
	Send, Hello World
	return
User avatar
mikeyww
Posts: 27372
Joined: 09 Sep 2014, 18:38

Re: problem with macro execution when starting ahk window

10 May 2021, 11:00

Here is an idea. It still has some issues when certain keys are pressed in the GUI after a hotkey has already been assigned.

Code: Select all

Gui, Add, Checkbox, vcheck1 gSave, SpaceKey
Gui, Add, Hotkey, vKey gSave
IniRead, read1, % ini := A_Desktop "\initest.ini", Test10, key1
GuiControl,, Key, %read1%
Gui, Show,, Test
Save:
Gui, Submit, NoHide
GuiControl, % check1 ? "Disable" : "Enable", Key
IniWrite, % hk := check1 ? "Space" : Key, %ini%, Test10, key1
HotKey, $%hk%, SayHello
Return
SayHello:
Send Hello World
Return
XtiiGmA
Posts: 26
Joined: 21 Apr 2021, 10:22

Re: problem with macro execution when starting ahk window

10 May 2021, 12:40

Hello friend, I have been trying to couple your code that works in my code without success.
I have tried to enter step by step what you do but the last lines of your code I do not understand.
To make it work, what you did is put a function in the "Save" hotkey.
So, I think the script saves everything at startup.
Could you explain this to me?

Code: Select all

GuiControl,% check1? "Disable": "Enable", Key

here I do not understand why only "%" and the question mark (?)
Is the same as this true?

Code: Select all

Save:
Gui, Submit, NoHide
if (check1=1)
{
	GuiControl, disable, Key
}
else
{
	GuiControl, enable, Key
}
return
I realized that this is the code that works when starting the window.
But I've tried to understand it and I can't.

Code: Select all

 IniWrite,% hk: = check1? "Space": Key,% ini%, Test10, key1
Write a variable and store it in check1?


Could you help me understand? or make a simpler code? Let's forget about problem 2, about the "space" key ... only that the program is executed when the window starts.

Code: Select all

#SingleInstance, force

ini := A_Desktop "\initest.ini"								;Ini = initest direction
Gui, Add, Checkbox, vcheck1 gSave,SpaceKey					;New Checkbox V=check1, G=Save, Name=SpaceKey
Gui, Add, Hotkey, vKey gSave								;New Hotkey V=Key, G=Save
IniRead, read1, %ini%, Test10, key1							;Read Key1 of Ini, Secction Test10
GuiControl,, Key, %read1%									;Make control V=Key with read1 
Gui, Show,, Test

Save:
Gui, Submit, NoHide
GuiControl, % check1 ? "Disable" : "Enable", Key
IniWrite, % hk := check1 ? "Space" : Key, %ini%, Test10, key1
HotKey, $%hk%, SayHello
Return

SayHello:
Send Hello World
Return
User avatar
mikeyww
Posts: 27372
Joined: 09 Sep 2014, 18:38

Re: problem with macro execution when starting ahk window

10 May 2021, 13:09

Sure. See Ternary operator, which is like "If... then... else"-- like you have it. The result is assigned to "hk", which is the final hotkey.

What does it do? If the "Space" box is checked, then hk is "Space"; otherwise, it is the value from the hotkey field. The script will then use hk in the Hotkey command.

In AHK, spaces sometimes matter, so := differs from : =. Check all of your syntax carefully; you changed what I wrote.
XtiiGmA
Posts: 26
Joined: 21 Apr 2021, 10:22

Re: problem with macro execution when starting ahk window

10 May 2021, 14:43

So is there any way that the hotkey when starting a window is executed?
with some simpler code?

I tried to put yours similar, but it does not work
User avatar
mikeyww
Posts: 27372
Joined: 09 Sep 2014, 18:38

Re: problem with macro execution when starting ahk window

10 May 2021, 15:31

This shows the GUI rather than executing the hotkey. If you want to execute the hotkey, just Gosub, SayHello.

Code: Select all

IniRead, Key, % ini := A_Desktop "\initest.ini", Test10, key1
Gui, New, +AlwaysOnTop
Gui, Add, Checkbox, vcheck1 gHK, SpaceKey
Gui, Add, Hotkey  , vKey    gHK
GuiControl,, Key, %Key%
OnExit("done")
Loop {
 WinWait, % wTitle := "ahk_exe notepad.exe"
 Gui, Show,, Test, NoActivate
 If !startedx
  Gosub, HK
 WinWaitClose
 Gui, Hide
}

HK:
SoundBeep, 1500
Gui, Submit, NoHide
If (hk > "") {
 Hotkey, IfWinActive, %wTitle%
 HotKey, $%hk%, Off
 Hotkey, IfWinActive
}
If check1 {
 GuiControl, Disable, Key
 hk = Space
} Else {
 GuiControl, Enable, Key
 hk := Key
}
Hotkey, IfWinActive, %wTitle%
HotKey, $%hk%, SayHello, On
Hotkey, IfWinActive
startedx := True
Return

SayHello:
Send Hello World
Return

done(ExitReason, ExitCode) {
 Global
 IniWrite, %hk%, %ini%, Test10, key1
 SoundBeep, 1000
}

Return to “Ask for Help (v1)”

Who is online

Users browsing this forum: Bing [Bot], Google [Bot] and 366 guests