Right click on GUI button --> Action

Get help with using AutoHotkey (v1.1 and older) and its commands and hotkeys
timoshina
Posts: 67
Joined: 06 Jun 2017, 12:45

Right click on GUI button --> Action

09 Apr 2020, 02:35

Hey guys, I'm trying to figure out a way to have a GUI button that has 2 different actions (LButton - action 1, RButton- action 2).
Is it possible?

Code: Select all

#NoEnv 
#SingleInstance, force

gui, add, button, x40 gButton1 w100, Button 1
gui, add, button, x40 gButton2 w100, Button 2
gui, add, button, x40 gButton3 w100, Button 3

Gui, show, w200
return


Button1:
MsgBox Button 1 (Left Click)
return

Button2:
MsgBox Button 2 (Left Click)
return

Button3:
MsgBox Button 3 (Left Click)
return
;------------------------------------------
Button1_right_click:  ;???
MsgBox Button 1 (RIGHT Click)
return

Button2_right_click:   ;???
MsgBox Button 2 (RIGHT Click)
return

Button3_right_click:    ;???
MsgBox Button 3 (RIGHT Click)
return
garry
Posts: 3763
Joined: 22 Dec 2013, 12:50

Re: Right click on GUI button --> Action

09 Apr 2020, 02:46

an example , click button or rightclick get menu

Code: Select all

#warn
#NoEnv
setworkingdir,%a_scriptdir%
sc=ContextMenuButton_Test

Menu,2: Menu1, Add
Menu,2: Menu1, Add, AHK-OLD , MC1
Menu,2: Menu1, Add, AHK     , MC2
Menu,2: Menu1, Add

Menu,2: Menu2, Add, Notepad, Notepad1
Menu,2: Menu2, Add
Menu,2: Menu2, Add, Calc   , Calc1

Gui,2: Add, Button, x10  y130 w90  h25  vMb1 gAa1,Test-1
Gui,2: Add, Button, x110 y130 w90  h25  vMb2 gAa2,Test-2
Gui,2: Show, x100 y10 h170 w330 ,%sc%
Return
;--------------------------------

2GuiClose:
ExitApp
esc::exitapp

2GuiContextMenu:
 {
 MouseGetPos,,,,Control1
 If (A_GuiControl = "Mb1")
   Menu,2: Menu1, Show
 If (A_GuiControl = "Mb2")
   Menu,2: Menu2, Show
 }
return
;---------------------------
mc1:
run,https://autohotkey.com/board/
return

mc2:
run,https://www.autohotkey.com/boards/
return
;---------------------------
notepad1:
run,notepad
return

calc1:
run,calc
return
;---------------------------
aa1:
msgbox,You used Button Test-1
return

aa2:
msgbox,You used Button Test-2
return
;================================
User avatar
Hellbent
Posts: 2109
Joined: 23 Sep 2017, 13:34

Re: Right click on GUI button --> Action

09 Apr 2020, 03:19

Here is one approach of many possible approaches.

Code: Select all

#SingleInstance, Force
SetBatchLines, -1

global Handles := []

Gui,1:+AlwaysOnTop
Gui,1:Add,Button,hwndhwnd gButt1Action1,Button 1
Handles[hwnd] := "Butt1Action2"
Gui,1:Add,Button,hwndhwnd gButt2Action1,Button 2
Handles[hwnd] := "Butt2Action2"
Gui,1:Add,Button,hwndhwnd gButt3Action1,Button 3
Handles[hwnd] := "Butt3Action2"
Gui,1:Show,% "x" A_ScreenWidth - 400  " y150 w300 h150",Demo

OnMessage(0x205,"RButtonUp")
Return
GuiClose:
*ESC::
	ExitApp
;##########################################################	
Butt1Action1:
	MsgBox, Button 1 action 1
	return

Butt1Action2:
	MsgBox, Button 1 action 2
	return
;##########################################################
Butt2Action1:
	MsgBox, Button 2 action 1
	return

Butt2Action2:
	MsgBox, Button 2 action 2
	return
;##########################################################
Butt3Action1:
	MsgBox, Button 3 action 1
	return

Butt3Action2:
	MsgBox, Button 3 action 2
	return
;##########################################################

RButtonUp(w,l,msg,hwnd){
	if(Handles[hwnd])
		gosub, % Handles[hwnd]
}

HTH

PS. Will try doing the meeting tomorrow.
timoshina
Posts: 67
Joined: 06 Jun 2017, 12:45

Re: Right click on GUI button --> Action

10 Apr 2020, 16:43

Hellbent wrote:
09 Apr 2020, 03:19
Here is one approach of many possible approaches.
Thank you brother, really appreciated!
timoshina
Posts: 67
Joined: 06 Jun 2017, 12:45

Re: Right click on GUI button --> Action

10 Apr 2020, 16:44

garry wrote:
09 Apr 2020, 02:46
an example , click button or rightclick get menu
Thanks Garry! This is also an interesting solution, might be useful for future projects.
timoshina
Posts: 67
Joined: 06 Jun 2017, 12:45

Re: Right click on GUI button --> Action

16 Apr 2020, 05:17

How can i make it work in this scenario?

Code: Select all

#SingleInstance, Force
SetBatchLines, -1

#IfWinActive ahk_exe SciTE.exe
Space::

this()
{
global Handles := []

Gui,1:+AlwaysOnTop
Gui,1:Add,Button,hwndhwnd gButt1Action1  vButt1Action1,Button 1
Handles[hwnd] := "Butt1Action2"
Gui,1:Add,Button,hwndhwnd gButt2Action1  vButt2Action1,Button 2
Handles[hwnd] := "Butt2Action2"
Gui,1:Add,Button,hwndhwnd gButt3Action1  vButt3Action1,Button 3
Handles[hwnd] := "Butt3Action2"
Gui,1:Show,% "x" A_ScreenWidth - 400  " y150 w300 h150",Demo

OnMessage(0x205,"RButtonUp")
Return

}

GuiClose:
*ESC::
	ExitApp
;##########################################################	
Butt1Action1:
	MsgBox, Button 1 action 1
	return

Butt1Action2:
	MsgBox, Button 1 action 2
	return
;##########################################################
Butt2Action1:
	MsgBox, Button 2 action 1
	return

Butt2Action2:
	MsgBox, Button 2 action 2
	return
;##########################################################
Butt3Action1:
	MsgBox, Button 3 action 1
	return

Butt3Action2:
	MsgBox, Button 3 action 2
	return
;##########################################################

RButtonUp(w,l,msg,hwnd){
	if(Handles[hwnd])
		gosub, % Handles[hwnd]
}
timoshina
Posts: 67
Joined: 06 Jun 2017, 12:45

Re: Right click on GUI button --> Action

16 Apr 2020, 21:57

could someone please help?
lblb
Posts: 190
Joined: 30 Sep 2013, 11:31

Re: Right click on GUI button --> Action

17 Apr 2020, 16:46

I think you could still do it more simply using the built-in GuiContextMenu which stores the control's name when you right-click on the button (and then you can use if statements). This is basically a simplified version of garry's code:

Code: Select all

#SingleInstance, Force
SetBatchLines, -1

Gui,2: Add, Button, x10  y130 w90  h25  vMb1 gAa1,Test-1
Gui,2: Add, Button, x110 y130 w90  h25  vMb2 gAa2,Test-2
Gui,2: Show, x100 y10 h170 w330
Return

Aa1:
Msgbox, Hello
Return 

Aa2:
Msgbox, Hello2
Return 

2GuiContextMenu:
Msgbox, %A_GuiControl%
Return
just me
Posts: 9451
Joined: 02 Oct 2013, 08:51
Location: Germany

Re: Right click on GUI button --> Action

18 Apr 2020, 05:28

Code: Select all

Space::
this()
{
global Handles := [] ; <<<<< if you move the line into the function the variable isn't super-global any more
...
So either

Code: Select all

RButtonUp(w,l,msg,hwnd){
	Global Handles ; <<<<< added
	if(Handles[hwnd])
		gosub, % Handles[hwnd]
}
or

Code: Select all

#SingleInstance, Force
SetBatchLines, -1
Global Handles ; <<<<< added
Return
or both

or something completely different ;)
timoshina
Posts: 67
Joined: 06 Jun 2017, 12:45

Re: Right click on GUI button --> Action

21 Apr 2020, 00:57

Guys, thank you so much! I didn't expect to get 3 solutions, lol Really appreciated! :thumbup:

Return to “Ask for Help (v1)”

Who is online

Users browsing this forum: aitaixy, Anput, dangoscrub, doodles333, joedf, Nerafius and 106 guests