hiteshluthraauto

Clipboard cleaner tool

8 posts in this topic

Hi all,

This is second project created in autoit for cleaning clipboard automatically when Ctrl + V is pressed , but facing one issue in script. While script works well in cleaning clipboard but not writing logs in correct manner, its logging same log multiple times (FYI...Filewritelog will log anything which is was copied). I am attaching source code and log file so that you guys can see code and help me in identifying the cause here.

Note: Script write to log file if Ctrl + C is pressed , but its writing multiple logs even if we press Ctrl + C once.

 

Thanks in advance

Clipboard_Cleaner.au3

Cliplog.txt

Share this post


Link to post
Share on other sites



hiteshluthraauto,

Welcome to the AutoIt forums.

As you are seeking help with your script, I have moved it to the correct section.

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

You can add loop in _IsPressed so program wait until key is released.
Example from help:

#include <Misc.au3>
#include <MsgBoxConstants.au3>

Local $hDLL = DllOpen("user32.dll")

While 1
    If _IsPressed("10", $hDLL) Then
        ConsoleWrite("_IsPressed - Shift Key was pressed." & @CRLF)
        ; Wait until key is released.
        While _IsPressed("10", $hDLL)
            Sleep(250)
        WEnd
        ConsoleWrite("_IsPressed - Shift Key was released." & @CRLF)
    ElseIf _IsPressed("1B", $hDLL) Then
        MsgBox($MB_SYSTEMMODAL, "_IsPressed", "The Esc Key was pressed, therefore we will close the application.")
        ExitLoop
    EndIf
    Sleep(250)
WEnd

DllClose($hDLL)

Or you can do a variable that stored last writed text and when you write you should check if last text <> current text.

Share this post


Link to post
Share on other sites

Hi Daavis,

I am known to requirement and i think we can use second option which you mentioned, can you please provide me example for it, pls ?

Last text <> Current text

Share this post


Link to post
Share on other sites

#5 ·  Posted (edited)

Wait until key is released:

#include <Misc.au3>
#include <MsgBoxConstants.au3>
#include <Timers.au3>
#include <Date.au3>
#include <File.au3>
#include <Clipboard.au3>
#include <GUIConstantsEx.au3>
#include <SendMessage.au3>
#include <WindowsConstants.au3>
#include <FileConstants.au3>
#include <Array.au3>


Global $g_idMemo, $g_hNext = 0
Global $Timeout = 10
Global $CopyTime
Global $CurrentTime
Global $LogDir = @UserProfileDir & "\Appdata\Roaming\Cliplog"
Global $LogBackup = @UserProfileDir & "\Appdata\Roaming\Cliplog\Backup"
Global $IsPasted = True
Local $LogFile = @UserProfileDir & "\Appdata\Roaming\Cliplog\Cliplog.txt"
Local $FileCreateDate = FileGetTime($LogFile, 1)

If FileExists($LogDir) Then

    If FileExists($LogFile) Then

        If $FileCreateDate[1] < @MON Or $FileCreateDate[0] < @YEAR Then
            FileMove(@UserProfileDir & "\Appdata\Roaming\Cliplog\Cliplog.txt", @UserProfileDir & "\Appdata\Roaming\Cliplog\Cliplog_" & @MDAY & @MON & @YEAR & ".txt", 8)
            If FileExists($LogBackup) Then
                FileMove(@UserProfileDir & "\Appdata\Roaming\Cliplog" & "\*.txt", @UserProfileDir & "\Appdata\Roaming\Cliplog\Backup", $FC_OVERWRITE + $FC_CREATEPATH)
            Else
                DirCreate($LogBackup)
                FileMove(@UserProfileDir & "\Appdata\Roaming\Cliplog" & "\*.txt", @UserProfileDir & "\Appdata\Roaming\Cliplog\Backup", $FC_OVERWRITE + $FC_CREATEPATH)
            EndIf
        EndIf


    Else

        Example()


    EndIf

Else

    DirCreate($LogDir)

EndIf





Example()

Func Example()
    Local $hGUI

    ; Create GUI
    $hGUI = GUICreate("Clipboard", 600, 400)
    $g_idMemo = GUICtrlCreateEdit("", 2, 2, 596, 396, $WS_VSCROLL)
    GUICtrlSetFont($g_idMemo, 9, 400, 0, "Courier New")

    GUISetState(@SW_SHOWMINIMIZED)

    ; Initialize clipboard viewer
    $g_hNext = _ClipBoard_SetViewer($hGUI)

    GUIRegisterMsg($WM_CHANGECBCHAIN, "WM_CHANGECBCHAIN")
    GUIRegisterMsg($WM_DRAWCLIPBOARD, "WM_DRAWCLIPBOARD")

    MemoWrite("Clipboard utility ....: AON")
    MemoWrite("Clipboard owner ....: " & @UserName)
    MemoWrite(":...............................................................................:")

    ; Loop until the user exits.
    Local $GetMsg = True
    While 1
        Local $currMsg = GUIGetMsg()
        Clear()

        ; if $currMsg = $GUI_EVENT_CLOSE then $GetMsg = False
    WEnd
    ; Shut down clipboard viewer
    _ClipBoard_ChangeChain($hGUI, $g_hNext)

EndFunc   ;==>Example

; Write message to memo
Func MemoWrite($sMessage = "", $IsDate = True)
    If $IsDate Then
        GUICtrlSetData($g_idMemo, _Now() & " " & $sMessage & @CRLF, 1)
    Else
        GUICtrlSetData($g_idMemo, $sMessage & @CRLF, 1)
    EndIf
EndFunc   ;==>MemoWrite

; Handle $WM_CHANGECBCHAIN messages
Func WM_CHANGECBCHAIN($hWnd, $iMsg, $wParam, $lParam)
    #forceref $hWnd, $iMsg
    ; Show that message was received
    MemoWrite("***** $WM_CHANGECBCHAIN *****")

    ; If the next window is closing, repair the chain
    If $wParam = $g_hNext Then
        $g_hNext = $lParam
        ; Otherwise pass the message to the next viewer
    ElseIf $g_hNext <> 0 Then
        _SendMessage($g_hNext, $WM_CHANGECBCHAIN, $wParam, $lParam, 0, "hwnd", "hwnd")
    EndIf
EndFunc   ;==>WM_CHANGECBCHAIN

; Handle $WM_DRAWCLIPBOARD messages
Func WM_DRAWCLIPBOARD($hWnd, $iMsg, $wParam, $lParam)
    #forceref $hWnd, $iMsg
    ; Display any text on clipboard
    MemoWrite(_ClipBoard_GetData())

    ; Pass the message to the next viewer
    If $g_hNext <> 0 Then _SendMessage($g_hNext, $WM_DRAWCLIPBOARD, $wParam, $lParam)
EndFunc   ;==>WM_DRAWCLIPBOARD




Func Clear()

    If _IsPressed("11") And _IsPressed("56") Then
        If Not $IsPasted Then
            $CMD = "echo off | clip" ;you start your script here
            RunWait(@ComSpec & " /c " & $CMD, @WindowsDir, @SW_SHOW)
        EndIf
        $IsPasted = True
    Else
        If _IsPressed("11") And _IsPressed("43") Then


            Global $sData = ClipGet() ; Retrieve the data stored in the clipboard.
            $CopyTime = _NowCalc() ; Store copytime to variable
            ;Msgbox ($MB_SYSTEMMODAL, "", "The following tmp"  & @CRLF & ClipGet() , $Timeout)
            _FileWriteLog($LogFile, $sData) ;Write Log to log file
            $IsPasted = False
            While _IsPressed("11") And _IsPressed("43")
                Sleep(250)
            WEnd
        EndIf
        $CurrentTime = _NowCalc()
        If Not $IsPasted Then
            If _DateDiff('s', $CopyTime, $CurrentTime) > 10 And _DateDiff('s', $CopyTime, $CurrentTime) < 12 Then
                $CMD = "echo off | clip" ;you start your script here
                RunWait(@ComSpec & " /c " & $CMD, @WindowsDir, @SW_SHOW)
                $IsPasted = True
            EndIf
        EndIf

        If _IsPressed("11") And _IsPressed("10") And _IsPressed("23") Then
            Exit


        EndIf

    EndIf
    Break(0) ; Disable Break/Pause/Exit of script
    GUIGetMsg() ; to cool out CPU when CTRL+V is not pressed. It was not kept in loop of while so that it can be called everytime but not when CTRL+ V is pressed.If not added then CPU usage will be 25%


EndFunc   ;==>Clear

Last text <> Current text:

#include <Misc.au3>
#include <MsgBoxConstants.au3>
#include <Timers.au3>
#include <Date.au3>
#include <File.au3>
#include <Clipboard.au3>
#include <GUIConstantsEx.au3>
#include <SendMessage.au3>
#include <WindowsConstants.au3>
#include <FileConstants.au3>
#include <Array.au3>


Global $g_idMemo, $g_hNext = 0
Global $Timeout = 10
Global $CopyTime
Global $CurrentTime
Global $LogDir = @UserProfileDir & "\Appdata\Roaming\Cliplog"
Global $LogBackup = @UserProfileDir & "\Appdata\Roaming\Cliplog\Backup"
Global $IsPasted = True
Local $LogFile = @UserProfileDir & "\Appdata\Roaming\Cliplog\Cliplog.txt"
Local $FileCreateDate = FileGetTime($LogFile, 1)

Global $LastText = ""

If FileExists($LogDir) Then

    If FileExists($LogFile) Then

        If $FileCreateDate[1] < @MON Or $FileCreateDate[0] < @YEAR Then
            FileMove(@UserProfileDir & "\Appdata\Roaming\Cliplog\Cliplog.txt", @UserProfileDir & "\Appdata\Roaming\Cliplog\Cliplog_" & @MDAY & @MON & @YEAR & ".txt", 8)
            If FileExists($LogBackup) Then
                FileMove(@UserProfileDir & "\Appdata\Roaming\Cliplog" & "\*.txt", @UserProfileDir & "\Appdata\Roaming\Cliplog\Backup", $FC_OVERWRITE + $FC_CREATEPATH)
            Else
                DirCreate($LogBackup)
                FileMove(@UserProfileDir & "\Appdata\Roaming\Cliplog" & "\*.txt", @UserProfileDir & "\Appdata\Roaming\Cliplog\Backup", $FC_OVERWRITE + $FC_CREATEPATH)
            EndIf
        EndIf


    Else

        Example()


    EndIf

Else

    DirCreate($LogDir)

EndIf





Example()

Func Example()
    Local $hGUI

    ; Create GUI
    $hGUI = GUICreate("Clipboard", 600, 400)
    $g_idMemo = GUICtrlCreateEdit("", 2, 2, 596, 396, $WS_VSCROLL)
    GUICtrlSetFont($g_idMemo, 9, 400, 0, "Courier New")

    GUISetState(@SW_SHOWMINIMIZED)

    ; Initialize clipboard viewer
    $g_hNext = _ClipBoard_SetViewer($hGUI)

    GUIRegisterMsg($WM_CHANGECBCHAIN, "WM_CHANGECBCHAIN")
    GUIRegisterMsg($WM_DRAWCLIPBOARD, "WM_DRAWCLIPBOARD")

    MemoWrite("Clipboard utility ....: AON")
    MemoWrite("Clipboard owner ....: " & @UserName)
    MemoWrite(":...............................................................................:")

    ; Loop until the user exits.
    Local $GetMsg = True
    While 1
        Local $currMsg = GUIGetMsg()
        Clear()

        ; if $currMsg = $GUI_EVENT_CLOSE then $GetMsg = False
    WEnd
    ; Shut down clipboard viewer
    _ClipBoard_ChangeChain($hGUI, $g_hNext)

EndFunc   ;==>Example

; Write message to memo
Func MemoWrite($sMessage = "", $IsDate = True)
    If $IsDate Then
        GUICtrlSetData($g_idMemo, _Now() & " " & $sMessage & @CRLF, 1)
    Else
        GUICtrlSetData($g_idMemo, $sMessage & @CRLF, 1)
    EndIf
EndFunc   ;==>MemoWrite

; Handle $WM_CHANGECBCHAIN messages
Func WM_CHANGECBCHAIN($hWnd, $iMsg, $wParam, $lParam)
    #forceref $hWnd, $iMsg
    ; Show that message was received
    MemoWrite("***** $WM_CHANGECBCHAIN *****")

    ; If the next window is closing, repair the chain
    If $wParam = $g_hNext Then
        $g_hNext = $lParam
        ; Otherwise pass the message to the next viewer
    ElseIf $g_hNext <> 0 Then
        _SendMessage($g_hNext, $WM_CHANGECBCHAIN, $wParam, $lParam, 0, "hwnd", "hwnd")
    EndIf
EndFunc   ;==>WM_CHANGECBCHAIN

; Handle $WM_DRAWCLIPBOARD messages
Func WM_DRAWCLIPBOARD($hWnd, $iMsg, $wParam, $lParam)
    #forceref $hWnd, $iMsg
    ; Display any text on clipboard
    MemoWrite(_ClipBoard_GetData())

    ; Pass the message to the next viewer
    If $g_hNext <> 0 Then _SendMessage($g_hNext, $WM_DRAWCLIPBOARD, $wParam, $lParam)
EndFunc   ;==>WM_DRAWCLIPBOARD




Func Clear()

    If _IsPressed("11") And _IsPressed("56") Then
        If Not $IsPasted Then
            $CMD = "echo off | clip" ;you start your script here
            RunWait(@ComSpec & " /c " & $CMD, @WindowsDir, @SW_SHOW)
        EndIf
        $IsPasted = True
    Else
        If _IsPressed("11") And _IsPressed("43") Then


            Global $sData = ClipGet() ; Retrieve the data stored in the clipboard.
            $CopyTime = _NowCalc() ; Store copytime to variable
            ;Msgbox ($MB_SYSTEMMODAL, "", "The following tmp"  & @CRLF & ClipGet() , $Timeout)
            If $LastText <> $sData Then _FileWriteLog($LogFile, $sData) ;Write Log to log file
            $IsPasted = False
            $LastText = $sData
        EndIf
        $CurrentTime = _NowCalc()
        If Not $IsPasted Then
            If _DateDiff('s', $CopyTime, $CurrentTime) > 10 And _DateDiff('s', $CopyTime, $CurrentTime) < 12 Then
                $CMD = "echo off | clip" ;you start your script here
                RunWait(@ComSpec & " /c " & $CMD, @WindowsDir, @SW_SHOW)
                $IsPasted = True
            EndIf
        EndIf

        If _IsPressed("11") And _IsPressed("10") And _IsPressed("23") Then
            Exit


        EndIf

    EndIf
    Break(0) ; Disable Break/Pause/Exit of script
    GUIGetMsg() ; to cool out CPU when CTRL+V is not pressed. It was not kept in loop of while so that it can be called everytime but not when CTRL+ V is pressed.If not added then CPU usage will be 25%


EndFunc   ;==>Clear

 

Edited by Daavis

Share this post


Link to post
Share on other sites

Thank you Thank you Thank you...Daavid for your assistance on this. I only heared that chaamps are here in autoit forums but seen today.

Thanks again ..David, you made my day :)

Share this post


Link to post
Share on other sites

This is closed now..thanks all

Share this post


Link to post
Share on other sites

Hi Gents,

You guys helped me in getting code corrected for my tool clipboard cleaner and it works flawlessly on desktops. I therefore got new requirement to add functionality into this tool so that it can work as it is... on remote session too. Lets say im running in tool on my desktop and took citrix session,RDP(mstsc) of another machine then clipboard does not get clear therefore this functionality would be required.

Can someone from many great minds help me in getting things done please ?Clipboard_Cleaner.au3

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