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…