Controlling Mouse Pointer/Cursor Using MATLAB Code
CODE:-
clc;
clear all;
import java.awt.Robot;
vks = Robot; vks.mouseMove(0,0);
s_size = get(0, 'screensize');
a_ratio = s_size(3)/s_size(4);
for i = 1:s_size(4)
vks.mouseMove(a_ratio*i, i);
pause(0.01);
end
import java.awt.Robot;
vks = Robot; vks.mouseMove(0,0);
s_size = get(0, 'screensize');
a_ratio = s_size(3)/s_size(4);
for i = 1:s_size(4)
vks.mouseMove(a_ratio*i, i);
pause(0.01);
end
Line By Line Explanation Of Above Code:-
# clc; --> It wll clear the Command Window
# clear all;--> It will clear all the variable present in the memory that time
# import java.awt.Robot;--> It will import java.awt's Robot class.
Package java.awt Description:- Contains all of the classes for creating user interfaces and for painting graphics and images. A user interface object such as a button or a scrollbar is called, in AWT terminology, a component. The Component class is the root of all AWT components. Some components of it shoots events when a user interacts with the components. The AWTEvent class and its subclasses are used to represent the events that AWT components can shoot.
# vks=Robot; --> assigning the value of imported Robot module of java.awt package to variable names vks
# vks.mouseMove(0,0); --> utilizing mouseMove() function of class Robot to move the mouse cursor at (0,0) screen location
# s_size = get(0, 'screensize'); --> get() function will return the current system's screen size & get stored in s_size variable.
# a_ratio = s_size(3)/s_size(4);--> the s_size variable will contain a 1x4 matrix, of whose the 1st 2 variables are 1 & 1. The next two entries will give the width & height respectively. In our case s_size contains [1,1,1366,768], which may differ in your case.
# for i = 1:s_size(4) --> here you notice there is no ';' (semicolon), in loops we avoid that. Since if we do that, it will not have any effect in successive lines. The value of 'i' will vary from 1 to s_size(4)
# vks.mouseMove(a_ratio*i, i); --> this will set the pointer location at the coordinates (a_ratio*i,i), for each value of 'i'.
# pause(0.01); --> this will set the delay interval of 0.01 seconds between the mouse pointer placement in different different locations.
# end--> it will end the for loop.
## The result of this program will be, mouse pointer will move diagonally from top right corner and end up at bottom left corner of the computer screen.
1 comments: