Windows command script that moves mouse cursor N pixels? - windows

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.)

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

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.

(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.

You could try installing AutoHotKey. It makes controlling mouse very simple. Example script:
#MaxThreadsPerHotkey 3
^g::
Toggle := !Toggle
Loop
{
If (!Toggle)
Break
Click
Sleep 1000 ; Make this number higher for slower clicks, lower for faster.
}
Return
After running script for this example press control+g and it will click every second to keep screen awake. ctrl+g again to stop.
install at: https://www.autohotkey.com/
found solution: https://autohotkey.com/boards/viewtopic.php?t=19846

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.)

Related

How to track mouse movements without limiting it to screen size?

I'm using WM_MOUSEMOVE to get changes in mouse position. When simulating "knobs" for example it's desired to let the user go up/down with mouse without any limits. In this cases I hide cursor and use SetCursorPos to change its position every time user moves with it and detect just the difference from the original position.
Unfortunately it doesn't seem to work - if I set the mouse position, it sometimes works, but sometimes is one or more pixels away, which is just wrong. And even bigger trouble is that after the call another WM_MOUSEMOVE seems to be delivered, which unfortunately does the same thing as it wants to move the cursor back to the original position again. So it ends up in an infinite cycle or settings mouse position and receiving messages until the user releases the mouse button.
What's the correct approach or what's the problem?
The raw input system can do this - it lets you register for raw mouse input that isn't clipped or confined to the screen boundaries.
Broadly speaking, you register for raw input using RegisterRawInputDevices(). Your window will then receive WM_INPUT messages, which you process using the GetRawInputData() function.
See Using Raw Input for an example.
I hide cursor and use SetCursorPos to change its position every time user moves with it and detect just the difference from the original position.
This is just plain wrong. Instead, use SetCapture() to capture the mouse. All movements will be reported as WM_MOUSEMOVE messages with coordinates that are relative to the specified window, even if the mouse is outside of that window, until you release the capture.
Asking the user to move the mouse continuously, even after the cursor hit the screen limit is a very bad idea in terms of User Interface, IMHO.
Some games have another approach: when the mouse hit the "limit", the game enter a special mode: things appears to function exactly as if the mouse was moving, even if the user don't move it. When the user wants to exit that mode, he just has to move the mouse of the limit.
Doing so requires a timer, armed when the mouse hit some limit, executing code periodically as if the mouse was moving. The timer is stopped when a real mouse movement makes it leaves the limit.
Ok folks, so I found a solution simple enough:
The main problem is that SetCursorPos may not set the coordinates accurately, I guess it's because of some high resolution processing, nevertheless it's probably a bug. Anyway if SetCursorPos doesn't set the coordinates correctly (but +-1 in x and/or y) it also sends WM_MOUSEMOVE to the target window. As a result the window performs the exact same operation as before and this goes on and on.
So the solution is to remove all WM_MOUSEMOVE messages right after SetCursorPos:
MSG msg;
while (::PeekMessage(&msg, NULL, WM_MOUSEMOVE, WM_MOUSEMOVE, PM_REMOVE)) { };
Then retrieving the current mouse cursor pos using ::GetCursorPos .
It's ugly but seems to fix the problem. It basically seems that in some position of the mouse, the system always adds or subtracts 1 in either coordinate, so this way you let system do the weird stuff and use the new coordinates without trying to persuade system that your coordinates are the correct ones :).

alarm on screen change

I have to use a software which list my clients processes. I need a sound alert program if something change on 50x10 pixel region. I try to write a program on autohotkey but i can't succeed in. Anybody have this program?
Here is an example that you can use.
^Launch_Media:: ; Make a reference screenshot with NirSoft NIRCMD by pressing Ctrl+Media or any other program...
run, "C:\Program Files\1 My Programs\nircmd.exe" savescreenshot "c:\Temp\Screenshot.bmp" 33 40 17 20 ; Location of "Save As" Icon in SciTE4AutoHotKey Editor
Return
Launch_Media:: ; Launch this test manually with Media Button
CoordMode Pixel ; Interprets the coordinates below as relative to the screen rather than the active window.
ImageSearch, FoundX, FoundY, 0, 0, 200, 200, C:\Temp\Screenshot.bmp ; search for image in area staring at 0,0 to 200,200
if ErrorLevel = 2
MsgBox Could not conduct the search.
else if ErrorLevel = 1
MsgBox Image could not be found on the screen.
else
SoundBeep, 1000, 1000
MsgBox The Image was found at %FoundX% %FoundY%.
ClickX:=FoundX + 5 ; Move the mouse click away from the edge of the icon
ClickY:=FoundY + 5 ; Move the mouse click away from the edge of the icon
Click, %ClickX%, %ClickY% ; Click on the Save As icon.
Return
When we know more about what you want to test (I think that an area of 50x10 might be too small), and what you want to do in case the area has changed, we might be able to help you with a more suited script.
In this example I used NirSoft's nircmd.exe, but you can create a reference image by other means as well. If You only need an audible alarm, you can comment all the other commands under if, then, else out with ;.
Did you check this post from 2 years ago in the AutoHotKey community?
http://www.autohotkey.com/board/topic/56219-fast-screen-change-check-using-histograms/
The above script might be a little overwhelming, but you can also create a screenshot of a fixed area and do an image comparison in AutoHotKey as others have done before you.

Restrict mouse movement over a specified window handle

I'm looking to simulate a kiosk mode for Safari on Windows. OSX will not work with my input hardware and Chrome's GPU acceleration is too slow for the machine I'm using.
The only plausible solution [so far] is to run Safari and send an F11 (fullscreen) keystroke, but prevent the URL bar from expanding when the mouse reaches the top pixels of the screen.
I've looked and can't seem to find any good solution and would like to know if I can restrict the cursor movement from reaching the top pixel of the screen?
If anyone has any other solutions, that would be great!
You can use the ClipCursor function to do this.
Confines the cursor to a rectangular area on the screen. If a subsequent cursor position (set by the SetCursorPos function or the mouse) lies outside the rectangle, the system automatically adjusts the position to keep the cursor inside the rectangular area.
You can poll the cursor position and correct it using a timer, but this is not ideal. You could also cover the top bar by a transparent topmost window. This way, input will never reach the top bar.
EDIT: If Internet explorer is an option you have the possibility to use the COM object to embed what you need in a custom application. Other browsers might have similar APIs, but I'm not familiar with them.

Autohotkey Mousemove Wrong Monitor

I'm using mousegetpos to get the current mouse position. I click somewhere else. Then I try to restore the original postion with mousemove. The mouse moves to a different monitor. I tried the alternative method dllcall, with no success. How do I move the mouse back to the original monitor?
It's easier to help if you post your code - then people can see where you're going wrong.
This works fine for me when pressing the Ctrl-T hotkey:
CoordMode, Mouse, Screen
^t::
MouseGetPos, x, y
; Do Stuff Here.
MouseMove, x, y
return
The CoordMode, Mouse, Screen line sets the coordinates relative to the entire screen rather than the active window. I've tested this on my multiple monitor setup and the mouse goes back to the original location every time, even across monitors. Let me know if it's not working for you.
Also, just to make things a little smoother, you can set the mouse speed to '0' before moving the mouse with:
SetDefaultMouseSpeed, 0
This makes the mouse appear to move instantly which looks a little cleaner in most scripts.
I can confirm that Gary's answer works perfectly for anybody else out there having similar problems. Thanks, Gary!
I was myself having a problem like this with Breakaway Audio Enhancer...
For anybody that uses or knows Breakaway, you have to double-click on the toolbar (in the taskbar) to mute it. The way Breakaway works with the sound pipeline other standard AHK mute scripts won't work, so moving the mouse to the toolbar and double-clicking is really the only method of muting. I wanted Caps Lock to mute (or unmute) audio and preferably have the mouse return to where it originally was.
I've had countless problems trying to get this to work with multiple monitors until Gary's post, so here is my solution for anybody else having similar issues:
Capslock::
BlockInput On
CoordMode, Mouse, Screen
MouseGetPos, xpos, ypos
MouseClick, left, 42, 965, 2 ;change the co-ordinates to match your system
MouseMove, xpos, ypos
SetDefaultMouseSpeed, 0
BlockInput Off
Return

how to implement a double click?

Let me explain what we are doing:
We have designed a frame of ir sensors/receivers that can be put around a screen, and basically it converts the screen into a touch-less screen. We are able to move the mouse cursor around when the user moves his finger inside the frame and we can also generate a right click, simply by seeing that if the user is holding his/her finger over the same coords for x amount of time then generate a right click.
The problem is double click. With a regular mouse a user simply double clicks the button and done. Any thoughts on how this can be achieved?
Thank you.
You would need to figure out if the finger is within the screen at all; once you've got a mechanism that tells you that - you can check for this sequence:
Time 0: (finger off screen)
Time 1: (finger touches P1)
Time 2: (finger off screen)
Time 3: (finger touches P2)
where P1 and P2 are close (up to a preset tolerance level) and Time 3 and Time 1 are close (up to a preset double-click sensitivity level).
A solution would be to use different timers for Right click and double click. When you place the finger over a clickable element, a circular progress bar appears at the position of the finger, when the progress reachs the end (the circle is complete) and the user moves the finger away a right click is detected. If the user doesn't move away the finger, a different progress bar appears, if that progress bar reachs 100%, then you have a double click.
In my opinion, a much better solution would be to NOT need a double click, or even a right click, check the XBox Kinect menu navigation interface for a good example.
Hope it helps.
Regards.

Resources