Jump to content

Sky Slate Blueberry Blackcurrant Watermelon Strawberry Orange Banana Apple Emerald Chocolate

Detect mouse movement


  • Please log in to reply
4 replies to this topic
~Guest
  • Guests
  • Last active:
  • Joined: --
This one's been bugging me: (no pun intended)

I want to be able to detect when the mouse moves, so I can run an action, kind of like how screensavers exit on mouse movement.
-I didn't actually think it would be too hard but there are some unexpected problems.

I'm using the following script for debugging. The actions I want to run when the mouse moves, run even if I don't move the mouse.

Labelis:
Loop,
	{
	MouseGetPos, PosX1, PosY1
		Sleep, 500
		{
		MouseGetPos, PosX2, PosY2
		PosXA2 = %PosX2%
		PosXA2 += 50   ;prevents small mouse movements being detected
		PosXB2 = %PosX2%
		PosXB2 -= 50
		PosYA2 = %PosY2%
		PosYA2 += 50
		PosYB2 = %PosY2%
		PosYB2 -= 50
		if (PosX1 > PosXA)
			{
			Goto MouseMoved
			}
		else
		if (PosX1 < PosXB2)
			{
			Goto MouseMoved
			}
		else
		if (PosY1 > PosYA2)
			{
			Goto MouseMoved
			}
		else
		if (PosY1 < PosYB2)
			{
			Goto MouseMoved
			}
		}
	Sleep, 50
	}
return

MouseMoved:
MsgBox, Did you move the mouse? `n%PosX1% X1                %PosY1% Y1`n`n%PosXA2% large value X2    %PosYA2% large value Y2`n`n%PosXB2% small value X2    %PosYB2% small value Y2`n`nX1 or y1 should be outside these ranges ie. larger than a large value 2 or smaller than a small value 2.
Goto Labelis

#Persistent

Does the code need to be modified or enhanced? Or am I going about this completely the wrong way?
Thanks for your time.

Serenity
  • Members
  • 1271 posts
  • Last active:
  • Joined: 07 Nov 2004
This script produces a msgbox when the mouse is moved more than 50 pixels at one time, in any direction:

#persistent
coordmode, mouse, screen

mousegetpos, sx, sy

settimer, check, 250
return

check:
mousegetpos, cx, cy
if (cx != sx or cy != sy)
{
  ; mouse has moved, calculate by how much
  if (cx > (sx+50) or cx < (sx-50) or cy > (sy+50) or cy < (sy-50))
  {
    msgbox, mouse has moved more than 50 pixels
    mousegetpos, sx, sy ; get new mouse position
  }  
}      
return

"Anything worth doing is worth doing slowly." - Mae West
Posted Image

Starter
  • Guests
  • Last active:
  • Joined: --
I've been searching the forum and finally came accross this script by Serenity which I thought I am going to slightly modify to meet my needs i.e.
I need a single one (i.e. just 'one' as opposed to continuous non-stop clicking) left click everytime the mouse cursor moves a certain distance (e.g. >=50 pixels) and remains over that spot for some time (e.g. 1.5 sec).
I have an Evoluent Vertical Mouse 2 which has a similar functionality (see the image below), but it does continuous clicking on one spot, not a single click, which tends to screw up things for me, therefore I can't use this build-in functionality meanigfully.
So back to the script provided by Serenity - unfortunately it doesn't work for me i.e. no message is displayed regardless of the distance the mouse cursor travels i.e. > 50 pixels.
Any idea why it is not working and what needs to be modified in it, so it starts working, or some alternative solution, would be greatly appreciated.
Thank you in advance

Posted Image

Starter
  • Members
  • 3 posts
  • Last active: Mar 12 2011 03:52 AM
  • Joined: 12 Mar 2011
Please disregard my request - apparently, it was a false alarm.
My appologies, the script does work.
I had just placed the script code after the code in the script I already use, and this way by some reason the script couldn't function, but after I have moved the script code on top of my script, it started working properly.

girlgamer
  • Moderators
  • 3263 posts
  • Last active: Feb 01 2015 09:49 AM
  • Joined: 04 Jun 2010
this is a possibility too

setmousepos:
mousegetpos, x1, y1
loop
{   gosub findmousenow
    sleep 1000
}
Exitapp

findmousenow:
mousegetpos, x2, y2
if ((%x1% <> %x2%) || (%y1% <> %y2%))
{  xmax := x1+50
   xmin := x1-50
   ymax := y1+50
   ymin := y1-50
   If x2 not between %xmax% And %xMin%
        goto gotmousemove
   if y2 not between %ymax% and %ymin%
        goto gotmousemove
return

gotmousemove:
; do whatever you need to do on a mouse move here
Return

The universe is a wondrous place! The faster you create unbreakable code, the faster the universe creates people that can break it. All scripting follows the rule Rule Of Twos -- 1) Good, 2) Fast 3) Cheap -- pick any Two.
I guarantee absolutely nothing about any code I provide except that it works in my machine. ‚óŹ
MMO Fighter   KeyLooperDemo   Key Spammer   TinyClickRecorder  GGs Password Generator.ahk
For the newest version of AutoHotkey and some killer scripts go here.
Rock-on%20kitten.gif