Sign in to follow this  
Followers 0
Humbug

Snippet: Simple Mouse Click Counter

4 posts in this topic

Hello folks,

im new to the forums and i didnt found a proper place start because i dont seem to the any access to start a topic in the proper thread yet.

i was searching for some mouse click counter so i could figure out if im going insane sooner or later while working with windoze all the time, however i didnt found anything adhoc thus i made this simple counter i'd like to share:

#include <Misc.au3>

HotKeySet("!{ESC}", "Quit")
HotKeySet("!{HOME}", "Stats")

$LOGFILE = "log.ini"
$LCOUNT = IniRead($LOGFILE, "Count", "LCLICK", "Default")
$RCOUNT = IniRead($LOGFILE, "Count", "RCLICK", "Default")

While 1
    Sleep(20)
    If _IsPressed("01") Then
        $LCOUNT=$LCOUNT+1
        IniWrite($LOGFILE, "Count", "LCLICK", $LCOUNT)
        Sleep(100)
    EndIf
    If _IsPressed("02") Then
        $RCOUNT=$RCOUNT+1
        IniWrite($LOGFILE, "Count", "RCLICK", $RCOUNT)
        Sleep(100)
    EndIf
WEnd

Func Quit()
    Exit
EndFunc

Func Stats()
    TrayTip("Count", "Left: " & $LCOUNT & @CRLF & "Right: " & $RCOUNT, 1, 0)
EndFunc

Share this post


Link to post
Share on other sites



i made a little more advanced version, but encountered one issue, it seems that

If _IsPressed("04", $DLL) Or _IsPressed("05", $DLL) Or _IsPressed("06", $DLL) Then

is all the middle mouse button pressed but not scrolled, i'd like to recognize scrolling too, but cant find any matching virtual key code. feedback would be appreciated.

here's the update:

#include <Misc.au3>

HotKeySet("!{ESC}", "Quit")
HotKeySet("!{HOME}", "Stats")

$LOGFILE = "log.ini"
$LCOUNT = IniRead($LOGFILE, "Count", "LCLICK", "Default")
$RCOUNT = IniRead($LOGFILE, "Count", "RCLICK", "Default")
$MMBCOUNT = IniRead($LOGFILE, "Count", "MMB", "Default")
$KEYCOUNT = IniRead($LOGFILE, "Count", "KEYS", "Default")
Global $DLL = DllOpen("user32.dll")
Local $KEYCODES[101] = [08,09,"0C","0D",10,11,12,13,14,"1B",20,21,22,23,24,25,26,27,28,29,"2A","2B","2C","2D","2E","2F",30,31,32,33,34,35,36,37,38,39,41,42,43,44,45,46,47,48,49,"4A","4B","4C","4D","4E","4F",50,51,52,53,54,55,56,57,58,59,"5A","5B","5C","5D","5E","5F",60,61,62,63,64,65,66,67,68,69,"6A","6B","6C","6D","6E","6F",70,71,72,73,74,75,76,77,78,79,"7A","7B","A0","A1","A2","A3","A4","A5"]

While 1
    Sleep(10)
    If _IsPressed("01", $DLL) Then
        $LCOUNT=$LCOUNT+1
        IniWrite($LOGFILE, "Count", "LCLICK", $LCOUNT)
        Do 
        Until Not _IsPressed("01", $DLL)
    EndIf
    If _IsPressed("02", $DLL) Then
        $RCOUNT=$RCOUNT+1
        IniWrite($LOGFILE, "Count", "RCLICK", $RCOUNT)
        Do 
        Until Not _IsPressed("02", $DLL)
    EndIf
    If _IsPressed("04", $DLL) Or _IsPressed("05", $DLL) Or _IsPressed("06", $DLL) Then
        $MMBCOUNT=$MMBCOUNT+1
        IniWrite($LOGFILE, "Count", "MMB", $MMBCOUNT)
        Sleep(50)
    EndIf
    
    For $I In $KEYCODES
        If _IsPressed($I, $DLL) Then
            $KEYCOUNT=$KEYCOUNT+1
            IniWrite($LOGFILE, "Count", "KEYS", $KEYCOUNT)
            Do 
            Until Not _IsPressed($I, $DLL)
        EndIf
    Next
    
    TraySetToolTip("Counter Stats" & @CRLF & "Left: " & $LCOUNT & @CRLF & "Right: " & $RCOUNT & @CRLF & "MMB: " & $MMBCOUNT & @CRLF & "Keys: " & $KEYCOUNT)
WEnd

Func Quit()
    Exit
    DllClose($DLL)
EndFunc

Func Stats()
    TrayTip("Counter Stats", "Left: " & $LCOUNT & @CRLF & "Right: " & $RCOUNT & @CRLF & "MMB: " & $MMBCOUNT & @CRLF & "Keys: " & $KEYCOUNT, 1, 0)
EndFunc

Share this post


Link to post
Share on other sites

thanks for the quick reply. i had a look but don't seem to fully understand how to work with it yet, i suppose i have to read up on DLL calls. while searching i found http://www.autoitscript.com/forum/index.php?showtopic=92027 which helped realizing the mousewheel count.

i guess there can be done a lot more optimization, however here's the little snippet:

#include <Misc.au3>
#include <WinAPI.au3>

HotKeySet("!{ESC}", "Quit")
HotKeySet("!{HOME}", "Stats")

$LOGFILE = "log.ini"
$LCOUNT = IniRead($LOGFILE, "Count", "LCLICK", "0")
$RCOUNT = IniRead($LOGFILE, "Count", "RCLICK", "0")
$MMBCOUNT = IniRead($LOGFILE, "Count", "MMB", "0")
$KEYCOUNT = IniRead($LOGFILE, "Count", "KEYS", "0")
$MMBROUNDS = IniRead($LOGFILE, "Count", "MMBROUNDS", "0")
Global $DLL = DllOpen("user32.dll")

Dim Const $WM_MOUSEWHEEL = 0x020A
Dim Const $tagMSLLHOOKSTRUCT = $tagPOINT&';dword mouseData;dword flags;dword time;ulong_ptr dwExtraInfo'

$hFunc = DllCallbackRegister("Mouse_LL", "long", "int;wparam;lparam")
$pFunc = DllCallbackGetPtr($hFunc)
$hHook = _WinAPI_SetWindowsHookEx($WH_MOUSE_LL, $pFunc, _WinAPI_GetModuleHandle(0))

Local $KEYCODES[101] = [08,09,"0C","0D",10,11,12,13,14,"1B",20,21,22,23,24,25,26,27,28,29,"2A","2B","2C","2D","2E","2F",30,31,32,33,34,35,36,37,38,39,41,42,43,44,45,46,47,48,49,"4A","4B","4C","4D","4E","4F",50,51,52,53,54,55,56,57,58,59,"5A","5B","5C","5D","5E","5F",60,61,62,63,64,65,66,67,68,69,"6A","6B","6C","6D","6E","6F",70,71,72,73,74,75,76,77,78,79,"7A","7B","A0","A1","A2","A3","A4","A5"]

While 1
    Sleep(10)
    If _IsPressed("01", $DLL) Then
        $LCOUNT+=1
        IniWrite($LOGFILE, "Count", "LCLICK", $LCOUNT)
        Do 
        Until Not _IsPressed("01", $DLL)
    EndIf
    If _IsPressed("02", $DLL) Then
        $RCOUNT+=1
        IniWrite($LOGFILE, "Count", "RCLICK", $RCOUNT)
        Do 
        Until Not _IsPressed("02", $DLL)
    EndIf
    If _IsPressed("04", $DLL) Then
        $MMBCOUNT+=1
        IniWrite($LOGFILE, "Count", "MMB", $MMBCOUNT)
        Do 
        Until Not _IsPressed("04", $DLL)
    EndIf
    
    For $I In $KEYCODES
        If _IsPressed($I, $DLL) Then
            $KEYCOUNT+=1
            IniWrite($LOGFILE, "Count", "KEYS", $KEYCOUNT)
            Do 
            Until Not _IsPressed($I, $DLL)
        EndIf
    Next
    TraySetToolTip("Counter Stats" & @CRLF & "Left: " & $LCOUNT & @CRLF & "Right: " & $RCOUNT & @CRLF & "MMB Clicks: " & $MMBCOUNT & @CRLF & "MMB Rounds: " & Int($MMBROUNDS/360) & @CRLF & "Keys: " & $KEYCOUNT)
WEnd

Func OnAutoItExit()
    _WinAPI_UnhookWindowsHookEx($hHook)
    DllCallbackFree($hFunc)
    DllClose($DLL)
EndFunc

Func Quit()
    Exit
EndFunc

Func Stats()
    TrayTip("Counter Stats", "Left: " & $LCOUNT & @CRLF & "Right: " & $RCOUNT & @CRLF & "MMB Clicks: " & $MMBCOUNT & @CRLF & "MMB Rounds: " & Int($MMBROUNDS/360) & @CRLF & "Keys: " & $KEYCOUNT, 1, 0)
EndFunc

Func Mouse_LL($iCode, $iwParam, $ilParam)
    Local $tMSLLHOOKSTRUCT = DllStructCreate($tagMSLLHOOKSTRUCT, $ilParam)
    If $iwParam = $WM_MOUSEWHEEL Then
        Local $VAL = DllStructGetData($tMSLLHOOKSTRUCT, 'mouseData')/2^16
        If BitAND($VAL, 0x8000) Then $VAL = BitOR($VAL, 0xFFFF0000)
        $MMBROUNDS = $MMBROUNDS + Abs($VAL)
         IniWrite($LOGFILE, "Count", "MMBROUNDS", $MMBROUNDS)
        ;ConsoleWrite(Int($MMBROUNDS/360) & @CRLF)
    EndIf
EndFunc

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
Sign in to follow this  
Followers 0