Sign in to follow this  
Followers 0
iCode

compare mouse cursor position - code efficiency

7 posts in this topic

is there a better way of writing this code?

i am interested in speed/efficiency more than readability

i want to get the cursor position at 2 different times, then compare them to see if it was moved beyond the threshold values

the code i have works fine, but i have a suspession there is a better way to write it so maybe it runs faster???

$XPixelThreshold = 10
$YPixelThreshold = 10

$a_mPos1 = MouseGetPos()

; wait on user to do stuff

$a_mPos2 = MouseGetPos()

; check to see if cursor was moved at least the number of pixels as specified by threshold values
If $a_mPos2[0] > $a_mPos1[0] + $XPixelThreshold Or $a_mPos2[0] < $a_mPos1[0] - $XPixelThreshold Or $a_mPos2[1] > $a_mPos1[1] + $YPixelThreshold Or $a_mPos2[1] < $a_mPos1[1] - $YPixelThreshold Then
    ; stuff to do
EndIf

FUNCTIONS: WinDock (dock window to screen edge) | EditCtrl_ToggleLineWrap (line/word wrap for AU3 edit control) | SendEX (yet another alternative to Send( ) ) | Spell Checker (Hunspell wrapper) | SentenceCase (capitalize first letter of sentences)

CODE SNIPPITS: Dynamic tab width (set tab control width according to window width)

Share this post


Link to post
Share on other sites



Hi, iCode. Can you please explain more about what you're trying to do? There is almost always a better way to accomplish the task than getting the Mouse position, as this can be so volatile (a bump of the desk, accidental wiggle, etc.). If you're looking to confine the mouse for example, you can use _MouseTrap. The more info you can provide us with, the better suited we are to assist. :)


√-1 2^3 ∑ π, and it was delicious!

Share this post


Link to post
Share on other sites

Hi,
No, it can not be more efficient. I tried using the WinAPI function but it's slower.

#include <StructureConstants.au3>

Local $hTimer = 0, $tPOINT = 0, $pPOINT = 0, $x = 0, $y = 0, $aMgp = 0

$hTimer = TimerInit()

$tPOINT = DllStructCreate($tagPOINT)
$pPOINT = DllStructGetPtr($tPOINT)

For $i = 1 To 1000
    DllCall("user32.dll", "bool", "GetCursorPos", "ptr", $pPOINT)

    $x = DllStructGetData($tPOINT, 1)
    $y = DllStructGetData($tPOINT, 2)
Next

ConsoleWrite("test1: " & TimerDiff($hTimer) / 1000 & @CrLf)

$hTimer = TimerInit()

For $i = 1 To 1000
    $aMgp = MouseGetPos()
Next

ConsoleWrite("test2: " & TimerDiff($hTimer) / 1000 & @CrLf)
Br, FireFox.

 

OS : Win XP SP2 (32 bits) / Win 7 SP1 (64 bits) / Win 8 (64 bits) | Autoit version: latest stable / beta.
Hardware : Intel(R) Core(TM) i5-2400 CPU @ 3.10Ghz / 8 GiB RAM DDR3.

My UDFs : Skype UDF | TrayIconEx UDF | GUI Panel UDF | Excel XML UDF | Is_Pressed_UDF

My Projects : YouTube Multi-downloader | FTP Easy-UP | Lock'n | WinKill | AVICapture | Skype TM | Tap Maker | ShellNew | Scriptner | Const Replacer | FT_Pocket | Chrome theme maker

My Examples : Capture toolIP Camera | Crosshair | Draw Captured Region | Picture Screensaver | Jscreenfix | Drivetemp | Picture viewer

My Snippets : Basic TCP | Systray_GetIconIndex | Intercept End task | Winpcap various | Advanced HotKeySet | Transparent Edit control

 

Share this post


Link to post
Share on other sites

If you're looking to reduce polling amount, have a look at windows hooks (however they need to be executable extremely fast, and any hangs in the code might hang the system). If not, no, not really.


Ever wanted to call functions in another process? ProcessCall UDFConsole stuff: Console UDFC Preprocessor for AutoIt OMG

Share this post


Link to post
Share on other sites

For some assa-9 reason i disabled auto-notification to topics. Sorry 'bout that. Some might well find it frustrating to spend the time to help someone, only to have them go AWOL :)

I'm not looking to trap the mouse or reduce polling, i'm just wondering if there isn't a better way of writing this so it runs faster...

If $a_mPos2[0] > $a_mPos1[0] + $XPixelThreshold Or $a_mPos2[0] < $a_mPos1[0] - $XPixelThreshold Or $a_mPos2[1] > $a_mPos1[1] + $YPixelThreshold Or $a_mPos2[1] < $a_mPos1[1] - $YPixelThreshold Then
    ; stuff to do
EndIf

What i am doing is detecting the cursor position when the primary is clicked ($mPos1), then waiting until it is clicked again ($mPos2), then checking if the cursor has been moved beyond a specified distance in pixels ($XPixelThreshold)

So i am checking in all 4 directions - N, S, E, W - so there are 4 of these...

$a_mPos2[0] > $a_mPos1[0] + $XPixelThreshold

...where IF position-2 is > than position-1, etc., etc., etc., THEN i know what to do next

It just seems that the way i wrote it is clumsy and slower than it could be. So i'm not looking for pretty, just fast  :bike:


FUNCTIONS: WinDock (dock window to screen edge) | EditCtrl_ToggleLineWrap (line/word wrap for AU3 edit control) | SendEX (yet another alternative to Send( ) ) | Spell Checker (Hunspell wrapper) | SentenceCase (capitalize first letter of sentences)

CODE SNIPPITS: Dynamic tab width (set tab control width according to window width)

Share this post


Link to post
Share on other sites

To get distance for x and y, just use Abs() in combination with x2-x1 or y2-y1.  That will reduce the comparisons by 2

Share this post


Link to post
Share on other sites

Took me awhile to figure out how to write it, but i think this is what you meant???

If Abs($a_mPos2[0] - $a_mPos1[0]) > $XPixelThreshold Or Abs($a_mPos2[1] - $a_mPos1[1]) > $YPixelThreshold Then

Testing tells me the time is the same, which i guess isn't bad considering another function is added, but at least it looks a little prettier AND i learnd something :)

thanks!


FUNCTIONS: WinDock (dock window to screen edge) | EditCtrl_ToggleLineWrap (line/word wrap for AU3 edit control) | SendEX (yet another alternative to Send( ) ) | Spell Checker (Hunspell wrapper) | SentenceCase (capitalize first letter of sentences)

CODE SNIPPITS: Dynamic tab width (set tab control width according to window width)

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