Right click on GUI not working Topic is solved

Get help with using AutoHotkey (v1.1 and older) and its commands and hotkeys
User avatar
zvit
Posts: 224
Joined: 07 Nov 2017, 06:15

Right click on GUI not working

Post by zvit » 24 May 2022, 07:43

@mikeyww posted a script here: viewtopic.php?t=82965 for right-clicking a GUI. His example works for me too.
However, when I try it on my own GUI, it doesn't work. Can someone please tell me what I'm getting wrong?

I tried three cases: Normal, RightClick, and DoubleClick. Only the normal case works.

I have a complex, transparent menu:

Image

But for the sake of this post, I stripped it down to bare minimum. Here's the code:

Code: Select all

Gui, 50:Add, Button, xm ym hWndhBtn12 gWriteDicOnly, % "Write Dictionary Only"
Gui, 50:Show
return

WriteDicOnly:
  Switch A_GuiEvent {
    ;Case "Normal": MsgBox, You have clicked the button.
    Case "RightClick": MsgBox, You have clicked the right mouse button.
    Case "DoubleClick": MsgBox, Double clicked!
  }
Return

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

Re: Right click on GUI not working

Post by mikeyww » 24 May 2022, 07:56

A ListView is not a button.

Code: Select all

Gui, 50:Add, Button,, Write Dictionary Only
Gui, 50:Show
OnMessage(0x0204, "WM_RBUTTONDOWN")
Return

WM_RBUTTONDOWN(wParam, lParam) {
 x := lParam & 0xFFFF, y := lParam >> 16
 (A_GuiControl) && Ctrl := "`n(in control " . A_GuiControl . ")"
 ToolTip You right-clicked in Gui window #%A_Gui% at client coordinates %x%x%y%.%Ctrl%
}
Explained: OnMessage

User avatar
zvit
Posts: 224
Joined: 07 Nov 2017, 06:15

Re: Right click on GUI not working

Post by zvit » 24 May 2022, 08:02

Okay, so you just write this stuff in your sleep? :lol: Thanks!

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

Re: Right click on GUI not working

Post by mikeyww » 24 May 2022, 08:10

No, I stole it from the AHK documentation! :)

User avatar
zvit
Posts: 224
Joined: 07 Nov 2017, 06:15

Re: Right click on GUI not working

Post by zvit » 24 May 2022, 10:44

How do I get a right-click to do something per label? I tried adding an argument to the function, but it didn't work:

Code: Select all

Gui, 50:Add, Button, xm ym gWriteDicOnly, % "Write Dictionary Only"
Gui, 50:Add, Button, xp y+10 gTest, % "Test"
Gui, 50:Show
return

WriteDicOnly:
  OnMessage(0x0204, "WM_RBUTTONDOWN", "WriteDicOnly")
Return

Test:
  OnMessage(0x0204, "WM_RBUTTONDOWN", "Test")
Return

WM_RBUTTONDOWN(wParam, lParam, label) {
  x := lParam & 0xFFFF, y := lParam >> 16
  (A_GuiControl) && Ctrl := "`n(in control " . A_GuiControl . ")"

  Switch label{
    Case "WriteDicOnly": msgbox % "here: WriteDicOnly"
    Case "Test": msgbox % "here: Test"
  }
}

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

Re: Right click on GUI not working  Topic is solved

Post by mikeyww » 24 May 2022, 20:08

Code: Select all

Gui, 50:Add, Button,, % "Write Dictionary Only"
Gui, 50:Add, Button,, % "Test"
Gui, 50:Show
OnMessage(0x0204, "WM_RBUTTONDOWN")
Return

WM_RBUTTONDOWN(wParam, lParam) {
 Switch A_GuiControl {
  Case "Write Dictionary Only": MsgBox, % "here: WriteDicOnly"
  Case "Test"                 : MsgBox, % "here: Test"
 }
}
You can use A_ThisLabel when needed. See the documentation.
Last edited by mikeyww on 24 May 2022, 20:32, edited 1 time in total.

User avatar
zvit
Posts: 224
Joined: 07 Nov 2017, 06:15

Re: Right click on GUI not working

Post by zvit » 24 May 2022, 20:25

Got it. I also read that you can use IsLabel(). Thanks!

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

Re: Right click on GUI not working

Post by mikeyww » 24 May 2022, 20:26

Although IsLabel is a function, it is not identical to A_ThisLabel. I edited the function, removing the label parameter.

User avatar
zvit
Posts: 224
Joined: 07 Nov 2017, 06:15

Re: Right click on GUI not working

Post by zvit » 24 May 2022, 21:00

Yes, I understood you forgot to remove the extra parameter.
Ahh, I see... IsLabel() only tells you if the label exists in the script.

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

Re: Right click on GUI not working

Post by mikeyww » 24 May 2022, 21:28

Yes. :thumbup:

User avatar
zvit
Posts: 224
Joined: 07 Nov 2017, 06:15

Re: Right click on GUI not working

Post by zvit » 25 May 2022, 03:35

Tested. Yours works. But when I add actual labels, only the second Case (test) works. Here's my code:

Code: Select all

Gui, 50:Add, Button, xm ym gWriteDicOnly, % "Write Dictionary Only"
Gui, 50:Add, Button, xp y+10 gTest, % "Test"
Gui, 50:Show
OnMessage(0x0204, "WM_RBUTTONDOWN")
return

WriteDicOnly:
;RunSomeCode()
Return

Test:
;RunSomeCode()
Return

WM_RBUTTONDOWN(wParam, lParam) {
  x := lParam & 0xFFFF, y := lParam >> 16
  (A_GuiControl) && Ctrl := "`n(in control " . A_GuiControl . ")"

  Switch A_GuiControl{
    Case "WriteDicOnly": msgbox % "here: WriteDicOnly"
    Case "Test": msgbox % "here: Test"
  }
}
I tried: Case gWriteDicOnly: and Case gWriteDicOnly: but it didn't help.

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

Re: Right click on GUI not working

Post by mikeyww » 25 May 2022, 05:01

To find your bug, display the A_GuiControl inside the function.

User avatar
zvit
Posts: 224
Joined: 07 Nov 2017, 06:15

Re: Right click on GUI not working

Post by zvit » 25 May 2022, 05:20

Yes, of course! It shows that it's looking for the label text and not the label name. Works now. Thanks!

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

Re: Right click on GUI not working

Post by mikeyww » 25 May 2022, 05:37

A_GuiControl:
The name of the variable associated with the GUI control that launched the current thread. If that control lacks an associated variable, A_GuiControl instead contains the first 63 characters of the control's text/caption (this is most often used to avoid giving each button a variable name). A_GuiControl is blank whenever: 1) A_Gui is blank; 2) a GUI menu bar item or event such as GuiClose/GuiEscape launched the current thread; 3) the control lacks an associated variable and has no caption; or 4) The control that originally launched the current thread no longer exists (perhaps due to Gui Destroy).

Post Reply

Return to “Ask for Help (v1)”