Jump to content

Double click button problem


Champak
 Share

Recommended Posts

I need a GUI button to register both single and double click "properly". Is this possible? I've tried making my own and I've searched and I haven't come up with one that suits my needs. The problem I've confronted with both my efforts and the ones I've found is they always register the first click AND THEN the double click, which essentially renders the option of single and double click useless. I've also tried having the button send single/double click to a hotkey set, but that doesn't work either. I know how to do this with a double hotkey press, and tried to convert that, but it didn't work.

A conceptual thought that came across my mind is to "capture and count" the GuiGetMsg of a button click, which I believe is the best way if it is possible. Essentially when a button is pressed the Case will do a Do..Until a certain time has passed, like 1.5 seconds, and in the Do...Until it is capturing the count that $Button1 is being pressed. When the until is reached there is an IF statement that will do whatever based on the count that was captured in the Do...Until.

Concept example:

Global $iTimer

While 1
    $msg = GuiGetMsg
        Select
            Case $msg = $Button
                If $iTimer = "" Or TimerDiff($iTimer) =< 1000
                    $iTimer = TimerInit()
                    Do

                    $count = GuiGetMsg=$Button;A function that will count how many times $Button is pressed

                    Until TimerDiff($iTimer) >= 1000
                    If $count = 1 Then Function1()
                    If $count = 2 Then Function2()
                EndIf
        EndSelect
WEnd

The TimerDiff check at the very start should take care of the button registering twice from the double click.

SO, possible or not?

Edited by Champak
Link to comment
Share on other sites

So not possible?

Search for NM_DBLCLK

Serial port communications UDF Includes functions for binary transmission and reception.printing UDF Useful for graphs, forms, labels, reports etc.Add User Call Tips to SciTE for functions in UDFs not included with AutoIt and for your own scripts.Functions with parameters in OnEvent mode and for Hot Keys One function replaces GuiSetOnEvent, GuiCtrlSetOnEvent and HotKeySet.UDF IsConnected2 for notification of status of connected state of many urls or IPs, without slowing the script.
Link to comment
Share on other sites

#include <GUIConstants.au3>
$handle = GUICreate("", 100, 100)
$Button= GUICtrlCreateButton ("Run Notepad",0, 0, 100,100,$BS_MULTILINE,BitOR($WS_EX_CLIENTEDGE,$WS_EX_STATICEDGE))

GUISetState(@SW_SHOW)

Global $iTimer 
Dim $CHK = 1 
While 1
    $arrayInfo = GUIGetCursorInfo($handle)
    if $arrayInfo[4] = $Button AND $arrayInfo[2] = 1 THEN $CHK = Function1($arrayInfo,500)
    IF $CHK = 0 THEN Function2()
    Dim $CHK = 1
    $msg = GuiGetMsg()
    Select
    Case $msg = $GUI_EVENT_CLOSE 
        ExitLoop
        EndSelect
WEnd
Exit

Func Function2()
Run("Notepad.exe", "", @SW_MAXIMIZE)
EndFunc
Func Function1($arrayInfo,$iTimer)
  
    While 1
    $arrayInfo = GUIGetCursorInfo($handle)
    IF $arrayInfo[4] = $Button AND $arrayInfo[2] = 0 THEN ExitLoop
    WEND 
    
    $begin = TimerInit()
    
    While 1
    $arrayInfo = GUIGetCursorInfo($handle)
    IF $arrayInfo[4] = $Button AND $arrayInfo[2] = 1 THEN ExitLoop
    IF TimerDiff($begin) >= $iTimer THEN Return 1
    WEnd
    
    While 1
    $arrayInfo = GUIGetCursorInfo($handle)
    IF $arrayInfo[4] = $Button AND $arrayInfo[2] = 0 THEN Return 0
    IF TimerDiff($begin) >= $iTimer THEN Return 1
    WEND 
EndFunc

OR

#include <GUIConstants.au3>
$handle = GUICreate("", 100, 100)
$Button= GUICtrlCreateButton ("Run Notepad",0, 0, 100,100,$BS_MULTILINE,BitOR($WS_EX_CLIENTEDGE,$WS_EX_STATICEDGE))

GUISetState(@SW_SHOW)

Global $iTimer 
Dim $CHK = 1 
While 1
    $arrayInfo = GUIGetCursorInfo($handle)
    if $arrayInfo[4] = $Button AND $arrayInfo[2] = 1 THEN $CHK = Function1($Button,$arrayInfo,500)
    IF $CHK = 0 THEN Function2()
    Dim $CHK = 1
    $msg = GuiGetMsg()
    Select
    Case $msg = $GUI_EVENT_CLOSE 
        ExitLoop
        EndSelect
WEnd
Exit

Func Function2()
Run("Notepad.exe", "", @SW_MAXIMIZE)
EndFunc
Func Function1($ButtonX,$arrayInfo,$iTimer)
  
    While 1
    $arrayInfo = GUIGetCursorInfo($handle)
    IF $arrayInfo[4] = $ButtonX AND $arrayInfo[2] = 0 THEN ExitLoop
    WEND 
    
    $begin = TimerInit()
    
    While 1
    $arrayInfo = GUIGetCursorInfo($handle)
    IF $arrayInfo[4] = $ButtonX AND $arrayInfo[2] = 1 THEN ExitLoop
    IF TimerDiff($begin) >= $iTimer THEN Return 1
    WEnd
    
    While 1
    $arrayInfo = GUIGetCursorInfo($handle)
    IF $arrayInfo[4] = $ButtonX AND $arrayInfo[2] = 0 THEN Return 0
    IF TimerDiff($begin) >= $iTimer THEN Return 1
    WEND 
EndFunc
Edited by wolf9228

صرح السماء كان هنا

 

Link to comment
Share on other sites

martin, Hi, I found NM_DBLCLK examples already. As stated in my first post, they function fine when you ONLY want to double click the button, but they still activate the function of the first click. wolf9228, thanks for the example, but the same problem still comes up...if you double click, it still activates the function of the first click...however, it is more elegant than the coding that I've put together. I need something that when you double click, it does not...even by accident, activate the function of the first click, and the button also does something when you click it once.

The button basically needs to prevent the function of the first click until a certain amount of time has passed where it doesn't receive a second click; if a second click happens within a certain period (300ms) it does the second click function instead of the first. OR do something like my theoretical example in post 1.

Link to comment
Share on other sites

I had to sit down and wonder alittle of how to do this, but I found a working solution:

#include <GUIConstants.au3>

Dim $iTimer = 0, $iClickCount = 0, $CheckTime = 300

$Form1 = GUICreate("Form1", 127, 57, 193, 115)
$Button1 = GUICtrlCreateButton("Click Me", 8, 8, 105, 33, 0)
GUISetState(@SW_SHOW)

While 1
    Switch GUIGetMsg()
        Case $GUI_EVENT_CLOSE
            Exit
        Case $Button1
            _HandleButton()
    EndSwitch
WEnd


Func _HandleButton()
    
    $iClickCount += 1
    $iTimer = TimerInit()
    
    AdlibEnable("CheckButtonPress", $CheckTime)
    
EndFunc

Func CheckButtonPress()
    
    If TimerDiff($iTimer) < $CheckTime Or $iTimer = 0 Then Return
    
    Switch $iClickCount
        Case 1
            MsgBox(0, "Clicked", "You clicked 1 times")
        Case 2
            MsgBox(0, "Clicked", "You clicked 2 times")
    EndSwitch
    
    $iClickCount = 0
    AdlibDisable()
    
EndFunc
Edited by FreeFry
Link to comment
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
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...