Jump to content

compare mouse cursor position - code efficiency


Go to solution Solved by Ascend4nt,

Recommended Posts

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)

Link to comment
Share on other sites

  • Moderators

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. :)

"Profanity is the last vestige of the feeble mind. For the man who cannot express himself forcibly through intellect must do so through shock and awe" - Spencer W. Kimball

How to get your question answered on this forum!

Link to comment
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.
Link to comment
Share on other sites

  • 2 weeks later...

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)

Link to comment
Share on other sites

  • Solution

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

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

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...