iCode Posted July 16, 2013 Share Posted July 16, 2013 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 More sharing options...
Moderators JLogan3o13 Posted July 16, 2013 Moderators Share Posted July 16, 2013 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 More sharing options...
FireFox Posted July 16, 2013 Share Posted July 16, 2013 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 More sharing options...
Shaggi Posted July 16, 2013 Share Posted July 16, 2013 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 Link to comment Share on other sites More sharing options...
iCode Posted July 30, 2013 Author Share Posted July 30, 2013 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 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 More sharing options...
Solution Ascend4nt Posted July 30, 2013 Solution Share Posted July 30, 2013 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 My contributions: Performance Counters in Windows - Measure CPU, Disk, Network etc Performance | Network Interface Info, Statistics, and Traffic | CPU Multi-Processor Usage w/o Performance Counters | Disk and Device Read/Write Statistics | Atom Table Functions | Process, Thread, & DLL Functions UDFs | Process CPU Usage Trackers | PE File Overlay Extraction | A3X Script Extract | File + Process Imports/Exports Information | Windows Desktop Dimmer Shade | Spotlight + Focus GUI - Highlight and Dim for Eyestrain Relief | CrossHairs (FullScreen) | Rubber-Band Boxes using GUI's (_GUIBox) | GUI Fun! | IE Embedded Control Versioning (use IE9+ and HTML5 in a GUI) | Magnifier (Vista+) Functions UDF | _DLLStructDisplay (Debug!) | _EnumChildWindows (controls etc) | _FileFindEx | _ClipGetHTML | _ClipPutHTML + ClipPutHyperlink | _FileGetShortcutEx | _FilePropertiesDialog | I/O Port Functions | File(s) Drag & Drop | _RunWithReducedPrivileges | _ShellExecuteWithReducedPrivileges | _WinAPI_GetSystemInfo | dotNETGetVersions | Drive(s) Power Status | _WinGetDesktopHandle | _StringParseParameters | Screensaver, Sleep, Desktop Lock Disable | Full-Screen Crash Recovery Wrappers/Modifications of others' contributions: _DOSWildcardsToPCRegEx (original code: RobSaunder's) | WinGetAltTabWinList (original: Authenticity) UDF's added support/programming to: _ExplorerWinGetSelectedItems | MIDIEx UDF (original code: eynstyne) (All personal code/wrappers centrally located at Ascend4nt's AutoIT Code) Link to comment Share on other sites More sharing options...
iCode Posted July 30, 2013 Author Share Posted July 30, 2013 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 More sharing options...
Recommended Posts
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 accountSign in
Already have an account? Sign in here.
Sign In Now