Home | Blog | Software | Reviews and Features | Forum | Help | Donate | About us
topbanner_forum
  *

avatar image

Welcome, Guest. Please login or register.
Did you miss your activation email?

Login with username, password and session length
  • December 10, 2016, 12:54:05 AM
  • Proudly celebrating 10 years online.
  • Donate now to become a lifetime supporting member of the site and get a non-expiring license key for all of our programs.
  • donate

Author Topic: move mouse to active window  (Read 10512 times)

TinyTony

  • Participant
  • Joined in 2005
  • *
  • default avatar
  • Posts: 1
    • View Profile
    • Donate to Member
move mouse to active window
« on: February 10, 2009, 10:42:54 AM »
Hi,
the idea is to move the mouse cursor automatically to the active window when the focus changes, for example: after ALT-Tab or dialog-boxes.
Its good for multimonitor systems.
I tried to code it with Autoit but i cant get it to work.

thx
Tony


sunwei415

  • Participant
  • Joined in 2007
  • *
  • default avatar
  • Posts: 3
    • View Profile
    • Donate to Member
Re: move mouse to active window
« Reply #1 on: April 03, 2009, 07:30:09 PM »
Hi, I tried to do something similiar with autohotkey, but failed.
See: move cursor to the center of newly activated window.
I will try to work on it more by calling windows api to switch window from autohotkey.

sunwei415

  • Participant
  • Joined in 2007
  • *
  • default avatar
  • Posts: 3
    • View Profile
    • Donate to Member
Re: move mouse to active window
« Reply #2 on: April 03, 2009, 07:36:21 PM »

sunwei415

  • Participant
  • Joined in 2007
  • *
  • default avatar
  • Posts: 3
    • View Profile
    • Donate to Member
Re: move mouse to active window
« Reply #3 on: April 04, 2009, 05:51:54 AM »
see the previous post for updated answer by autohotkey.

tomos

  • Charter Member
  • Joined in 2006
  • ***
  • Posts: 10,341
    • View Profile
    • Donate to Member
Re: move mouse to active window
« Reply #4 on: April 04, 2009, 01:27:02 PM »
Take a look at this:moving mouse to change active window.

I get a "No Thread specified" message there
Tom

MilesAhead

  • Supporting Member
  • Joined in 2009
  • **
  • Posts: 7,290
    • View Profile
    • Miles Ahead Software
    • Donate to Member
Re: move mouse to active window
« Reply #5 on: April 04, 2009, 01:41:24 PM »
The first link seems to be in the thread I think.


Edit: I only have a single monitor system.  But playing around with the code I find, for myself, I like the mouse cursor to be near the origin.  If you try to go to the center of the window, it's interpreted differently in different types of windows.  Plus, since MouseMove by default moves relative to the active window, you don't even need to get the position of the active window to do any calculations.  In my modification I just use 16 as offset from the top left origin.  Here's my AutoHotKey variation:

~!Tab::
KeyWait, Alt
KeyWait, Tab
MouseMove,16,16
return

« Last Edit: April 04, 2009, 04:27:54 PM by MilesAhead »

MilesAhead

  • Supporting Member
  • Joined in 2009
  • **
  • Posts: 7,290
    • View Profile
    • Miles Ahead Software
    • Donate to Member
Re: move mouse to active window
« Reply #6 on: April 05, 2009, 03:36:51 PM »
After experimenting I see that my script doesn't take account of minimized windows, which is a main reason to use AltTab in the first place.. Duh!!  :)

So now I get the window position and if either the x or y is < 0 then it's minimized, so I wait a short number of loops with a 1/10th second sleep for the window to open, then put the mouse cursor near the origin.  Here's the revised section of code:

~!Tab::
KeyWait, Alt
KeyWait, Tab
WinGetPos,x,y,width,height,A
While (x < 0 Or y < 0)
{
Sleep,100
WinGetPos,x,y,width,height,A
IfGreater,A_Index,2,Break
}
MouseMove,16,16
return


The rest of the code just does Tray menu stuff.

A compiled version is on my hotkeys page.


MilesAhead

  • Supporting Member
  • Joined in 2009
  • **
  • Posts: 7,290
    • View Profile
    • Miles Ahead Software
    • Donate to Member
Re: move mouse to active window
« Reply #7 on: April 06, 2009, 05:43:26 PM »
I added a call to EmptyWorkingSet to reduce the memory footprint on startup, in v. 1.2 .. avail for download.

lanux128

  • Global Moderator
  • Joined in 2005
  • *****
  • Posts: 6,258
    • View Profile
    • Coding Snacks by Lanux128
    • Read more about this member.
    • Donate to Member
Re: move mouse to active window
« Reply #8 on: June 26, 2009, 01:38:21 AM »
for multi-monitor setup, it'd be nice if the cursor only moves when the activated window is not on the primary monitor.

MilesAhead

  • Supporting Member
  • Joined in 2009
  • **
  • Posts: 7,290
    • View Profile
    • Miles Ahead Software
    • Donate to Member
Re: move mouse to active window
« Reply #9 on: June 26, 2009, 06:23:12 PM »
for multi-monitor setup, it'd be nice if the cursor only moves when the activated window is not on the primary monitor.

I have no way to test it.  I'm using a kvma switch. (If anyone with a MM setup wants to build on the snippet I posted they're welcome to do so.)  :)
« Last Edit: June 26, 2009, 06:59:44 PM by MilesAhead »

lanux128

  • Global Moderator
  • Joined in 2005
  • *****
  • Posts: 6,258
    • View Profile
    • Coding Snacks by Lanux128
    • Read more about this member.
    • Donate to Member
Re: move mouse to active window
« Reply #10 on: July 19, 2009, 11:38:59 PM »
this code moves the cursor to any active window if switching between apps on the primary and secondary monitor and among apps on the secondary monitor. however if you're switching among apps on primary screen, the cursor is left alone.

i had to put in some manual methods to achieve what i wanted, maybe it will be useful to someone..

;assume it's a 2-monitor setup
SysGet, Mon2, Monitor, 2

~!Tab::
KeyWait, Alt
KeyWait, Tab

WinGetActiveStats, getTitle, width, height, x, y
If x >= 1440  ; 2nd monitor as 1st monitor's res is 1440
{
  center_x:=(Mon2Right-Mon2Left)/2
  center_y:=(Mon2Bottom-Mon2Top)/2
}
Else If (x >= 0 and x < 1440) ; 1st monitor
{
   MouseGetPos, cur_x, cur_y
   If (cur_x >= 0 and cur_x < 1440)
    {
     center_x:=cur_x
     center_y:=cur_y
    }
  Else
    {
   center_x:=x+width/2
   center_y:=y+height/2
    }
}
Else If x < 0
{
; do nothing as it's probably minimized
}
MouseMove,center_x,center_y,

Return