Page 1 of 1

Button - how to detect left click and right click?

Posted: 17 Nov 2020, 09:16
by fliptoback
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

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

Posted: 17 Nov 2020, 09:23
by TheDewd

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

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

Posted: 17 Nov 2020, 09:27
by fliptoback
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!

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

Posted: 17 Nov 2020, 09:27
by mikeyww
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

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

Posted: 17 Nov 2020, 09:32
by fliptoback
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!

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

Posted: 17 Nov 2020, 09:39
by mikeyww
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).

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

Posted: 17 Nov 2020, 09:45
by fliptoback
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.

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

Posted: 17 Nov 2020, 10:11
by just me
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).

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

Posted: 17 Nov 2020, 10:40
by mikeyww
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

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

Posted: 17 Nov 2020, 10:44
by TheDewd
I like to use HWND instead of A_GuiControl because it becomes BLANK if your control has a variable.

Gui, Add, Button, vMyButton, OK

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

Posted: 17 Nov 2020, 10:54
by mikeyww
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

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

Posted: 17 Nov 2020, 11:30
by TheDewd
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.