Jump to content

Sky Slate Blueberry Blackcurrant Watermelon Strawberry Orange Banana Apple Emerald Chocolate
Photo

Keystroke to click position on screen


  • Please log in to reply
13 replies to this topic
Met
  • Members
  • 11 posts
  • Last active: Jan 06 2014 09:56 AM
  • Joined: 14 Jan 2013

Hi, I am very new to AHK and have no experience what-so-ever.  However, I was wondering if there is a script that can click a certain location onscreen when a certain key is pressed, without effecting the current mouse location.  If anyone could help me out that would be great.



ruespe
  • Members
  • 567 posts
  • Last active: Dec 01 2014 07:59 PM
  • Joined: 17 Jun 2008
Have a look into the help-file or here: http://www.autohotke...ocs/Hotkeys.htm and http://www.autohotke.../MouseClick.htm

dmg
  • Members
  • 2395 posts
  • Last active: Nov 04 2015 06:46 AM
  • Joined: 19 Nov 2010

It depends on your specific needs. There is a command called ControlClick which can send a simulated click to a control without moving the cursor, but not everything IS a control:

http://www.autohotke...ontrolClick.htm

 

Then there is the MouseClick command, like ruespe posted. But it moves the cursor to the desired position.

 

Are you really needing to click a location without moving the cursor, or will VERY quickly moving the cursor and returning it to its starting point be good enough?


"My dear Mr Gyrth, I am never more serious than when I am joking."
~Albert Campion

-----------------------------------------------------------------------------------------------
Website | Demo scripts | Blog | External contact

Met
  • Members
  • 11 posts
  • Last active: Jan 06 2014 09:56 AM
  • Joined: 14 Jan 2013

Thank you for all your help thus far, and in response to dmg, moving it very quickly will be okay.



adrianh
  • Members
  • 616 posts
  • Last active: Apr 07 2016 03:35 PM
  • Joined: 28 Oct 2012
Then you should probably be good with Click and MouseGetPos

http://l.autohotkey....mands/Click.htm
http://l.autohotkey....MouseGetPos.htm


Adrian

my library base
AHK_L is the bomb! With a whole lot of bug fixes, Unicode support, associative array objects, array like objects, classes and variadic functions, why wouldn't you switch?


dmg
  • Members
  • 2395 posts
  • Last active: Nov 04 2015 06:46 AM
  • Joined: 19 Nov 2010

Try this:

x := ;place your desired coordinates in these two variables.
y := 

f1::
 {
   mousegetpos, start_x, start_y
   mouseclick, left, %x%, %y%, 1, 0
   mousemove, %start_x%, %start_y%, 0
 }
return

This script will get the current mouse sposition using MouseGetPos:

http://www.autohotke...MouseGetPos.htm

Then uses MouseClick at its highest speed to move the cursor and click the coordinates in the x and y variables. Then it uses MouseMove to move the cursor back to where it was when you pressed the hotkey:

http://www.autohotke...s/MouseMove.htm

 

You just have to provide the coordinates you want clicked.


"My dear Mr Gyrth, I am never more serious than when I am joking."
~Albert Campion

-----------------------------------------------------------------------------------------------
Website | Demo scripts | Blog | External contact

Met
  • Members
  • 11 posts
  • Last active: Jan 06 2014 09:56 AM
  • Joined: 14 Jan 2013

Thank you very much for all your help dmg, adrianh and ruespe, I greatly appreciate it.



richard22
  • Members
  • 11 posts
  • Last active: Jan 17 2013 10:34 PM
  • Joined: 11 Jan 2013

The way i do it by useing most of the code that dmg above is using but I also use auto it v3 for the auto it window info tool that comes with it.  Under the mouse tab in the window info tool you can find the position of your mouse on the screen.



Met
  • Members
  • 11 posts
  • Last active: Jan 06 2014 09:56 AM
  • Joined: 14 Jan 2013

Okay I got it to work, there's only one issue I am really having at this point, sometimes the mouse will click before it moves it to the location.  Any tips on countering that?

x := 1249
y := 213
a := 1158
b := 213
c := 1307
d := 213
e := 1335
f := 213
f1::
 {
   mousegetpos, start_x, start_y
   mouseclick, left, %x%, %y%, 1, 0
   mousemove, %start_x%, %start_y%, 0
 }
return 

f5::
 {
   mousegetpos, start_x, start_y
   mouseclick, left, %a%, %b%, 1, 0
   mousemove, %start_x%, %start_y%, 0
 }
return

f3::
 {
   mousegetpos, start_x, start_y
   mouseclick, left, %c%, %d%, 1, 0
   mousemove, %start_x%, %start_y%, 0
 }
return

f4::
 {
   mousegetpos, start_x, start_y
   mouseclick, left, %e%, %f%, 1, 0
   mousemove, %start_x%, %start_y%, 0
 }
return


dmg
  • Members
  • 2395 posts
  • Last active: Nov 04 2015 06:46 AM
  • Joined: 19 Nov 2010

It should not be possible for it to click before it gets to the coordinates. Are you sure that is what is happening?

 

If it is then one thing you might try is to slow down the MouseClick speed a little. It may have no effect but it is worth trying.


"My dear Mr Gyrth, I am never more serious than when I am joking."
~Albert Campion

-----------------------------------------------------------------------------------------------
Website | Demo scripts | Blog | External contact

Met
  • Members
  • 11 posts
  • Last active: Jan 06 2014 09:56 AM
  • Joined: 14 Jan 2013

Delayed it to .1s, still does it.



ruespe
  • Members
  • 567 posts
  • Last active: Dec 01 2014 07:59 PM
  • Joined: 17 Jun 2008

Miracle. Try mousemove, %x%, %y%, 0 before clicking.



dmg
  • Members
  • 2395 posts
  • Last active: Nov 04 2015 06:46 AM
  • Joined: 19 Nov 2010

Yes, using MouseMove followed by Click should theoretically fix it, but it should not be possible for it to happen in the first place.

 

@Met:

Can you explain what you are observing in detail please? If it is clicking before it gets to the coordinates it may be a bug that needs to be fixed.


"My dear Mr Gyrth, I am never more serious than when I am joking."
~Albert Campion

-----------------------------------------------------------------------------------------------
Website | Demo scripts | Blog | External contact

adrianh
  • Members
  • 616 posts
  • Last active: Apr 07 2016 03:35 PM
  • Joined: 28 Oct 2012
Hmmmm, interesting.  http://l.autohotkey..../MouseClick.htm
The SendPlay mode is able to successfully generate mouse events in a broader variety of games than the other modes. In addition, some applications and games may have trouble tracking the mouse if it moves too quickly. The speed parameter or SetDefaultMouseSpeed can be used to reduce the speed (in the default SendEvent mode only).
 
So, perhaps using SendPlay mode is a better option, so put this at the beginning of the script.
SendMode Play
Also, I would recommend using a function as this potentially could do something bad when using the same global variables. (At worst though it would appear to move the mouse, though still click correctly)

Like this:
SendMode Play

x := 1249
y := 213
a := 1158
b := 213
c := 1307
d := 213
e := 1335
f := 213

f1::MoveClickBack(x, y)
f5::MoveClickBack(a, b)
f3::MoveClickBack(c, d)
f4::MoveClickBack(e, f)

MoveClickBack(x, y)
{
   local start_x, start_y
   mousegetpos, start_x, start_y
   mouseclick, left, %x%, %y%, 1, 0
   mousemove, %start_x%, %start_y%, 0
}
It is also cleaner and reduces copy coding which is a bad practice anyway. When you put a command right after the ::, the return is explicit after the line is executed. A further FYI, using the {} where you did does nothing for scoping, it is just a visual for humans.


Adrian

my library base
AHK_L is the bomb! With a whole lot of bug fixes, Unicode support, associative array objects, array like objects, classes and variadic functions, why wouldn't you switch?