rony2006

Mouse click after hotkey

5 posts in this topic

Hello, I have the following udf and I want when i press ALT + Q (the hotkey set by default to show a simple gui) to begin directly "Read Region" function.

I tried to add MouseClick after hotkey but doesn't click on Read Region.

Can you help me please?

;r-OCR
;By: ronriel

;updated oct. 20, 2008
;autoit-v3.2.12.1
;Tested  Windows XP SP2 with MS Office 2007

#Include <ScreenCapture.au3>
#include <WindowsConstants.au3>
#Include <Misc.au3>

_Singleton("script",0)
Opt("GUIOnEventMode",1)
Opt("WinTitleMatchMode", 4)
Opt("TrayOnEventMode",1)
Opt("TrayMenuMode",1)
HotKeySet("!q","ShowWin")

Local $err = ObjEvent("AutoIt.Error","Error")

$GUI = GUICreate("", 90, 40,-1,-1,0x80880000,$WS_EX_TOOLWINDOW)
WinSetTrans($GUI,'',200)
$B1 = GUICtrlCreateButton("Read Region", 0, 0, 90, 20)
GUICtrlSetBkColor(-1,0xe6e6fa)
GUICtrlSetOnEvent(-1,"Capture")
$B2 = GUICtrlCreateButton("Read File", 0, 20, 90, 20)
GUICtrlSetBkColor(-1,0xe6e6fa)
GUICtrlSetOnEvent(-1,"Capture")

$GUI2 =  GUICreate("", 0 , 0 , 0, 0,  BitOR($WS_POPUP,$WS_BORDER), BitOR($WS_EX_TOPMOST, $WS_EX_TOOLWINDOW))
GUISetBkColor(0xe6e6fa,$GUI2)
WinSetTrans($GUI2,'',60)
GUISetState(@SW_HIDE)

$GUI3 = GUICreate("", 0 , 0 , 0,0 ,  BitOR($WS_POPUP,$WS_BORDER), BitOR($WS_EX_TOPMOST, $WS_EX_TOOLWINDOW))
GUISetBkColor(0xFF0000,$GUI3)
GUISetState(@SW_HIDE)

TrayCreateItem("Info")
TrayItemSetOnEvent(-1,'Ex')
$Exit = TrayCreateItem("Exit")
TrayItemSetOnEvent(-1,'Ex')
TraySetState()

Func ShowWin()
    If BitAND(WinGetState($GUI),2) Then
        GUISetState(@SW_HIDE,$GUI)
    Else
        $Mpos = MouseGetPos()
        WinMove($GUI,'',$Mpos[0]-45,$Mpos[1]-10)
        GUISetState(@SW_SHOW,$GUI)
    EndIf
EndFunc

Func Capture()
    Local $Image = @TempDir&"\ScreenCapture.png", $v = 0

    If @GUI_CtrlId = $B1 Then
        GUISetState(@SW_HIDE,$GUI)
        GUISetState(@SW_SHOW,$GUI3)
        Do
            Sleep(10)
            ToolTip("Read Region")
            $c = MouseGetPos()
            WinMove($GUI3,"",$c[0]-2.5,$c[1]-2.5,5,5)
            If _IsPressed(01) Then
                ToolTip('')
                $C1 = MouseGetPos()
                    Do
                        Sleep(10)
                        $C2 = MouseGetPos()
                            If $C2[0] <> $C1[0] And $C2[1] <> $C1[1] Then
                                GUISetState(@SW_SHOW,$GUI2)
                            EndIf
                            If $C1[0] < $C2[0] Then
                                $X = $C1[0]
                                $W = $C2[0] - $C1[0]
                            Else
                                $X = $C2[0]
                                $W = $C1[0] - $C2[0]
                            EndIf
                            If $C1[1] < $C2[1] Then
                                $Y = $C1[1]
                                $H = $C2[1] - $C1[1]
                            Else
                                $Y = $C2[1]
                                $H = $C1[1] - $C2[1]
                            EndIf
                        WinMove($GUI2,"",$X,$Y,$W,$H)
                        WinMove($GUI3,"",$c2[0]-2.5,$c2[1]-2.5,5,5)
                    Until Not _IsPressed(01)
                $v = 1
            EndIf
        Until $v = 1
        GUISetState(@SW_HIDE,$GUI2)
        GUISetState(@SW_HIDE,$GUI3)
        _ScreenCapture_Capture($Image,$x,$y,$x+$W,$y+$h,False)
    ElseIf @GUI_CtrlId = $B2 Then
        GUISetState(@SW_HIDE,$GUI)
        $Image = FileOpenDialog("Choose an Image File",@DesktopDir&"\","Images (*.jpg;*.bmp;*.gif;*.png;*.tif)")
        if @error then Return
    EndIf
    $OCR = OCR($Image)
    If @error then Return
    ClipPut($OCR)
    TrayTip("Info","Text saved to Clipboard",2,1)
EndFunc

Func OCR($Image)
    Local  $y = 0, $string

    $miDoc = ObjCreate("MODI.Document")
    $miDoc.Create($Image)
    If @error Then Return SetError(1)

    $miDoc.Ocr(9, True, True);language is English
    If @error Then
        TrayTip("Info","OCR could not read any character.",2,1)
        ClipPut("Error!")
        Return SetError(1)
    EndIf

    For $Word In $miDoc.Images(0).Layout.Words
        $delim = ' '
        If $miDoc.Images(0).Layout.Words($y).Rects(0).Bottom < $miDoc.Images(0).Layout.Words($y+1).Rects(0).Top Then $delim = @CRLF
        $string &= $Word.text & $delim
        $y += 1
    Next
Return StringTrimRight($string,1)
EndFunc

Func Error()
Endfunc

Func Ex()
    If @TRAY_ID = $Exit Then
        Exit
    Else
        MsgBox(0,"Info", "Press ALT+Q to start."&@CRLF&@CRLF&"r-OCR"&@CRLF&"By: ronriel (ronriel@yahoo.com)")
    EndIf
EndFunc

While 1
Sleep(100000)
WEnd

 

Share this post


Link to post
Share on other sites



#3 ·  Posted (edited)

Hi, add

ControlClick($GUI, '', $B1)

to the ShowWin() function.

PS: after the if's, that is, because before the buttons/gui is hidden.

Edited by careca

Spoiler

Paster - Main function is to paste text, but has more functions.

OpenW - Open With... alternative, Open any file with any application, set it's icon, set application as default.

Renamer - Rename files and folders, remove portions of text from the filename etc.

BeatsPlayer - Music player.

Params Tool - Right click an exe to see it's parameters or execute them.

Regedit Control - Registry browsing history, quickly jump into any saved key.

Time4Shutdown - Write the time for shutdown in minutes.

Power Profiles Tool - Set a profile as active, delete, duplicate, export and import.

Firefox Profile Backup - Backup/restore previously saved profile.

Finished Task Shutdown - Shuts down pc when specified window/Wndl/process closes.

NetworkSpeedShutdown - Shuts down pc if download speed goes under "X" Kb/s.

 

Share this post


Link to post
Share on other sites

#4 ·  Posted (edited)

7 hours ago, JohnOne said:

There is no MouseClick in your code.

Yes he only tried to solve with mouseclick, so the title of this thread is wrong.

@rony2006: i think this:

;r-OCR
;By: ronriel

;updated oct. 20, 2008
;autoit-v3.2.12.1
;Tested  Windows XP SP2 with MS Office 2007

#Include <ScreenCapture.au3>
#include <WindowsConstants.au3>
#Include <Misc.au3>

_Singleton("script",0)
Opt("GUIOnEventMode",1)
Opt("WinTitleMatchMode", 4)
Opt("TrayOnEventMode",1)
Opt("TrayMenuMode",1)
HotKeySet("!q","ShowWin")

Local $err = ObjEvent("AutoIt.Error","Error")

$GUI = GUICreate("", 90, 40,-1,-1,0x80880000,$WS_EX_TOOLWINDOW)
WinSetTrans($GUI,'',200)
$B1 = GUICtrlCreateButton("Read &Region", 0, 0, 90, 20)
GUICtrlSetBkColor(-1,0xe6e6fa)
GUICtrlSetOnEvent(-1,"Capture")
$B2 = GUICtrlCreateButton("Read &File", 0, 20, 90, 20)
GUICtrlSetBkColor(-1,0xe6e6fa)
GUICtrlSetOnEvent(-1,"Capture")
GUISetState(@SW_SHOW)

$GUI2 =  GUICreate("", 0 , 0 , 0, 0,  BitOR($WS_POPUP,$WS_BORDER), BitOR($WS_EX_TOPMOST, $WS_EX_TOOLWINDOW))
GUISetBkColor(0xe6e6fa,$GUI2)
WinSetTrans($GUI2,'',60)
GUISetState(@SW_HIDE)

$GUI3 = GUICreate("", 0 , 0 , 0,0 ,  BitOR($WS_POPUP,$WS_BORDER), BitOR($WS_EX_TOPMOST, $WS_EX_TOOLWINDOW))
GUISetBkColor(0xFF0000,$GUI3)
GUISetState(@SW_HIDE)

TrayCreateItem("Info")
TrayItemSetOnEvent(-1,'Ex')
$Exit = TrayCreateItem("Exit")
TrayItemSetOnEvent(-1,'Ex')
TraySetState()
ControlClick($GUI,'',$B1)

Func ShowWin()
    If BitAND(WinGetState($GUI),2) Then
        GUISetState(@SW_HIDE,$GUI)
    Else
        $Mpos = MouseGetPos()
        WinMove($GUI,'',$Mpos[0]-45,$Mpos[1]-10)
        GUISetState(@SW_SHOW,$GUI)
    EndIf
EndFunc

Func Capture()
    Local $Image = @TempDir&"\ScreenCapture.png", $v = 0

    If @GUI_CtrlId = $B1 Then
        GUISetState(@SW_HIDE,$GUI)
        GUISetState(@SW_SHOW,$GUI3)
        Do
            Sleep(10)
            ToolTip("Read Region")
            $c = MouseGetPos()
            WinMove($GUI3,"",$c[0]-2.5,$c[1]-2.5,5,5)
            If _IsPressed(01) Then
                ToolTip('')
                $C1 = MouseGetPos()
                    Do
                        Sleep(10)
                        $C2 = MouseGetPos()
                            If $C2[0] <> $C1[0] And $C2[1] <> $C1[1] Then
                                GUISetState(@SW_SHOW,$GUI2)
                            EndIf
                            If $C1[0] < $C2[0] Then
                                $X = $C1[0]
                                $W = $C2[0] - $C1[0]
                            Else
                                $X = $C2[0]
                                $W = $C1[0] - $C2[0]
                            EndIf
                            If $C1[1] < $C2[1] Then
                                $Y = $C1[1]
                                $H = $C2[1] - $C1[1]
                            Else
                                $Y = $C2[1]
                                $H = $C1[1] - $C2[1]
                            EndIf
                        WinMove($GUI2,"",$X,$Y,$W,$H)
                        WinMove($GUI3,"",$c2[0]-2.5,$c2[1]-2.5,5,5)
                    Until Not _IsPressed(01)
                $v = 1
            EndIf
        Until $v = 1
        GUISetState(@SW_HIDE,$GUI2)
        GUISetState(@SW_HIDE,$GUI3)
        _ScreenCapture_Capture($Image,$x,$y,$x+$W,$y+$h,False)
    ElseIf @GUI_CtrlId = $B2 Then
        GUISetState(@SW_HIDE,$GUI)
        $Image = FileOpenDialog("Choose an Image File",@DesktopDir&"\","Images (*.jpg;*.bmp;*.gif;*.png;*.tif)")
        if @error then Return
    EndIf
    $OCR = OCR($Image)
    If @error then Return
    ClipPut($OCR)
    TrayTip("Info","Text saved to Clipboard",2,1)
EndFunc

Func OCR($Image)
    Local  $y = 0, $string

    $miDoc = ObjCreate("MODI.Document")
    $miDoc.Create($Image)
    If @error Then Return SetError(1)

    $miDoc.Ocr(9, True, True);language is English
    If @error Then
        TrayTip("Info","OCR could not read any character.",2,1)
        ClipPut("Error!")
        Return SetError(1)
    EndIf

    For $Word In $miDoc.Images(0).Layout.Words
        $delim = ' '
        If $miDoc.Images(0).Layout.Words($y).Rects(0).Bottom < $miDoc.Images(0).Layout.Words($y+1).Rects(0).Top Then $delim = @CRLF
        $string &= $Word.text & $delim
        $y += 1
    Next
Return StringTrimRight($string,1)
EndFunc

Func Error()
Endfunc

Func Ex()
    If @TRAY_ID = $Exit Then
        Exit
    Else
        MsgBox(0,"Info", "Press ALT+Q to start."&@CRLF&@CRLF&"r-OCR"&@CRLF&"By: ronriel (ronriel@yahoo.com)")
    EndIf
EndFunc

While 1
Sleep(100000)
WEnd

is what you want, i hope the short popup of the MainGui (Read Region|Read File) is ok.

Otherwise you have to split your capture-func in 2 funcs:

;r-OCR
;By: ronriel

;updated oct. 20, 2008
;autoit-v3.2.12.1
;Tested  Windows XP SP2 with MS Office 2007

#include <ScreenCapture.au3>
#include <WindowsConstants.au3>
#include <Misc.au3>

_Singleton("script", 0)
Opt("GUIOnEventMode", 1)
Opt("WinTitleMatchMode", 4)
Opt("TrayOnEventMode", 1)
Opt("TrayMenuMode", 1)
HotKeySet("!q", "ShowWin")

Local $err = ObjEvent("AutoIt.Error", "Error")
Global $Image

$GUI = GUICreate("", 90, 40, -1, -1, 0x80880000, $WS_EX_TOOLWINDOW)
WinSetTrans($GUI, '', 200)
$B1 = GUICtrlCreateButton("Read &Region", 0, 0, 90, 20)
GUICtrlSetBkColor(-1, 0xe6e6fa)
GUICtrlSetOnEvent(-1, "CaptureGui")
$B2 = GUICtrlCreateButton("Read &File", 0, 20, 90, 20)
GUICtrlSetBkColor(-1, 0xe6e6fa)
GUICtrlSetOnEvent(-1, "CaptureGui")

$GUI2 = GUICreate("", 0, 0, 0, 0, BitOR($WS_POPUP, $WS_BORDER), BitOR($WS_EX_TOPMOST, $WS_EX_TOOLWINDOW))
GUISetBkColor(0xe6e6fa, $GUI2)
WinSetTrans($GUI2, '', 60)
GUISetState(@SW_HIDE)

$GUI3 = GUICreate("", 0, 0, 0, 0, BitOR($WS_POPUP, $WS_BORDER), BitOR($WS_EX_TOPMOST, $WS_EX_TOOLWINDOW))
GUISetBkColor(0xFF0000, $GUI3)
GUISetState(@SW_HIDE)

TrayCreateItem("Info")
TrayItemSetOnEvent(-1, 'Ex')
$Exit = TrayCreateItem("Exit")
TrayItemSetOnEvent(-1, 'Ex')
TraySetState()
ReadRegion()

Func ShowWin()
    If BitAND(WinGetState($GUI), 2) Then
        GUISetState(@SW_HIDE, $GUI)
    Else
        $Mpos = MouseGetPos()
        WinMove($GUI, '', $Mpos[0] - 45, $Mpos[1] - 10)
        GUISetState(@SW_SHOW, $GUI)
    EndIf
EndFunc   ;==>ShowWin

Func ReadRegion()
    $Image = @TempDir & "\ScreenCapture.png"
    Local $v = 0
    GUISetState(@SW_HIDE, $GUI)
    GUISetState(@SW_SHOW, $GUI3)
    Do
        Sleep(10)
        ToolTip("Read Region")
        $c = MouseGetPos()
        WinMove($GUI3, "", $c[0] - 2.5, $c[1] - 2.5, 5, 5)
        If _IsPressed(01) Then
            ToolTip('')
            $C1 = MouseGetPos()
            Do
                Sleep(10)
                $C2 = MouseGetPos()
                If $C2[0] <> $C1[0] And $C2[1] <> $C1[1] Then
                    GUISetState(@SW_SHOW, $GUI2)
                EndIf
                If $C1[0] < $C2[0] Then
                    $X = $C1[0]
                    $W = $C2[0] - $C1[0]
                Else
                    $X = $C2[0]
                    $W = $C1[0] - $C2[0]
                EndIf
                If $C1[1] < $C2[1] Then
                    $y = $C1[1]
                    $H = $C2[1] - $C1[1]
                Else
                    $y = $C2[1]
                    $H = $C1[1] - $C2[1]
                EndIf
                WinMove($GUI2, "", $X, $y, $W, $H)
                WinMove($GUI3, "", $C2[0] - 2.5, $C2[1] - 2.5, 5, 5)
            Until Not _IsPressed(01)
            $v = 1
        EndIf
    Until $v = 1
    _ScreenCapture_Capture($Image, $X, $y, $X + $W, $y + $H, False)
    GUISetState(@SW_HIDE, $GUI2)
    GUISetState(@SW_HIDE, $GUI3)
EndFunc   ;==>ReadRegion

Func CaptureGui()
    If @GUI_CtrlId = $B1 Then
        ReadRegion()
    ElseIf @GUI_CtrlId = $B2 Then
        GUISetState(@SW_HIDE, $GUI)
        $Image = FileOpenDialog("Choose an Image File", @DesktopDir & "\", "Images (*.jpg;*.bmp;*.gif;*.png;*.tif)")
        If @error Then Return
    EndIf
    $OCR = OCR($Image)
    If @error Then Return
    ClipPut($OCR)
    TrayTip("Info", "Text saved to Clipboard", 2, 1)
EndFunc   ;==>CaptureGui

Func OCR($Image)
    Local $y = 0, $string

    $miDoc = ObjCreate("MODI.Document")
    $miDoc.Create($Image)
    If @error Then Return SetError(1)

    $miDoc.Ocr(9, True, True);language is English
    If @error Then
        TrayTip("Info", "OCR could not read any character.", 2, 1)
        ClipPut("Error!")
        Return SetError(1)
    EndIf

    For $Word In $miDoc.Images(0).Layout.Words
        $delim = ' '
        If $miDoc.Images(0).Layout.Words($y).Rects(0).Bottom < $miDoc.Images(0).Layout.Words($y + 1).Rects(0).Top Then $delim = @CRLF
        $string &= $Word.text & $delim
        $y += 1
    Next
    Return StringTrimRight($string, 1)
EndFunc   ;==>OCR

Func Error()
EndFunc   ;==>Error

Func Ex()
    If @TRAY_ID = $Exit Then
        Exit
    Else
        MsgBox(0, "Info", "Press ALT+Q to start." & @CRLF & @CRLF & "r-OCR" & @CRLF & "By: ronriel (ronriel@yahoo.com)")
    EndIf
EndFunc   ;==>Ex

While 1
    Sleep(100000)
WEnd

 

Edited by AutoBert

Share this post


Link to post
Share on other sites

Thanks guys.

First example of AutoBert is working ok.

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