Trigger a picture control g-label via keyboard?

Get help with using AutoHotkey (v1.1 and older) and its commands and hotkeys
JBensimon
Posts: 118
Joined: 19 Nov 2017, 11:19

Trigger a picture control g-label via keyboard?

Post by JBensimon » 11 Mar 2023, 15:10

If I tab over to a Gui picture control, for example one defined as follows,

Code: Select all

Gui, Add, Picture, +Border +TabStop gPickIcon vSelectedIcon Icon23, %A_WinDir%\System32\SHELL32.dll
, is it possible to trigger its g-label (here PickIcon) using the keyboard rather than the mouse? Neither Enter nor Space do the job (but a mouse click does).

Thanks.

JB

swagfag
Posts: 6222
Joined: 11 Jan 2017, 17:59

Re: Trigger a picture control g-label via keyboard?

Post by swagfag » 11 Mar 2023, 16:39

Code: Select all

#Requires AutoHotkey v1.1.33.02

#If GuiControlGetFocusedVarName() = "SelectedIcon"
Enter::
Space::
	; ....
return

GuiControlGetFocusedVarName() {
	GuiControlGet focusedVarName, FocusV
	return focusedVarName
}

User avatar
mikeyww
Posts: 26601
Joined: 09 Sep 2014, 18:38

Re: Trigger a picture control g-label via keyboard?

Post by mikeyww » 11 Mar 2023, 16:40

Hi,

You can create a hotkey that calls the same subroutine.

JBensimon
Posts: 118
Joined: 19 Nov 2017, 11:19

Re: Trigger a picture control g-label via keyboard?

Post by JBensimon » 13 Mar 2023, 10:12

Thank you both for the replies. Yes, a custom hotkey will do the trick, but what I was wondering was whether there was "Windows native" way (of which I was unaware) to trigger a picture control action, and I guess the answer is no. Microsoft is usually pretty good at making sure that things can be done with keyboard only (although they seem to have almost completely abandoned that effort in their "modern" apps and UI elements).

Thanks again.

User avatar
mikeyww
Posts: 26601
Joined: 09 Sep 2014, 18:38

Re: Trigger a picture control g-label via keyboard?

Post by mikeyww » 13 Mar 2023, 10:45

Another alternative is below.

Code: Select all

#Requires AutoHotkey v1.1.33
image := A_ScriptDir "\test.png"
Gui Add, Pic,, % image
Gui Add, Button, ggo Default w0 h0
Gui Show
Return

go() {
 MsgBox 123
}

User avatar
Hellbent
Posts: 2102
Joined: 23 Sep 2017, 13:34

Re: Trigger a picture control g-label via keyboard?

Post by Hellbent » 14 Mar 2023, 09:56

A simple way to add keyboard navigation is to create a list of controls to cycle through and track the index of the currently focused control.

For simple interactions you can just use context sensitive hotkeys like this. ( cycling through and interacting with 3 picture controls using the tab and enter keys )
.
change focus 1.gif
change focus 1.gif (356.16 KiB) Viewed 482 times
.

Code: Select all

#SingleInstance, Force

Gui1 := {}

Gui, New, +AlwaysOnTop +HwndHwnd
Gui1.Hwnd := hwnd

Gui1.Controls := {}
Gui1.Handles := []

cc := Gui1.Controls.Picture_1 := { Hwnd: "" , X: "m" , Y: "m" , W: 200 , H: 200 , Label: "PictureRoutine_1" }
Gui, Add, Picture, % "x" cc.X " y" cc.Y " w" cc.W " h" cc.H " Icon46 hwndhwnd g" cc.Label , Shell32.dll
cc.Hwnd := Hwnd
Gui1.Handles[ Hwnd ] := cc

cc := Gui1.Controls.Picture_2 := { Hwnd: "" , X: "m" , Y: "+10" , W: 200 , H: 200 , Label: "PictureRoutine_2" }
Gui, Add, Picture, % "x" cc.X " y" cc.Y " w" cc.W " h" cc.H " Icon47 hwndhwnd +Border g" cc.Label , Shell32.dll
cc.Hwnd := hwnd
Gui1.Handles[ hwnd ] := cc

cc := Gui1.Controls.Picture_3 := { Hwnd: "" , X: "m" , Y: "+10" , W: 200 , H: 200 , Label: "PictureRoutine_3" }
Gui, Add, Picture, % "x" cc.X " y" cc.Y " w" cc.W " h" cc.H " Icon48 hwndhwnd g" cc.Label , Shell32.dll
cc.Hwnd := hwnd
Gui1.Handles[ hwnd ] := cc

Gui, Show

Gui1.ControlList := [ "Picture_1" , "Picture_2" , "Picture_3" ]
Gui1.FocusedControl := 2

return
GuiClose:
GuiContextMenu:
*ESC::ExitApp

#If ( WinActive( "ahk_Id " Gui1.Hwnd ) )

	Tab::
		WinSet, Style, -0x00800000 , % "ahk_id "  Gui1.Controls[ Gui1.ControlList[ Gui1.FocusedControl ] ].Hwnd
		( ++Gui1.FocusedControl > Gui1.ControlList.Length() ) ? ( Gui1.FocusedControl := 1 )
		WinSet, Style, +0x00800000 , % "ahk_id "  Gui1.Controls[ Gui1.ControlList[ Gui1.FocusedControl ] ].Hwnd
		GuiControl, % Gui1.Hwnd ":Focus", % Gui1.Controls[ Gui1.ControlList[ Gui1.FocusedControl ] ].Hwnd
		return

	Enter::
		Try{
			gosub, % Gui1.Controls[ Gui1.ControlList[ Gui1.FocusedControl ] ].Label
		}catch{
			Gui, % Gui1.Hwnd ":+OwnDialogs"
			MsgBox, Error 
		}
		return
		
#If


PictureRoutine_1:
	if( Gui1.FocusedControl != 1 ){
		WinSet, Style, -0x00800000 , % "ahk_id "  Gui1.Controls[ Gui1.ControlList[ Gui1.FocusedControl ] ].Hwnd
		Gui1.FocusedControl := 1
		WinSet, Style, +0x00800000 , % "ahk_id "  Gui1.Controls[ Gui1.ControlList[ Gui1.FocusedControl ] ].Hwnd
		GuiControl, % Gui1.Hwnd ":Focus", % Gui1.Controls[ Gui1.ControlList[ Gui1.FocusedControl ] ].Hwnd
	}
	ToolTip, % "Control # " Gui1.FocusedControl
	SetTimer, TipsOff, -2000
	SoundBeep 555
	return
	
PictureRoutine_2:
	if( Gui1.FocusedControl != 2 ){
		WinSet, Style, -0x00800000 , % "ahk_id "  Gui1.Controls[ Gui1.ControlList[ Gui1.FocusedControl ] ].Hwnd
		Gui1.FocusedControl := 2
		WinSet, Style, +0x00800000 , % "ahk_id "  Gui1.Controls[ Gui1.ControlList[ Gui1.FocusedControl ] ].Hwnd
		GuiControl, % Gui1.Hwnd ":Focus", % Gui1.Controls[ Gui1.ControlList[ Gui1.FocusedControl ] ].Hwnd
	}
	ToolTip, % "Control # " Gui1.FocusedControl
	SetTimer, TipsOff, -2000
	SoundBeep 666
	return	

PictureRoutine_3:
	if( Gui1.FocusedControl != 3 ){
		WinSet, Style, -0x00800000 , % "ahk_id "  Gui1.Controls[ Gui1.ControlList[ Gui1.FocusedControl ] ].Hwnd
		Gui1.FocusedControl := 3
		WinSet, Style, +0x00800000 , % "ahk_id "  Gui1.Controls[ Gui1.ControlList[ Gui1.FocusedControl ] ].Hwnd
		GuiControl, % Gui1.Hwnd ":Focus", % Gui1.Controls[ Gui1.ControlList[ Gui1.FocusedControl ] ].Hwnd
	}
	ToolTip, % "Control # " Gui1.FocusedControl
	SetTimer, TipsOff, -2000
	SoundBeep 777
	return	


TipsOff:
	ToolTip
	return



Or for more complicated interactions you can monitor the window message [ 0x100 ]

Code: Select all

OnMessage( 0x100 , Func( "_CaptureKeys" ).Bind( Gui1) )
In this example there is only one control and everything you think you see is fake. ( It's all just one updating image / picture control )
.
change focus 2.gif
change focus 2.gif (226.59 KiB) Viewed 482 times

User avatar
mikeyww
Posts: 26601
Joined: 09 Sep 2014, 18:38

Re: Trigger a picture control g-label via keyboard?

Post by mikeyww » 14 Mar 2023, 10:34

I think that the request related to not using any hotkey definitions to accomplish the goal.
Yes, a custom hotkey will do the trick, but what I was wondering was whether there was "Windows native" way (of which I was unaware) to trigger a picture control action.

User avatar
Hellbent
Posts: 2102
Joined: 23 Sep 2017, 13:34

Re: Trigger a picture control g-label via keyboard?

Post by Hellbent » 14 Mar 2023, 10:45

mikeyww wrote:
14 Mar 2023, 10:34
I think that the request related to not using any hotkey definitions to accomplish the goal.
Yes, a custom hotkey will do the trick, but what I was wondering was whether there was "Windows native" way (of which I was unaware) to trigger a picture control action.
Yep
Or for more complicated interactions you can monitor the window message [ 0x100 ]

Code: Select all

OnMessage( 0x100 , Func( "_CaptureKeys" ).Bind( Gui1) )

Code: Select all

#SingleInstance, Force

Gui1 := {}

Gui, New, +AlwaysOnTop +HwndHwnd
Gui1.Hwnd := hwnd

Gui, Show, w300 h300
OnMessage( 0x100 , Func( "_CaptureKeys" ).Bind( Gui1 ) )

return
GuiClose:
GuiContextMenu:
*ESC::ExitApp


_CaptureKeys( Gui1 , input ){
	hwnd := WinActive( "A" )
	if( hwnd != Gui1.Hwnd )
		return 1
	
	if( Input = 9 )
		ToolTip, % "You Pressed the TAB key"
	else
		ToolTip, % "You Pressed: " chr( Input ) "`nInput: " Input 
	SetTimer, TipsOff, -2000
}


TipsOff:
	ToolTip
	return





User avatar
Hellbent
Posts: 2102
Joined: 23 Sep 2017, 13:34

Re: Trigger a picture control g-label via keyboard?

Post by Hellbent » 14 Mar 2023, 11:59

Here is a specific example of triggering the label of a picture control using window messages to monitor for a "Enter" keystroke.
Depending on what you are doing it can get more complex and require some extra logic. See my two earlier posts for insights.

Code: Select all

#SingleInstance, Force

Gui1 := {}

Gui, New, +AlwaysOnTop +HwndHwnd
Gui1.Hwnd := hwnd

Gui, Margin, % Gui1.Margin := 10 , 10

Gui1.Controls := {}

cc := Gui1.Controls.Picture_1 := { hwnd: "" , X: Gui1.Margin , Y: Gui1.Margin , W: 200 , H: 200 , Label: "PicRoutine" }

Gui, Add, Pic, %  "x" cc.X " y" cc.Y " w" cc.W " h" cc.H " Icon46 +hwndhwnd g" cc.Label , Shell32.dll

cc.Hwnd := hwnd

Gui, Show,

OnMessage( 0x100 , Func( "_CaptureKeys" ).Bind( Gui1 ) )

return
GuiClose:
GuiContextMenu:
*ESC::ExitApp

PicRoutine:
	Gui, % Gui1.Hwnd ":+OwnDialogs" 
	MsgBox, You pressed the pic control
	return 
	
_CaptureKeys( Gui1 , input ){
	hwnd := WinActive( "A" )
	if( hwnd != Gui1.Hwnd )
		return 1
	if( Input = 13 )
		gosub, % Gui1.Controls.Picture_1.Label
}




JBensimon
Posts: 118
Joined: 19 Nov 2017, 11:19

Re: Trigger a picture control g-label via keyboard?

Post by JBensimon » 31 Mar 2023, 10:27

Thank you both for all the suggestions. I'll do some testing when I get a chance ...

... but now that I come back to this question with fresh eyes, a simpler thought occurs to me: create a size 0 x 0 button at the expected tabstop position of the picture control (i.e. add the button immediately after adding the picture) and give it the same g-label as the picture -> pressing the spacebar when the invisible button has focus will have the same effect as clicking the picture:

Code: Select all

Gui, Add, Picture, x5 y5 +Border gPickIcon vSelectedIcon Icon23, %A_WinDir%\System32\SHELL32.dll
Gui, Add, Button, x5 y5 w0 h0 gPickIcon,
Whay say you?

JB

Post Reply

Return to “Ask for Help (v1)”