Sign in to follow this  
Followers 0
kripz

Mousecapture not working

6 posts in this topic

#include <Misc.au3>;

dim $coords[4], $on = false;
HotKeySet("{PAUSE}", "Main")
Opt ("MustDeclareVars", 1)

While 1
    Sleep(1000)
WEnd

Func Main()
$coords = WinGetPos("Untitled - Notepad")
If IsArray($coords) Then
    If $coords[0] <> -32000 Then
        If $on Then
            $on = False
            _MouseTrap()
            MsgBox(0, "", "OFF "  & $coords[0] & " " & $coords[1] & " " & $coords[2] & " " & $coords[3])
        Else
            _MouseTrap($coords[0], $coords[1], $coords[0] + $coords[2], $coords[1] + $coords[3]);
            $on = True;
            MsgBox(0,"", "On " & $coords[0] & " " & $coords[1] & " " & $coords[2] & " " & $coords[3])
        EndIf
    Else
        MsgBox(0,"", "Notepad Minimized "  & $coords[0] & " " & $coords[1] & " " & $coords[2] & " " & $coords[3])
    EndIf
Else
    $on = False
    MsgBox(0, "", "Notepad not found")
EndIf

_MouseTrap()
EndFunc

It finds the window properly but the mouse can leave notepad, what am i doing wrong?

Also any comments to improve code will be appreciated, im new to this.

Share this post


Link to post
Share on other sites



#2 ·  Posted (edited)

CODE
#include <Misc.au3>;

dim $coords[4], $on = false;

HotKeySet("{PAUSE}", "Main")

Opt ("MustDeclareVars", 1)

While 1

Sleep(1000)

WEnd

Func Main()

$coords = WinGetPos("Untitled - Notepad")

If IsArray($coords) Then

If $coords[0] <> -32000 Then

If $on Then

$on = False

_MouseTrap()

MsgBox(0, "", "OFF " & $coords[0] & " " & $coords[1] & " " & $coords[2] & " " & $coords[3])

Else

_MouseTrap($coords[0], $coords[1], $coords[0] + $coords[2], $coords[1] + $coords[3]);

$on = True;

MsgBox(0,"", "On " & $coords[0] & " " & $coords[1] & " " & $coords[2] & " " & $coords[3])<<<<<<<<<<<Take this out<<<<<<<<<<<<

EndIf

Else

MsgBox(0,"", "Notepad Minimized " & $coords[0] & " " & $coords[1] & " " & $coords[2] & " " & $coords[3])

EndIf

Else

$on = False

MsgBox(0, "", "Notepad not found")

EndIf

_MouseTrap()

EndFunc

MsgBox(0,"", "On " & $coords[0] & " " & $coords[1] & " " & $coords[2] & " " & $coords[3])<<<<<<<<<<<Take this out<<<<<<<<<<<<

or move it about the mousetrap

Edited by flip209

" I haven't failed. I've just found 10,000 ways that won't work." Thomas Edison "You cannot help men permanently by doing for them what they could and should do for themselves." Abraham Lincoln

Share this post


Link to post
Share on other sites

I moved it above the mouse capture but it still doesnt work.

Share this post


Link to post
Share on other sites

When Main() gets run, you perform the part inside the If/Else/EndIf and then you clear the _MouseTrap() every time on the line just before EndFunc.

Also, MsgBox() forces the mouse outside the trap and breaks it.

This tweak worked:

#include <Misc.au3>;

Dim $coords[4], $on = False;
HotKeySet("{PAUSE}", "Main")
Opt("MustDeclareVars", 1)

While 1
    Sleep(1000)
WEnd

Func Main()
    $coords = WinGetPos("Untitled - Notepad")
    If IsArray($coords) Then
        If $coords[0] <> -32000 Then
            If $on Then
                $on = False
                _MouseTrap()
                TrayTip("MouseTrap", "OFF " & $coords[0] & " " & $coords[1] & " " & $coords[2] & " " & $coords[3], 5)
            Else
                WinActivate("Untitled - Notepad")
                _MouseTrap($coords[0], $coords[1], $coords[0] + $coords[2], $coords[1] + $coords[3])
                $on = True;
                TrayTip("MouseTrap", "On " & $coords[0] & " " & $coords[1] & " " & $coords[2] & " " & $coords[3], 5)
            EndIf
        Else
            MsgBox(0, "", "Notepad Minimized " & $coords[0] & " " & $coords[1] & " " & $coords[2] & " " & $coords[3])
        EndIf
    Else
        $on = False
        MsgBox(0, "", "Notepad not found")
    EndIf
EndFunc   ;==>Main

:)


Valuater's AutoIt 1-2-3, Class... Is now in Session!For those who want somebody to write the script for them: RentACoder"Any technology distinguishable from magic is insufficiently advanced." -- Geek's corollary to Clarke's law

Share this post


Link to post
Share on other sites

#5 ·  Posted (edited)

Ahh i see, simple mistake.

Do i need

_MouseTrap()

after

$on = False
        MsgBox(0, "", "Notepad not found")
    EndIf
EndFunc ;==>Main

so

$on = False
        MsgBox(0, "", "Notepad not found")
    EndIf
EndFunc ;==>Main

_MouseTrap()

So when the program exits it untraps the mouse?

Thanks for the help.

Edited by kripz

Share this post


Link to post
Share on other sites

Ahh i see, simple mistake.

Do i need

_MouseTrap()

after

$on = False
        MsgBox(0, "", "Notepad not found")
    EndIf
EndFunc;==>Main

so

$on = False
        MsgBox(0, "", "Notepad not found")
    EndIf
EndFunc;==>Main

_MouseTrap()

So when the program exits it untraps the mouse?

Thanks for the help.

Yes, that would be a good idea. Maybe put it after the while loop rather than after the Main() function to make it more obviuous when you read the script. Might also consider a small addition -

#include <Misc.au3>;

Dim $coords[4], $on = False;
HotKeySet("{PAUSE}", "Main")
Opt("MustDeclareVars", 1)

While 1
    Sleep(1000)
WEnd

Func Main()
    $coords = WinGetPos("Untitled - Notepad")
    If IsArray($coords) Then
        If $coords[0] <> -32000 Then
            If $on Then
                $on = False
                _MouseTrap()
                TrayTip("MouseTrap", "OFF " & $coords[0] & " " & $coords[1] & " " & $coords[2] & " " & $coords[3], 5)
            Else
                WinActivate("Untitled - Notepad")
                _MouseTrap($coords[0], $coords[1], $coords[0] + $coords[2], $coords[1] + $coords[3])
                $on = True;
                TrayTip("MouseTrap", "On " & $coords[0] & " " & $coords[1] & " " & $coords[2] & " " & $coords[3], 5)
            EndIf
        Else
            MsgBox(0, "", "Notepad Minimized " & $coords[0] & " " & $coords[1] & " " & $coords[2] & " " & $coords[3])
        EndIf
    Else
        if $on then MouseTrap();<--------- if script is still running but NotePad stops you release the mouse.
        $on = False
        MsgBox(0, "", "Notepad not found")
    EndIf
EndFunc  ;==>Main

Would also be worth having another HotKeySet to exit the program.


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.

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