Jump to content
PoojaKrishna

[SOLVED] Detect Double Click

Recommended Posts

PoojaKrishna

Hi friends,

Is there any way to detect the left double clicks like we get the primary clicks  using _IsPressed("01").

We can detect double clicks by initiating a timer and finding the delay between left clicks on same coordinates but still want to if there is a better direct way.

Thank you friends.

 

#Include <Misc.au3> ; Used for the _IsPressed
 HotKeySet("{ESC}", "_ExitScript") ;Hot key Esc to Exit script
 $nCountClick = 0 ;Click Count
 While 1 ; Loop until user presses ESc to exit the Recorder
        If _IsPressed("01") then ; If left mouse is pushed.
            While (_IsPressed("01")) ;Wait until user releases button
                Sleep(100)
            WEnd
            $pos = MouseGetPos() ; Get the position.
            $nCountClick = $nCountClick + 1 ;Add click count
            ToolTip($nCountClick & ") " & 'Left Clicked X: ' & $pos[0] & ', Y: ' & $pos[1]) ;Show the click count with position
        EndIf
 WEnd

Func _ExitScript() ;HotKet method to Exit script
    Exit
EndFunc ;=> _ExitScript

 

Share this post


Link to post
Share on other sites
PoojaKrishna
39 minutes ago, DynamicRookie said:
If _IsPressed('01') Then
     $iPos = MouseGetPos()
     While 1
     If _IsPressed('01') Then
         $iCheck = MouseGetPos()
         If $iCheck[0] = $iPos[0] And $iCheck[1] = $iPos[1] Then
             MsgBox(0,'','Double Clicked'
         Endif
     EndIf
     WEnd
Endif

No shorter way i know of.

 

Sorry, your code didn't work for me.

The code below works and counts all single clicks and double clicks but it counts the single clicks that comes as part of the double clicks too.

#Include <Misc.au3> ; Used for the _IsPressed
 HotKeySet("{ESC}", "_ExitScript") ;Hot key Esc to Exit script
 $nCountSingleClick = 0 ;Left click count
 $nCountDoubleClick = 0 ;Double Click count
 $PreX = 0 ;Previous X
 $PreY = 0 ;Previous Y
 $hTimer = TimerInit() ;Start timer
 While 1 ; Loop until user presses ESc to exit the Recorder
        If _IsPressed("01") then ; If left mouse is pushed.
            While (_IsPressed("01")) ;Wait until user releases button
                ;Sleep(100)
            WEnd
            $pos = MouseGetPos() ; Get the position.
            $nTimeDelay = Int(TimerDiff($hTimer)) + 1 ;Get the delay in milli seconds
            $hTimer = TimerInit() ;Start the timer again
            If $PreX = $pos[0] and $PreY = $pos[1]  and $nTimeDelay < 500 Then  ;If two consecutive clicks are made on the same position with a delay leass than 500 ms
                $nCountDoubleClick = $nCountDoubleClick + 1
                ToolTip ($nCountDoubleClick & ") " & "Double Click X: " & $pos[0] & ', Y: ' & $pos[1]) ;Double click
            Else
                $nCountSingleClick = $nCountSingleClick + 1
                ToolTip($nCountSingleClick & ") " & 'Left Clicked X: ' & $pos[0] & ', Y: ' & $pos[1])
                $PreX = $pos[0]
                $PreY = $pos[1]
            EndIf
        EndIf
 WEnd
Func _ExitScript()
    Exit
EndFunc ;=> _ExitScript

 

Share this post


Link to post
Share on other sites
bernd670
Posted (edited)

Hello,

try this

Spoiler
#include <Misc.au3>

HotKeySet("{ESC}", "_ExitScript")

Const $maxDCT = 500 ; max. Doubleclicktime (ms)

Local $hDLL = DllOpen("user32.dll")
Local $LCTimer = TimerInit() ; Leftclick Timer

While True
    If _IsPressed("01", $hDLL) Then checkLeftClick()
WEnd

DllClose($hDLL)


Func checkLeftClick()
    Local $bDC = False ; DoubleClick?

    Local $ClickDelay = TimerInit()
    Local $PrePos = MouseGetPos()

    While (_IsPressed("01", $hDLL)) ; Wait until user releases button
    WEnd

    While (TimerDiff($ClickDelay) < $maxDCT)

        If _IsPressed("01", $hDLL) Then
            Local $Pos = MouseGetPos()

            If ((TimerDiff($ClickDelay) < $maxDCT) And ($PrePos[0] = $Pos[0]) And ($PrePos[1] = $Pos[1])) Then
                $bDC = True
                ExitLoop
            EndIf
        EndIf
    WEnd

    While (_IsPressed("01", $hDLL)) ; Wait until user releases button
    WEnd

    If $bDC Then
        ConsoleWrite("Doubleclick (left," & $PrePos[0] & "," & $PrePos[1] & ")" & @CRLF)
    Else
        ConsoleWrite("Click (left," & $PrePos[0] & "," & $PrePos[1] & ")" & @CRLF)
    EndIf

    Return $bDC
EndFunc   ;==>checkLeftClick

Func _ExitScript()
    Exit
EndFunc   ;==>_ExitScript

 

 

Edited by bernd670
  • Like 1

greetings
bernd


I hacked 127.0.0.1 -> pcfred6.gif

Share this post


Link to post
Share on other sites
Deye
Posted (edited)

Here is another one for you  to try :

#include <Misc.au3> ; Used for the _IsPressed
HotKeySet("{ESC}", "_ExitScript") ;Hot key Esc to Exit script

$nCountSingleClick = 0 ;Left click count
$nCountDoubleClick = 0 ;Double Click count

$hDLL = DllOpen("user32.dll")
OnAutoItExitRegister('_DllClose')

$Clicks = 0
$DblClickSpeed = 200

While 1 ; Loop until user presses ESc to exit the Recorder
    If _IsPressed("01") Then ; If left mouse is pushed.
        $hTimer = TimerInit() ;Start the timer again
        $Clicks += 1
        $pos = MouseGetPos() ; Get the position.
        If $Clicks >= 2 Then
            $Clicks = 0
            $nCountDoubleClick += 1
            ToolTip($nCountDoubleClick & ") " & "Double Click X: " & $pos[0] & ', Y: ' & $pos[1]) ;Double click
        EndIf

        While (_IsPressed("01")) ;Wait until user releases button
            Sleep(10)
        WEnd
    EndIf

    If $Clicks And Int(TimerDiff($hTimer)) > $DblClickSpeed Then
        $Clicks = 0
        $nCountSingleClick += 1
        ToolTip($nCountSingleClick & ") " & 'Single Click X: ' & $pos[0] & ', Y: ' & $pos[1])
    EndIf
WEnd

Func _ExitScript()
    Exit
EndFunc   ;==>_ExitScript

Func _DllClose()
    DllClose($hDLL)
EndFunc   ;==>_DllClose

 

Edited by Deye
  • Like 1

Share this post


Link to post
Share on other sites
PoojaKrishna

@bernd670 and @Deye ,

Thank you so much. Both the scripts work perfect.

Share this post


Link to post
Share on other sites
PoojaKrishna

Hi,

Here comes my version with detecting any number of continuous short clicks on a same position. 

Thank you so much for your help friends :-)

#Include <Misc.au3> ; Used for the _IsPressed
HotKeySet("{ESC}", "_ExitScript") ;Press Ecs key to Exit script
Local $hDLL = DllOpen("user32.dll") ; Dll as calling _IsPressed function repeatedly
While 1
    $nLeftclicks = 0 ;Initialize left click count on each iteration
    If _IsPressed("01", $hDLL) Then ;If left mouse button pressed
        $Pos = MouseGetPos() ;Get mouse position
        Do
            While (_IsPressed("01", $hDLL)) ;Wait until user releases button
                Sleep(10)
            WEnd
            $nLeftclicks += 1 ;Increment click count
            sleep(100) ;Wait for 100ms
            $aTemp = MouseGetPos() ;Get new mouse position again
        Until(Not ($aTemp[0] = $Pos[0] and $aTemp[1] = $Pos[1] and _IsPressed("01", $hDLL))) ;Loop until mouse is on a new position or it is not pushed again
        ToolTip("X: " & $Pos[0] & "Y: " & $Pos[1] & "Number of continous clicks: " & $nLeftclicks)
        ConsoleWrite("X: " & $Pos[0] & "Y: " & $Pos[1] & "Number of continous clicks: " & $nLeftclicks & @crlf)
    EndIf
Wend

Func _ExitScript()
    DllClose($hDLL)
    Exit
EndFunc ;=> _ExitScript

 

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

  • Similar Content

    • Paxman
      By Paxman
      Just seem to have hit a bit of a hurdle.
      I am trying to get Autoit to tick a box in a dialogue box. However no windows shortcut works (eg. spacebar etc.).
      So having moved to the tick box (how can i produce a double click of the mouse (which ticks the box) without moving the mouse each time. I will need to do this many times in a tall dialogue box you need to scroll through if trying to use the mouse to do it?
      Can i map a keyboard shortcut or something?
      Thanks
      Neil
       
    • Pike
      By Pike
      Hello Everyone,
      New Guy here. This is a two-part question, hopefully easy enough though. I know it's ugly coding, this is because I have only a couple hours worth of coding experience and could really use some help.
      1) When I single click the button I want one action (like playing a sound clip) and when I double click the button I want a different action (such as opening dialogue box to ask for soundclip directory address). At the bottom of the script I made a comment which one I would like single click versus double click.
      2) I can not get the FileOpenDialogue to save the file address to the config.ini file at the 'One' position on the .ini.
      #include <ButtonConstants.au3> #include <GUIConstantsEx.au3> #include <WindowsConstants.au3> #include <MsgBoxConstants.au3> #include <FileConstants.au3> #include <AutoItConstants.au3> #include <Sound.au3> Opt('MustDeclareVars', 1) Opt("GUIOnEventMode", 1) Global $OneID _Main() Func _Main() GUICreate("Form1", 156, 86, 281, 276) $OneID = GUICtrlCreateButton("Button", 15, 17, 122, 37) GUICtrlSetOnEvent($OneID, "OnOne") GUISetOnEvent($GUI_EVENT_CLOSE, "OnExit") GUISetState() While 1 While 1 Sleep(1000) WEnd WEnd EndFunc ;---------------Functions------------- While 1 Sleep(100) WEnd Func OnOne() ; ******************Single Click for this action**************** $OneID = IniRead("config.ini", "Config", "One", $OneID) If $OneID = True Then SoundPlay($OneID, @HotKeyPressed) Else ; *******Double Click for this action*****And Help Saving File Address to config.ini 'One'*********** $OneID = FileOpenDialog("Select file", @WorkingDir, "All (*.*)") IniWrite("config.ini", "Config", "One", $OneID) $OneID = InputBox("Enter Information", "Short Name for File?", "") IniWrite("config.ini", "Config", "NameOne", $OneID) EndIf EndFunc Any help would be greatly appreciated, and if you need anymore clarity as to what exactly I'm trying to do, please do not hesitate to ask. Thanks so much!
      Pike
    • Simpel
      By Simpel
      Hi,
      I'm wondering why my code snippet isn't working. I copied most of it from a script where it work's. Here is the code:
      #include <Constants.au3> Opt("TrayAutoPause", 0) ; no pause while click Opt("TrayMenuMode", 1) ; no exit and pause Opt("TrayOnEventMode", 1); While 1 Sleep(100) WEnd Exit #Region ### START Tray section Global $hIni = TrayCreateItem("open INI") TrayItemSetOnEvent (-1, "_INI") TrayCreateItem("") Global $hExit = TrayCreateItem("Exit") TrayItemSetOnEvent (-1, "_Quit") TraySetState(1) TraySetClick(16); INI and Exit with secondary click TraySetOnEvent( $TRAY_EVENT_PRIMARYDOUBLE, "_OpenPath" ); _OpenPath with primary double click #EndRegion ### END Tray section Func _OpenPath() $sPfad = @ScriptDir ConsoleWrite('@@ Debug(' & @ScriptLineNumber & ') : $sPfad = ' & $sPfad & @CRLF & '>Error code: ' & @error & @CRLF) ;### Debug Console $iDelimiterPfad = StringInStr($sPfad, "\", 0, -1) ConsoleWrite('@@ Debug(' & @ScriptLineNumber & ') : $iDelimiterPfad = ' & $iDelimiterPfad & @CRLF & '>Error code: ' & @error & @CRLF) ;### Debug Console $sPfadHoeher = StringLeft($sPfad, $iDelimiterPfad) ConsoleWrite('@@ Debug(' & @ScriptLineNumber & ') : $sPfadHoeher = ' & $sPfadHoeher & @CRLF & '>Error code: ' & @error & @CRLF) ;### Debug Console ShellExecute($sPfadHoeher) EndFunc Func _INI() ConsoleWrite("_INI" & @CRLF) Sleep(10) EndFunc Func _Quit() Exit EndFunc I can see the trayicon but there is nothing happening neither primary double click nor secondary click. What's wrong?
      Regards, Conrad
    • bustasnipe
      By bustasnipe
      Hello I have been going to this forum regularly for a couple weeks, getting all sorts of good info from you people.
      So first off thanks, you are all gentlemen/women and scholars.
      I just wanted to point out that I think it would be neat if you could double click either side of the quotation marks in the Scite editor to select the string of text within. Instead of having to manually select all the text within, which is not hard, but u may sometimes miss a extra character or something. Just like how you can select variables and single words by double clicking. Thanks to all those who have worked on and continue to work on Autoit, I love it.
       
    • XuxinhaKill
      By XuxinhaKill
      Well that is what I need... Identify a double click on a GUI label

      Something like this:

      #include <GUIConstantsEx.au3> #include <StaticConstants.au3> #include <WindowsConstants.au3> $n1 = "Null" $n2 = "Null" GUICreate("Teste",200,200) $Name1 = GUICtrlCreateLabel($n1,0,0,100,100,$SS_CENTER) $Name2 = GUICtrlCreateLabel($n2,0,100,100,100,$SS_CENTER) GUISetState(@SW_SHOW) While 1 ; If the label Name1 is double clicked then it pops up a inputbox If DoubleClick($Name1) Then $n1 = InputBox("Name","Name") GUICtrlSetData($Name1,$n1) EndIf WEnd
×