20

I am trying to find out how to move the mouse cursor N pixels to some direction.... through a command script, since I cannot install anything on my computer.

I basically try to keep the screen active forever, until I kill the script.

(Yes, I've been searching high and low for a way to do it by a command script.... but could not find anything. I hope it's possible.)

Harry Johnston
  • 35,639
  • 6
  • 68
  • 158
rapt
  • 11,810
  • 35
  • 103
  • 145
  • I doubt that it is possible in this case. There's really no precedent for a batch script to control the mouse pointer. It sounds like there's _probably_ a better way to achieve what you're trying to achieve, whatever that is. – David Dec 10 '12 at 19:16
  • @David : any direction for a better way? – rapt Dec 10 '12 at 19:18
  • @rapt: Depends on what you're trying to achieve. "keep the screen active forever" - why? What's the goal here? There may be some Windows API calls to directly access monitor state, or turn off power saving, etc. Maybe a small utility that turns off power saving options and then turns them back on when needed? – David Dec 10 '12 at 19:22
  • @David: I am trying to keep the screen from going into sleep. Directly controlling this option is blocked on my computer. But if the mouse pointer is moving, the screen will not go into sleep. – rapt Dec 10 '12 at 19:30
  • @rapt: I'm _sure_ there's a deeper reason than just wanting to keep the screen on. How is this option blocked? Do you not have control of the computer? If that's the case then part of the solution may involve asking whoever owns the computer. In any event, a little Googling led me here: http://stackoverflow.com/questions/1949609/simulating-mouse-movement-c which talks about using the Windows API from .NET code to move the mouse. I imagine the same API calls are available from other languages as well, possibly even from batch scripts. Essentially an API call would be the way to go. – David Dec 10 '12 at 19:45
  • autohotkey installs without admin privileges, also see this question: http://stackoverflow.com/questions/12355279/i-want-my-optical-mouse-to-jitter-a-little – SeanC Dec 10 '12 at 22:06

4 Answers4

26

The most straightforward way to manipulate mouse with batch file is with

rundll32 user32.dll,SetCursorPos

But this is not very useful - just sets the mouse to 0,0 position.

Check the mouse.bat - it is a self compiled C#/batch file and does not require external tools and the source is visible and editable.

Examples:

//clicks at the current position
call mouse click

//double clicks at the current position
call mouse doubleClick

//right clicks at the current position
call mouse rightClick

//returns the position of the cursor
call mouse position

//scrolls up the mouse wheel with 1500 units
call mouse scrollUp 150

//scrolls down with 100 postitions
call mouse scrollDown 100

 //relatively(from the current position) moves the mouse with 100 horizontal and 100 vertial postitions
call mouse moveBy 100x100

//absolute positioning
call mouse moveTo 100x100

//relative drag (lefclick and move)
call mouse dragBy 300x200

//absolute drag
call mouse dragTo 500x500
npocmaka
  • 55,367
  • 18
  • 148
  • 187
  • 2
    Any ideas whats the windows 10 x64 equivalent of this? `rundll32 user32.dll,SetCursorPos` – rmn Oct 01 '18 at 06:01
  • Hello @npocmaka I'd like to ask you a question: in order to use the above examples in my batch file, should I copy your mouse.bat code inside my batch file? – Gennaro Arguzzi May 11 '22 at 08:02
  • 1
    @GennaroArguzzi - you can embed the code into your batch file , but you should be careful. I think it will be more convenient if you just put the mouse.bat in the same directory as your script and just call it with `call` – npocmaka May 11 '22 at 12:00
  • it does not work – Sav Apr 20 '23 at 10:54
  • @Sav - what does not work? Are there any errors? – npocmaka Apr 20 '23 at 17:20
  • 1
    mouse.bat works for me on Win10 64bit in 2023. Thanks! – AMTK Aug 21 '23 at 21:22
17

Search for NirCmd, and install it in C:\windows, and do:

nircmd setcursor 100 50
nircmd movecursor 10 10

or another commands for clicks etc.

MoE
  • 171
  • 2
  • 3
5

(Late answer but can still be useful for others) If you just want to keep your computer from falling asleep, the software "Caffeine" does this quite well.

SharpNip
  • 350
  • 3
  • 9
1

try setting sleep mode to 'none', but if you want to move your mouse without even touching it, download memz clean and let the "random cursor movement" to be bluish. now enjoy. (if it doesn't work, the payloads must be disabled. try doing shift+esc to enable/disable payloads. try doing ctrl+shift+s to skip some time because in some minutes the mouse will shake more better.)

Stephen
  • 27
  • 7