Manipulating Mouse Cursor with MATLAB (and Upgrading Pong:D)!


Last week, I accidentally found that desktop properties can be accessed in MATLAB by using get(0) command and can also be modified by using get(0,ParameterName,ParameterValue). Among all those desktop properties, you can access many parameters such as your monitor screen size, color depth, etc, but the most interesting parameter for me was ‘PointerLocation’. Yup, this parameter lets you know the position of your mouse cursor and you can also modify it. So, to learn more about this thing, I created a script m-file to move my cursor randomly around my screen. Here are my script:

ScreenSize=get(0,'ScreenSize');
while 1
    set(0,'PointerLocation',[rand*ScreenSize(3),rand*ScreenSize(4)])
    pause(0.2)
end

After I successfully move my cursor randomly and also endlessly:D (Yup, if you execute my script, you’ll need to press ctrl+C to terminate the script and exit the infinite loop:D, so that you can use MATLAB for another thing and stop moving your mouse cursor randomly), I created another script to read cursor location and display them into MATLAB command window. Here is the location reading script:

while 1
    mousepos=get(0,'PointerLocation');
    disp(['Current Mouse Location: ',...
          num2str(mousepos(1)),',',num2str(mousepos(2))])
    pause(0.2)
end

Based on my to script above, I noticed that I can use get(0,’PointerLocation) to manipulate things in GUI program. So I decided to take a quick look at my Pong m-files and apply the command in it, so that the player can also use mouse to move the paddle. After a few minutes of tweaking and debugging, Pong with mouse control is done! So, I’m replacing the old Pong codes with this one and you can download it from my MATLAB Fun Toolbox page. Here is a video of me playing Pong with mouse control. I’m sorry for the bad quality, because I recorded it using my handphone (and I’m sorry for the audio because I was watching One Tree Hill at my other laptop when I recorded this:D).

Well, I think that’s all that I’m gonna write today. I hope this tips is useful for you. Now, back to my thesis:D…

Regards,

0X_R

This entry was posted in Uncategorized and tagged , , , . Bookmark the permalink.

3 Responses to Manipulating Mouse Cursor with MATLAB (and Upgrading Pong:D)!

  1. nikhil srivastava says:

    thanx i have beeen searching since last 8 hours!!!!
    best wishes from me for ur great career ahead

  2. It helped me a lot doing a task in my project.
    Thanks a lot !🙂

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s