Button - how to detect left click and right click? Topic is solved

Get help with using AutoHotkey (v1.1 and older) and its commands and hotkeys
fliptoback
Posts: 84
Joined: 03 Sep 2016, 01:27

Button - how to detect left click and right click?

Post by fliptoback » 17 Nov 2020, 09:16

Hi,

I have a lot of buttons (these are program launchers) on the GUI. I would like to be able to detect if the user press the right button or left button so I can fire the right code behind.

I have done the following but it is not working:

Code: Select all

BtnShortcut:
Switch A_GuiEvent
{
case "RightClick":
{
msgbox, you have clicked right mouse button
}
case "LeftClick":
{
msgbox, you have clicked left mouse button
}
}
return
I have checked that A_GuiEvent returns the string "normal" when i press the mouse button. How do I detect the right/left click?

Thanks in advance

User avatar
TheDewd
Posts: 1513
Joined: 19 Dec 2013, 11:16
Location: USA

Re: Button - how to detect left click and right click?

Post by TheDewd » 17 Nov 2020, 09:23

Code: Select all

#SingleInstance, Force

Gui, Margin, 10, 10
Gui, Add, Button, w80 h24 HWNDhButton1, Button 1
Gui, Add, Button, w80 h24 HWNDhButton2, Button 2
Gui, Show, w400 h200, Example
return

~LButton::
	MouseGetPos, MouseX, MouseY, MouseWin, MouseCtl, 2
	
	If (MouseCtl = hButton1) {
		MsgBox, You left-clicked "Button 1"
	} Else If (MouseCtl = hButton2) {
		MsgBox, You left-clicked "Button 2"
	}
return

~RButton::
	MouseGetPos, MouseX, MouseY, MouseWin, MouseCtl, 2
	
	If (MouseCtl = hButton1) {
		MsgBox, You right-clicked "Button 1"
	} Else If (MouseCtl = hButton2) {
		MsgBox, You right-clicked "Button 2"
	}
return

~MButton::
	MouseGetPos, MouseX, MouseY, MouseWin, MouseCtl, 2
	
	If (MouseCtl = hButton1) {
		MsgBox, You middle-clicked "Button 1"
	} Else If (MouseCtl = hButton2) {
		MsgBox, You middle-clicked "Button 2"
	}
return

fliptoback
Posts: 84
Joined: 03 Sep 2016, 01:27

Re: Button - how to detect left click and right click?

Post by fliptoback » 17 Nov 2020, 09:27

TheDewd wrote:
17 Nov 2020, 09:23

Code: Select all

#SingleInstance, Force

Gui, Margin, 10, 10
Gui, Add, Button, w80 h24 HWNDhButton1, Button 1
Gui, Add, Button, w80 h24 HWNDhButton2, Button 2
Gui, Show, w400 h200, Example
return

~LButton::
	MouseGetPos, MouseX, MouseY, MouseWin, MouseCtl, 2
	
	If (MouseCtl = hButton1) {
		MsgBox, You left-clicked "Button 1"
	} Else If (MouseCtl = hButton2) {
		MsgBox, You left-clicked "Button 2"
	}
return

~RButton::
	MouseGetPos, MouseX, MouseY, MouseWin, MouseCtl, 2
	
	If (MouseCtl = hButton1) {
		MsgBox, You right-clicked "Button 1"
	} Else If (MouseCtl = hButton2) {
		MsgBox, You right-clicked "Button 2"
	}
return

~MButton::
	MouseGetPos, MouseX, MouseY, MouseWin, MouseCtl, 2
	
	If (MouseCtl = hButton1) {
		MsgBox, You middle-clicked "Button 1"
	} Else If (MouseCtl = hButton2) {
		MsgBox, You middle-clicked "Button 2"
	}
return
Wow! That works immediately! Thanks TheDewd!

I will try to adapt this code into mine.

Thanks again!

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

Re: Button - how to detect left click and right click?  Topic is solved

Post by mikeyww » 17 Nov 2020, 09:27

Just another flavor.... (But the other versions are better, as they are button-specific.)

Code: Select all

Gui, Font, s10
Gui, Add, Button,, OK
Gui, Show, w120, Test
Return

ButtonOK:
GuiContextMenu:
Switch A_GuiEvent {
 Case "Normal": MsgBox, You have activated the button.
 Case "RightClick": MsgBox, You have clicked the right mouse button.
}
Return

GuiEscape:
GuiClose:
ExitApp

fliptoback
Posts: 84
Joined: 03 Sep 2016, 01:27

Re: Button - how to detect left click and right click?

Post by fliptoback » 17 Nov 2020, 09:32

mikeyww wrote:
17 Nov 2020, 09:27
Just another flavor.... (But the other versions are better, as they are button-specific.)

Code: Select all

Gui, Font, s10
Gui, Add, Button,, OK
Gui, Show, w120, Test
Return

ButtonOK:
GuiContextMenu:
Switch A_GuiEvent {
 Case "Normal": MsgBox, You have activated the button.
 Case "RightClick": MsgBox, You have clicked the right mouse button.
}
Return

GuiEscape:
GuiClose:
ExitApp
Thanks Mikey. That is very similar to the original code I was having issues with.

So the trick is to add the GuiContextMenu!!

I will try that now!

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

Re: Button - how to detect left click and right click?

Post by mikeyww » 17 Nov 2020, 09:39

No problem, but:
Launched whenever the user right-clicks anywhere in the window except the title bar and menu bar. It is also launched in response to pressing the Menu key or Shift+F10.
The idea is that this would function as a context menu for the GUI, rather than as a context menu for the button alone.

The script shows the routines as combined, but you can also separate them if needed. You would not need the check for "Normal", because ButtonOK would be launched whenever the button is activated (clicked, or using ENTER or SPACE in some cases).

fliptoback
Posts: 84
Joined: 03 Sep 2016, 01:27

Re: Button - how to detect left click and right click?

Post by fliptoback » 17 Nov 2020, 09:45

mikeyww wrote:
17 Nov 2020, 09:39
No problem, but:
Launched whenever the user right-clicks anywhere in the window except the title bar and menu bar. It is also launched in response to pressing the Menu key or Shift+F10.
The idea is that this would function as a context menu for the GUI, rather than as a context menu for the button alone.

The script shows the routines as combined, but you can also separate them if needed. You would not need the check for "Normal", because ButtonOK would be launched whenever the button is activated (clicked, or using ENTER or SPACE in some cases).
Thanks Mikey. I think I get what you mean. I will experiment a bit with this contextmenu bit.

just me
Posts: 9529
Joined: 02 Oct 2013, 08:51
Location: Germany

Re: Button - how to detect left click and right click?

Post by just me » 17 Nov 2020, 10:11

GuiContextMenu:
... The following built-in variables are available within GuiContextMenu:
  1. A_GuiControl, which contains the text or variable name of the control that received the event (blank if none).

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

Re: Button - how to detect left click and right click?

Post by mikeyww » 17 Nov 2020, 10:40

That works, too. Thanks.

Code: Select all

Gui, Font, s10
Gui, Add, Button,, OK
Gui, Show, w120, Test
Return

ButtonOK:
MsgBox, 64, %A_GuiControl%, You have activated the button.
Return

GuiContextMenu:
If (A_GuiControl != "OK")
 Return
If (A_GuiEvent = "RightClick")
 MsgBox, 64, %A_GuiControl%, You have clicked the right mouse button on the "OK" button.
Else 
 MsgBox, 64, %A_GuiControl%, You have activated the context menu for the "OK" button.
Return

GuiEscape:
GuiClose:
ExitApp

User avatar
TheDewd
Posts: 1513
Joined: 19 Dec 2013, 11:16
Location: USA

Re: Button - how to detect left click and right click?

Post by TheDewd » 17 Nov 2020, 10:44

I like to use HWND instead of A_GuiControl because it becomes BLANK if your control has a variable.

Gui, Add, Button, vMyButton, OK

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

Re: Button - how to detect left click and right click?

Post by mikeyww » 17 Nov 2020, 10:54

Seems to work just fine.

Code: Select all

Gui, Font, s10
Gui, Add, Button, vMyButton, OK
Gui, Show, w120, Test
Return

ButtonOK:
MsgBox, 64, %A_GuiControl%, You have activated the button.
Return

GuiContextMenu:
If (A_GuiControl != "MyButton")
 Return
If (A_GuiEvent = "RightClick")
 MsgBox, 64, %A_GuiControl%, You have clicked the right mouse button on the "OK" button.
Else 
 MsgBox, 64, %A_GuiControl%, You have activated the context menu for the "OK" button.
Return

GuiEscape:
GuiClose:
ExitApp

User avatar
TheDewd
Posts: 1513
Joined: 19 Dec 2013, 11:16
Location: USA

Re: Button - how to detect left click and right click?

Post by TheDewd » 17 Nov 2020, 11:30

Please ignore my last reply... I see what I did now.

You used special labels for the button, such as ButtonOK.

I was trying to call A_GuiControl from ~LButton::

For Left-Click, I like to have a single Label or Function, instead of having to use ButtonOK, ButtonCancel, etc.

Normally I use WM_LBUTTONUP & WM_LBUTTONDOWN.

Post Reply

Return to “Ask for Help (v1)”