Jump to content

Is the code I wrote not too much effort to the CPU And is there a better way to do it?


Guest
 Share

Recommended Posts

I wrote code that if you hold the middle mouse button for more than 3 seconds, will appear msgbox for a one second.

* If you continue to hold the middle button after the msgbox, then nothing will happen (this is the part I was working on)

Only if you stop holding the middle button and then repeat the action again, then it will happen.

The question is - Did I do it right? I mean - good for CPU.

Because there might be a way to make it better ...

the code:

#include <Misc.au3>
$dif = 0
$Stimer = True
$Pressed = False
$dll = DllOpen("user32.dll")
While 1
    Sleep (100)
    If _IsPressed("04", $dll) Then
        ToolTip("Pressed")
            If $Stimer = True Then
                $Stimer = False
                $begin = TimerInit()
            EndIf    
            $dif = TimerDiff($begin)
            If $dif > 3000 And $Pressed = False Then
                $Pressed = True
                MsgBox(0,"","Action",1)
                $begin = TimerInit()
            EndIf    
    Else    
        ToolTip("Not Pressed")
        If $Pressed = True Then $Pressed = False
        $begin = TimerInit()
        Sleep(500)
    EndIf
WEnd

Thanks for helpers.

Edited by Guest
Link to comment
Share on other sites

#include 

$user32 = DllOpen( "user32.dll" )

While 1
Select
Case _IsPressed( "04", $user32 )
Check( )
EndSelect
WEnd


Func Check( )
$timer = TimerInit( )
While 1
Sleep( 10 )
If TimerDiff( $timer ) >= 3000 AND _IsPressed( "04", $user32 ) Then
MsgBox( 0, "", "Action", 1 )
$timer = TimerInit( )
ExitLoop
Else
ContinueLoop
EndIf
WEnd
EndFunc

That's how I would do it. Much less code and it will keep checking even if you hold it down forever.

Link to comment
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
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...