How do I make the mouse cursor move at a custom rotation angle?

I want to make the mouse cursor move at a custom rotated angle. The idea is to input the angle of rotation form the user, then apply the rotation to the original path of the mouse cursor movement. Any help would be appreciated.

8 Comments

It's not completely clear to me. Do you mean: When the defined angle is 90 deg, you want that the mouse cursor moves vertically, when the user moves the mouse horizontally?
This can be achieved easily by rotating the mouse :-)
Shikhar wants to automate mouse rotation with a script, the question couldn't be clearer
John BG: does Shikhar Vats want to pre-record a cursor path and then replay the cursor movement with the path rotated around some point? Or does Shikhar Vats want to input the angle and then have live cursor movement have its path rotated around some point? Which point does Shikhar Vats want the path to be rotated with respect to? Or does Shikhar Vats want the appearance of the cursor to be rotated but the positioning to be left alone?
@Jan: that is exactly what I am trying... but how do I rotate the mouse tho? @Walter: i want the appearance of the cursor to be rotated about a starting point which would be at the center of the screen.
Shikhar Vats: do you want the path of movement to be rotated? Or do you want the cursor movement to be left alone, but for example the arrow cursor would tilt? For example some of the directions are shown at https://unicode-table.com/en/sets/arrows-symbols/
@Shikhar Vats: Sorry, my "rotating the mouse" meant to rotate the physical device. This is very fast, trivial and does not need any software. But I can imagine that it does not satisfy your needs and I meant this with humor. (Nevertheless, it would solve the problem immediately :-) )
Walter: I misread your question, the appearance of cursor doesn't matter, it is the path that the cursor moves that needs to be rotated. SO for example, if the angle is 90 degrees, the cursor appears to move vertically when I move my mouse horizontally.
Update: So I have been trying out a few other options. One of them being is similar to what Jan mentioned: Making the original cursor visible and have a pseudo code.
currentPoint = get (gca, 'CurrentPoint');
x0 = currentPoint(1,1);
y0 = currentPoint(1,2);
screenSize = get(0, 'screensize');%the size of screen
x_start = screenSize(3)/2;
y_start = screenSize(4)/2;
r = 30;
R = 250;
rot = 10;
y_target = R*sin(pi/2)+y_start;
x_target = R*cos(pi/2)+x_start;
%%Mouse moves into the initial circle
if (x0-x_start).^2+(y0-y_start).^2<(0.5*r).^2
tic;
start_flag = true;
pause(2);
target.FaceColor = 'g';
startTime = (clock);
num = 1;
num2 = 1;
x_group(num) = x0;
y_group(num) = y0;
x_group2(num2) = x0;
y_group2(num2) = y0;
else
%%Mouse starts moving from the initial circle
num = num+1;
x_group(num) = x0;
y_group(num) = y0;
%%Calculation of the new route
x_new = (x0-x_group(num-1))*cos(rot*pi/180)-(y0-y_group(num-1))*sin(rot*pi/180)+x_group2(num2);
y_new = (y0-y_group(num-1))*cos(rot*pi/180)+(x0-x_group(num-1))*sin(rot*pi/180)+y_group2(num2);
num2 = num2+1;
x_group2(num2) = x_new;
y_group2(num2) = y_new;
hold on;
plot_orig(num2) = plot(x0,y0,'^g');
% set(plot_orig(num2-1),'Visible','off');
plot_new(num2) = rectangle('Position',[x_new-0.5*10,y_new-0.5*10,10,10],'Curvature',[1,1], 'FaceColor','b');
delete (plot_new(num2-1));
if sqrt((x_new-x_start).^2+(y_new-y_start).^2) > 250
start_flag = false;
time_session = toc;
target.FaceColor = 'r';
end
end
This almost solves my purpose except for one problem, it shows an error(Cannot access method 'delete' in class 'matlab.ui.Root'.) in the delete(plot_new(num2-1) and randomly plots a blue circle during the path. I can't figure out how to go about solving it.
The pink circle is the starting point, red is the target and blue is the rotated mouse cursor. The green shows the original path
@Star Strider: You are correct, this is meant for the same psychology experiment which is a part of our research.

Sign in to comment.

Answers (1)

This does not work:
function myMouseTest % FAILING!!!!!!!!
Alpha = 90 * pi / 180;
Rotation = [cos(Alpha), -sin(Alpha); sin(Alpha), cos(Alpha)];
FigH = figure('WindowButtonMotionFcn', {@myMouseCheater, Rotation});
setappdata(FigH, 'oldMousePos', get(groot, 'Pointerlocation'));
drawnow;
end
function myMouseCheater(FigH, EventData, Rotation) % FAILING!!!!!!!!
oldScreenPos = getappdata(FigH, 'oldMousePos');
curScreenPos = get(groot, 'Pointerlocation');
Moved = curScreenPos - oldScreenPos;
newScreenPos = oldScreenPos + Moved * Rotation;
set(groot, 'Pointerlocation', newScreenPos);
setappdata(FigH, 'oldMousePos', newScreenPos);
end
Currently, I can test this in a virtuial machine only. There setting the pointer location by get(groot, 'Pointerlocation') does not work. I post it here, because it might be useful for others to play around.
By the way:
Robby =java.awt.Robot;
Robby.mouseMove(10, 100);
does not work also: It has no effect to the position of the mouse cursor, but it affects the mouse style (e.g. set to the arrows, when the position would be on the corner of a window). It is a VMWare 7.1.4 with a Win7/64 Enterprise guest OS.
[EDITED] When working on the desktop directly, the moving works, but I do not find a way to store the old coordinates when the mouse cursor is outside the figure. I the mouse is moved out of the window e.g. in the top right corner, the last position inside the window is stored. When the mouse enters the window again at teh bottom left corner, the WindowButtonMotionFcn thinks, that the mouse has been move in on setp along teh diagonal. Currently I do not know how to solve this.
This problem will not appear for a maximized figure, which covers the full screen. Please explain more details, Shikhar.

6 Comments

As far as I know matlab does not expose the mechanism to capture mouse movement outside of a figure. So, within matlab the only way to capture mouse movement over the entire screen is to make the window fullscreen.
I would suspect that listening to the mouse movement event and changing the mouse position after it's effectively been moved by the OS is never going to work very well and most likely will result in the cursor jumping around. To do this properly, you'd have to write a filter driver something you can't do in matlab.
Note: this applies to Windows. Don't know anything about other OSes.
@Shikhar: Do you really need the mouse to move messed up? Or would it be enough to set the original mouse cursor invisible and rendering a pseudo cursor which looks like the mouse is moved in a rotated angle?
Perhaps your problem can be solved with other methods. Please explain more details.
This seems to be an interesting psychology experiment (or skillful prank).
With respect to the psychology experiment, it would be interesting to see how long it takes a subject to discover the ‘problem’ and learn to manipulate the mouse to achieve the desired objective, for example to pursue a moving object.
@Star Strider: A colleage has pressed the hot key for rotating the screen by 180 deg by accident. He was not able to use the mouse to select the context menu on the desktop to rotate the screen again. He even tried to look from above to the monitor while standing behind it. After minutes of desperation and a lot and laughing I revealed the trivial trick: Rotate the mouse by 180 deg also, then it moves in the expected direction again. :-)
Do you remeber Apple's USB mouse? The round shape allows to identify the orientation by the position of the key only with perhaps a deviation of 10 deg. But it took less then a centimenter of movement to know exactly where "up" is amd to adjust the motion.
Update: So I have been trying out a few other options. One of them being is similar to what Jan mentioned: Making the original cursor visible and have a pseudo code.
currentPoint = get (gca, 'CurrentPoint');
x0 = currentPoint(1,1);
y0 = currentPoint(1,2);
screenSize = get(0, 'screensize');%the size of screen
x_start = screenSize(3)/2;
y_start = screenSize(4)/2;
r = 30;
R = 250;
rot = 10;
y_target = R*sin(pi/2)+y_start;
x_target = R*cos(pi/2)+x_start;
%%Mouse moves into the initial circle
if (x0-x_start).^2+(y0-y_start).^2<(0.5*r).^2
tic;
start_flag = true;
pause(2);
target.FaceColor = 'g';
startTime = (clock);
num = 1;
num2 = 1;
x_group(num) = x0;
y_group(num) = y0;
x_group2(num2) = x0;
y_group2(num2) = y0;
else
%%Mouse starts moving from the initial circle
num = num+1;
x_group(num) = x0;
y_group(num) = y0;
%%Calculation of the new route
x_new = (x0-x_group(num-1))*cos(rot*pi/180)-(y0-y_group(num-1))*sin(rot*pi/180)+x_group2(num2);
y_new = (y0-y_group(num-1))*cos(rot*pi/180)+(x0-x_group(num-1))*sin(rot*pi/180)+y_group2(num2);
num2 = num2+1;
x_group2(num2) = x_new;
y_group2(num2) = y_new;
hold on;
plot_orig(num2) = plot(x0,y0,'^g');
% set(plot_orig(num2-1),'Visible','off');
plot_new(num2) = rectangle('Position',[x_new-0.5*10,y_new-0.5*10,10,10],'Curvature',[1,1], 'FaceColor','b');
delete (plot_new(num2-1));
if sqrt((x_new-x_start).^2+(y_new-y_start).^2) > 250
start_flag = false;
time_session = toc;
target.FaceColor = 'r';
end
end
This almost solves my purpose except for one problem, it shows an error(Cannot access method 'delete' in class 'matlab.ui.Root'.) in the delete(plot_new(num2-1) and randomly plots a blue circle during the path. I can't figure out how to go about solving it.
The pink circle is the starting point, red is the target and blue is the rotated mouse cursor. The green shows the original path
@Star Strider: You are correct, this is meant for the same psychology experiment which is a part of our research.
Fortunately, I have not done that myself!
I do not remember it. I agree with the Wikipedia article that it would be a significant human-factors design error. My first (and last) Apple machine was an Apple ][ that I bought in 1979 (and now have in storage). I have used MS-DOS and Windows PCs since 1985, because they were ‘open source’ and all the devices and software was written for them, and not Apple machines.

Sign in to comment.

Asked:

on 27 Mar 2017

Edited:

on 29 Mar 2017

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!