Can I use GuiControlGet in a Hotkey?

Get help with using AutoHotkey (v1.1 and older) and its commands and hotkeys
DaveT1
Posts: 224
Joined: 07 Oct 2014, 11:23

Can I use GuiControlGet in a Hotkey?

11 Feb 2015, 14:22

Hi All,

Sorry, I think that this might be a really basic question. I'm trying to move a button on a window by using a hotkey. This is the simple code that I have so far:

Code: Select all

#NoEnv  ; Recommended for performance and compatibility with future AutoHotkey releases.
#Warn  ; Enable warnings to assist with detecting common errors.
SendMode Input  ; Recommended for new scripts due to its superior speed and reliability.
SetWorkingDir %A_ScriptDir%  ; Ensures a consistent starting directory.
SetTitleMatchMode, 2 ;this allows comparison of substrings of Window Titles, rather than the full text.
#SingleInstance force ; This suppresses the warning dialogue that a newly launched script is already running.

;Set up the window and add a simple button.
Gui, New, , MyGui Title
Gui, Add, Text, w500 h200, test
Gui, Color, Green
Gui, Add, Button, x250, OKButton
Gui, Show

;Now use GuiControlGet to return the position of the button:

;OPTION 1: This returns the Pos results.
GuiControlGet, T, Pos, OKButton
msgbox, Error = %Errorlevel% `nButton x = %Tx%`nButton y = %Ty%
ExitApp

;OPTION 2: This is the same as above, but in a hotkey. But now the Pos variables are empty!
Left::
	GuiControlGet, T, Pos, OKButton
	msgbox, Error = %Errorlevel% `nButton x = %Tx%`nButton y = %Ty%
	Return

GuiEscape:
GuiClose:
	ExitApp
Thing is I can't understand why Option1 returns the position results for the button, but that Option2 only returns empty!

Once I can return the position of the button via option 2, I can then add to the position a fixed amount by using GuiControl, Move, OKButton, % "x"OKButtonx+10.

So my question is, why option 1 returns the position, but that option 2 does not.

I'm sure that I'm missing something basic here - so any pointers very much appreciated.
User avatar
TLM
Posts: 1608
Joined: 01 Oct 2013, 07:52
Contact:

Re: Can I use GuiControlGet in a Hotkey?

11 Feb 2015, 14:49

If you don't add a return or something to stop the ExitApp command, the script will close.

Code: Select all

#NoEnv  ; Recommended for performance and compatibility with future AutoHotkey releases.
#Warn  ; Enable warnings to assist with detecting common errors.
SendMode Input  ; Recommended for new scripts due to its superior speed and reliability.
SetWorkingDir %A_ScriptDir%  ; Ensures a consistent starting directory.
SetTitleMatchMode, 2 ;this allows comparison of substrings of Window Titles, rather than the full text.
#SingleInstance force ; This suppresses the warning dialogue that a newly launched script is already running.

;Set up the window and add a simple button.
Gui, New, , MyGui Title
Gui, Add, Text, w500 h200, test
Gui, Color, Green
Gui, Add, Button, x250, OKButton
Gui, Show

;Now use GuiControlGet to return the position of the button:

;OPTION 1: This returns the Pos results.
GuiControlGet, T, Pos, OKButton
msgbox, Error = %Errorlevel% `nButton x = %Tx%`nButton y = %Ty%
Return ; << added to stop the below ExitApp
; ExitApp ; << this will exit the entire script

;OPTION 2: This is the same as above, but in a hotkey. But now the Pos variables are empty!
Left::
    GuiControlGet, T, Pos, OKButton
    msgbox, Error = %Errorlevel% `nButton x = %Tx%`nButton y = %Ty%
    Return

GuiEscape:
GuiClose:
    ExitApp
Now if you press Left, you should get the button's position
DaveT1
Posts: 224
Joined: 07 Oct 2014, 11:23

Re: Can I use GuiControlGet in a Hotkey?

11 Feb 2015, 16:08

Hi TLM,

Thanks for the reply. I'm not sure if I made things clear. I have a script with either option 1 or option 2 in. Not both in the same file. The problem is that the script with only option 1 does return the button position information, but the script with only option 2 in returns no content for the positional information.

The only difference between these 2 scripts is that the second one is contained in a hotkey.

I just can't figure this one out.

Cheers.
lexikos
Posts: 9592
Joined: 30 Sep 2013, 04:07
Contact:

Re: Can I use GuiControlGet in a Hotkey?

11 Feb 2015, 17:05

The hotkey thread starts with default settings, including default Gui 1.
Gui, New, , MyGui Title
Gui New wrote:Creates a new window and sets it as the default for the current thread.
If you don't give the Gui a name (Gui Name: New) or store its HWND (+hwndVAR in the options), you have no way to refer to it in the hotkey thread. You probably should just remove Gui New and set the title in Gui Show. Alternatively, you can use Gui 1: New.
MJs
Posts: 454
Joined: 23 Sep 2014, 03:29

Re: Can I use GuiControlGet in a Hotkey?

11 Feb 2015, 17:16

you should show us both scripts, but if they are the same same that means each line is the same as the one in the other script up until that line with OPTION 1, then TLM way ought to work EXCEPT if you're not working on the default GUI, and it might be that, the button is not on the default GUI.
see if you can relate to that:
http://ahkscript.org/boards/viewtopic.php?f=5&t=6307
lexikos
Posts: 9592
Joined: 30 Sep 2013, 04:07
Contact:

Re: Can I use GuiControlGet in a Hotkey?

11 Feb 2015, 19:30

I think you both missed the point. Both "options" are included in the same script in DaveT1's first post.

The script does not exit until you dismiss the MsgBox, so ExitApp is not an issue.
MJs
Posts: 454
Joined: 23 Sep 2014, 03:29

Re: Can I use GuiControlGet in a Hotkey?

11 Feb 2015, 20:13

The only difference between these 2 scripts is that the second one is contained in a hotkey.
and
Not both in the same file
suggests that there are two scripts, but he merged them in his first post,
that's why I thought TLM's script would work if they were the same (except for the method to get the position info), except for none default GUI

just one more little thing lexikos, when I was writing the reply your post wasn't shown, but when I submitted it, yours is shown with 11 minute different, is this normal?, or it just took me 11 minutes to write it :o
DaveT1
Posts: 224
Joined: 07 Oct 2014, 11:23

Re: Can I use GuiControlGet in a Hotkey?

12 Feb 2015, 03:14

Hi lexikos and MJs, many thanks for getting back on this.

@MJs: I'm sorry if I've caused confusion....the single script I posted would be run with either option 1 or option 2, but not both together. In other words I'd comment out the option I was not testing - apologies for the confusion.

@lexikos: nail hit on head ;)! If I comment out the Gui, New, , MyGui Title line, then the hotkey thread works perfectly, ie, this code

Code: Select all

;Set up the window and add a simple button.
;Gui, New, , MyGui Title
Gui, Add, Text, w500 h200, test
Gui, Color, Green
Gui, Add, Button, x250, OKButton
Gui, Show

Left::
	GuiControlGet, T, Pos, OKButton
	GuiControl, Move, OKButton, % "x"Tx-10
	Return

GuiEscape:
GuiClose:
	ExitApp
works perfectly - so many thanks for pointing out that one.

But, if you don't mind me exploring this a little more.....I'd originally introduced Gui, New, , MyGui Title to help readability for my own understanding (newbie AHK user here!). Of course I hadn't appreciated the impact of doing this! But if I wanted to retain this, then you say I should name this window, so that I can then refer to it in the hotkey thread. So the code below I think does this:

Code: Select all

;Set up the window and add a simple button.
Gui, MyGui:New, , MyGui Title
Gui, MyGui:Add, Text, w500 h200, test
Gui, MyGui:Color, Green
Gui, MyGui:Add, Button, x250, OKButton
Gui, MyGui:Show

Left::
	GuiControlGet, T, MyGui:Pos, OKButton
	GuiControl, MyGui:Move, OKButton, % "x"Tx-10
	Return

GuiEscape:
GuiClose:
	ExitApp
I can appreciate that there is probably overkill / unnecessary use of MyGui: here. But it improves clarity (for me as a newbie) and would have avoided me failing into the trap that you spotted I was well and truly stuck in!

Thanks again everyone for the help, and apologies again if my 1st post caused any confusion.

Return to “Ask for Help (v1)”

Who is online

Users browsing this forum: Ineedhelplz, Spawnova and 232 guests