Lee Bussy

Check for Right Click?

15 posts in this topic

#1 ·  Posted (edited)

Hello there ... my current project has me a little stumped. I'd like to take an action if someone right-clicks on a button. I am polling for button clicks currently with the following function:

Func CheckEvents()
; check for button click
    $nMsg = GUIGetMsg()
    Select
        Case $nMsg = $GUI_EVENT_CLOSE
            Exit
        Case $nMsg = $btnOne
            Return "btnOne"
        Case $nMsg = $btnTwo
            Return "btnTwo"
        Case $nMsg = $btnThree
            Return "btnThree"
        Case $nMsg = $btnFour
            Return "btnFour"
    EndSelect
    Return ""
EndFunc

That works of course but only on left clicks. Is it possible to capture a right-click? I saw the context menu option but that's not what I want ... I need to return a value if there's a right click.

EDIT: The function GUICtrlSetOnEvent() looks like what I want, except I want it on a control not on the GUI.

EDIT #2: Hrm, found GUI Reference - OnEvent Mode .... researching that now :-)

Edited by Lee Bussy

Share this post


Link to post
Share on other sites



Okay I tried ... the best i can come up with is this kludgey bit of "art":

#include <GUIConstantsEx.au3>

Opt("MouseCoordMode", 2)
Opt("PixelCoordMode", 2)
Opt("GUIOnEventMode", 1) ; Change to OnEvent mode

$mainwindow = GUICreate("", 200, 100)
GUISetOnEvent($GUI_EVENT_CLOSE, "CLOSEClicked")
$okbutton = GUICtrlCreateButton("OK", 70, 50, 60)
GUISetState(@SW_SHOW)
GUISetOnEvent($GUI_EVENT_SECONDARYDOWN, "RightClick")

While 1
  Sleep(1000)
WEnd

Func CLOSEClicked()
  Exit
EndFunc

Func RightClick()
    $pos = MouseGetPos()
;MsgBox(0, "Mouse x,y:", $pos[0] & "," & $pos[1])
    If $pos[0] > 70 And $pos[0] < 130 And $pos[1] > 50 And $pos[1] < 75 Then
        MsgBox(0, "GUI Event", "You right clicked")
    EndIf
EndFunc

Is there a better way?

Share this post


Link to post
Share on other sites

If _IsPressed(02) Then
     MsgBox(0, "Test", "You right clicked")
EndIf

Share this post


Link to post
Share on other sites

If _IsPressed(02) Then
     MsgBox(0, "Test", "You right clicked")
EndIf
Thanks for the reply KentonBomb. I think maybe I'm missing something or not describing my need correctly. That bit of code should show if the mouse is right-clicked however there's still no way to limit that to the control I wish to check. Right clicking on a control will not "trigger" a control event unless I'm missing something simple. With _IsPressed(02) I'd still need to check location, would I not?

Share this post


Link to post
Share on other sites

This is exactly what you need:

#include <Misc.au3>

GUICreate("Hi", 200, 200)
GUICtrlCreateLabel("Right click the folowing button!", 20, 20)
GUICtrlCreateButton("Hello", 80, 100, 50, 50)
GUISetState(@SW_SHOW)

While 1
    $pos = MouseGetPos()
    If _IsPressed(02) And 495 < $pos[0] And $pos[0] < 540 And 385 < $pos[1] And $pos[1] < 430 Then
        MsgBox(0, "Test", "Oh yes!")
    EndIf
    Sleep(100)
WEnd

Replace what's left to "<" sign with your actual button coordinates. If the button doesn't stay in the same place, use client (your GUI in this case) coords.

Share this post


Link to post
Share on other sites

#6 ·  Posted (edited)

Lee Bussy

Example:

#include <GuiConstantsEx.au3>
#include <ButtonConstants.au3>
#include <WindowsConstants.au3>
#include <Misc.au3>
#include <WinAPI.au3>

Global $DllHandle = DllOpen("user32.dll")

$hGui = GUICreate("Double Click Demo", 200, 100)

$button = GUICtrlCreateButton("start", 60, 35, 75, 23, $BS_NOTIFY)

$aBtnPos = ControlGetPos($hGui, "", $button)

GUIRegisterMsg($WM_COMMAND, "MY_WM_COMMAND")

GUISetState()

While GUIGetMsg() <> -3
    If _IsPressed("02", $DllHandle) Then
        $tPoint = _WinAPI_GetMousePos(True, $hGui)
        
        $X = DllStructGetData($tPoint, "X")
        $Y = DllStructGetData($tPoint, "Y")
        
        If ($X >= $aBtnPos[0]) And ($X <= $aBtnPos[0] + $aBtnPos[2]) _
            And ($Y >= $aBtnPos[1]) And ($Y <= $aBtnPos[1] + $aBtnPos[3]) Then _DebugPrint("Right mouse click", @ScriptLineNumber)
            Sleep(100)
    EndIf   
WEnd

DllClose($DllHandle)

Func MY_WM_COMMAND($hWnd, $Msg, $wParam, $lParam)
    Local $nNotifyCode = BitShift($wParam, 16)       ;HiWord
    Local $nID         = BitAnd($wParam, 0x0000FFFF) ;LoWord
    
    If $nID = $button Then
        Switch $nNotifyCode
        Case $BN_CLICKED
            _DebugPrint("Left mouse click", @ScriptLineNumber)
        Case $BN_DBLCLK
            _DebugPrint("Left mouse double click", @ScriptLineNumber)
        EndSwitch
    EndIf
    Return $GUI_RUNDEFMSG
EndFunc

Func _DebugPrint($s_text, $line = @ScriptLineNumber)
    ConsoleWrite("+=====================================" & @LF & _
                "--> Script Line (" & $line & "):" & @LF & "!" & @TAB & $s_text & @LF & _
                "+=====================================" & @LF)
EndFunc
Edited by rasim

Share this post


Link to post
Share on other sites

So basically there is no event generated by a control when you right click - it's all done at the form/GUI level and then I have to check the coordinates of the click. Not as I had hoped, but I can handle it now that I know.

Thanks for the replies and showing me more than one way to skin a cat. Should come along swimmingly now.

Share this post


Link to post
Share on other sites

Well 7 Years passed, i tried to find a solution in google for this problem but still nothing simple...

If only it would be 

_IsPressed(02, $controlName)

=(. Anyone knows the easy way without use coordinates ?

1 person likes this

Share this post


Link to post
Share on other sites

UritOR

Welcome to the AutoIt forums.

In future please do not resurrect threads from this far back - the language has changed so much that it is likely that the functionality has already been incorporated and the previously posted code will almost certainly not run in the current version of AutoIt without modification.

And does this help?

#include <GUIConstantsEx.au3>
#include <MsgBoxConstants.au3>

$hGUI = GUICreate("Test", 500, 500)

$cButton = GUICtrlCreateButton("Test", 10, 10, 80, 30)

GUISetState()

While 1

    Switch GUIGetMsg()
        Case $GUI_EVENT_CLOSE
            Exit
        Case $cButton

            MsgBox($MB_SYSTEMMODAL, "Pressed", "LEFT")
        Case $GUI_EVENT_SECONDARYUP
            $aCInfo = GUIGetCursorInfo($hGUI)
            If $aCInfo[4] = $cButton Then
                MsgBox($MB_SYSTEMMODAL, "Pressed", "RIGHT")
            EndIf

    EndSwitch

WEnd

M23

1 person likes this

Any of my own code posted anywhere on the forum is available for use by others without any restriction of any kind._______My UDFs:

Spoiler

ArrayMultiColSort ---- Sort arrays on multiple columns
ChooseFileFolder ---- Single and multiple selections from specified path treeview listing
Date_Time_Convert -- Easily convert date/time formats, including the language used
ExtMsgBox --------- A highly customisable replacement for MsgBox
GUIExtender -------- Extend and retract multiple sections within a GUI
GUIFrame ---------- Subdivide GUIs into many adjustable frames
GUIListViewEx ------- Insert, delete, move, drag, sort, edit and colour ListView items
GUITreeViewEx ------ Check/clear parent and child checkboxes in a TreeView
Marquee ----------- Scrolling tickertape GUIs
NoFocusLines ------- Remove the dotted focus lines from buttons, sliders, radios and checkboxes
Notify ------------- Small notifications on the edge of the display
Scrollbars ----------Automatically sized scrollbars with a single command
StringSize ---------- Automatically size controls to fit text
Toast -------------- Small GUIs which pop out of the notification area

 

Share this post


Link to post
Share on other sites

UritOR,

Of course:

#include <GUIConstantsEx.au3>
#include <MsgBoxConstants.au3>

Opt("GUIOnEventMode", 1)

$hGUI = GUICreate("Test", 500, 500)
GUISetOnEvent($GUI_EVENT_CLOSE, "_Exit")
GUISetOnEvent($GUI_EVENT_SECONDARYUP, "_SecondaryUp")

$cButton = GUICtrlCreateButton("Test", 10, 10, 80, 30)
GUICtrlSetOnEvent($cButton, "_Button")

GUISetState()

While 1
    Sleep(10)
WEnd



Func _Button()
    MsgBox($MB_SYSTEMMODAL, "Pressed", "LEFT")
EndFunc



Func _SecondaryUp()
    $aCInfo = GUIGetCursorInfo($hGUI)
    If $aCInfo[4] = $cButton Then
        MsgBox($MB_SYSTEMMODAL, "Pressed", "RIGHT")
    EndIf

EndFunc



Func _Exit()
    Exit
EndFunc

M23


Any of my own code posted anywhere on the forum is available for use by others without any restriction of any kind._______My UDFs:

Spoiler

ArrayMultiColSort ---- Sort arrays on multiple columns
ChooseFileFolder ---- Single and multiple selections from specified path treeview listing
Date_Time_Convert -- Easily convert date/time formats, including the language used
ExtMsgBox --------- A highly customisable replacement for MsgBox
GUIExtender -------- Extend and retract multiple sections within a GUI
GUIFrame ---------- Subdivide GUIs into many adjustable frames
GUIListViewEx ------- Insert, delete, move, drag, sort, edit and colour ListView items
GUITreeViewEx ------ Check/clear parent and child checkboxes in a TreeView
Marquee ----------- Scrolling tickertape GUIs
NoFocusLines ------- Remove the dotted focus lines from buttons, sliders, radios and checkboxes
Notify ------------- Small notifications on the edge of the display
Scrollbars ----------Automatically sized scrollbars with a single command
StringSize ---------- Automatically size controls to fit text
Toast -------------- Small GUIs which pop out of the notification area

 

Share this post


Link to post
Share on other sites

Thank you. ^_^

Share this post


Link to post
Share on other sites

At last i found it. Tks Melba23 !

Share this post


Link to post
Share on other sites

#14 ·  Posted (edited)

On 3/3/2016 at 5:29 AM, hiepkhachsg said:

At last i found it. Tks Melba23 !

That does not help to get GUICtrlRead(@GUI_CtrlId) of right clicked button while you can get it left clicked. :)

not  a real solution to button press.

#include <GUIConstantsEx.au3>
#include <MsgBoxConstants.au3>

Opt("GUIOnEventMode", 1)

$hGUI = GUICreate("Test", 500, 500)
GUISetOnEvent($GUI_EVENT_CLOSE, "_Exit")
GUISetOnEvent($GUI_EVENT_SECONDARYUP, "_SecondaryUp")

$cButton = GUICtrlCreateButton("Test", 10, 10, 80, 30)
GUICtrlSetOnEvent($cButton, "_Button")
$cButton2 = GUICtrlCreateButton("Testz", 10, 50, 80, 30)
GUICtrlSetOnEvent($cButton2, "_Button")
GUISetState()

While 1
    Sleep(10)
WEnd 
Func _Button()
    $nTempId=GUICtrlRead(@GUI_CtrlId)
    $aCInfo = GUIGetCursorInfo($hGUI)
    MsgBox($MB_SYSTEMMODAL, "Pressed",  "LEFT"&$nTempId&$aCInfo)
EndFunc 
Func _SecondaryUp()
    $aCInfo = GUIGetCursorInfo($hGUI)
    If $aCInfo[4] = $cButton Then
    $nTempId=GUICtrlRead(@GUI_CtrlId) ;cannot get 
        MsgBox($MB_SYSTEMMODAL, "Pressed", "RIGHT"&$nTempId&$aCInfo)
    EndIf
    If $aCInfo[4] = $cButton2 Then
    $nTempId=GUICtrlRead(@GUI_CtrlId) ;cannot get 
        MsgBox($MB_SYSTEMMODAL, "Pressed", "RIGHT2"&$nTempId&$aCInfo)
    EndIf

EndFunc 
Func _Exit()
    Exit
EndFunc

very bad solution is

for $x=1 to 3
        If $aCInfo[4] = $cButton[$x] Then
            $nTempId=GUICtrlRead(@GUI_CtrlId) ;cannot get 
            $nTempId2=GUICtrlRead($cButton[$x]) ;can get            
            MsgBox($MB_SYSTEMMODAL, "Pressed", "RIGHT"&$nTempId&"::"&$nTempId2)
        EndIf
    next

 

Edited by guiltyking
case study

Share this post


Link to post
Share on other sites

guiltyking,

As the @GUI_CtrlId macro just holds the ControlID of the last actioned control, why can you not use the $aCInfo[4] value directly?

#include <GUIConstantsEx.au3>
#include <MsgBoxConstants.au3>

Opt("GUIOnEventMode", 1)

$hGUI = GUICreate("Test", 500, 500)
GUISetOnEvent($GUI_EVENT_CLOSE, "_Exit")
GUISetOnEvent($GUI_EVENT_SECONDARYUP, "_SecondaryUp")

$cButton = GUICtrlCreateButton("Test", 10, 10, 80, 30)
GUICtrlSetOnEvent($cButton, "_Button")
$cButton2 = GUICtrlCreateButton("Testz", 10, 50, 80, 30)
GUICtrlSetOnEvent($cButton2, "_Button")
GUISetState()

While 1
    Sleep(10)
WEnd

Func _Button()
    $nTempId = GUICtrlRead(@GUI_CtrlId)
    MsgBox($MB_SYSTEMMODAL, "Pressed", "LEFT " & $nTempId)
EndFunc   ;==>_Button

Func _SecondaryUp()
    $aCInfo = GUIGetCursorInfo($hGUI)
    Switch $aCInfo[4]
        Case $cButton
            $nTempId = GUICtrlRead($cButton) ;can get
            MsgBox($MB_SYSTEMMODAL, "Pressed", "RIGHT " & $nTempId)
        Case $cButton2
            $nTempId = GUICtrlRead($cButton2) ;can get
            MsgBox($MB_SYSTEMMODAL, "Pressed", "RIGHT2 " & $nTempId)
    EndSwitch

EndFunc   ;==>_SecondaryUp

Func _Exit()
    Exit
EndFunc   ;==>_Exit

M23


Any of my own code posted anywhere on the forum is available for use by others without any restriction of any kind._______My UDFs:

Spoiler

ArrayMultiColSort ---- Sort arrays on multiple columns
ChooseFileFolder ---- Single and multiple selections from specified path treeview listing
Date_Time_Convert -- Easily convert date/time formats, including the language used
ExtMsgBox --------- A highly customisable replacement for MsgBox
GUIExtender -------- Extend and retract multiple sections within a GUI
GUIFrame ---------- Subdivide GUIs into many adjustable frames
GUIListViewEx ------- Insert, delete, move, drag, sort, edit and colour ListView items
GUITreeViewEx ------ Check/clear parent and child checkboxes in a TreeView
Marquee ----------- Scrolling tickertape GUIs
NoFocusLines ------- Remove the dotted focus lines from buttons, sliders, radios and checkboxes
Notify ------------- Small notifications on the edge of the display
Scrollbars ----------Automatically sized scrollbars with a single command
StringSize ---------- Automatically size controls to fit text
Toast -------------- Small GUIs which pop out of the notification area

 

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!


Register a new account

Sign in

Already have an account? Sign in here.


Sign In Now