Showing posts with label mouse. Show all posts
Showing posts with label mouse. Show all posts

Tuesday, January 24, 2012

MATLAB GUI: Tracking mouse actions

In the second tutorial of the series, we will look at how we can track the mouse actions such as pressing and releasing. We will start with the GUI we created in the first tutorial, and we will add a text field that details the last mouse action and its location.

Step-by-step guide

1. Download mouse1.zip, and ready the files in a working folder of MATLAB.
2. Open gui_mouse.fig using GUIDE.
3. Add a static text to the GUI and rename its tag to lbl_last_action
4. Open the property window of the figure (double click on the figure), click on WindowButtonDownFcn to populate the callback and paste the following code:
    pos = get(hObject, 'currentpoint'); % get mouse location on figure
    x = pos(1); y = pos(2); % assign locations to x and y
    set(handles.lbl_last_action, 'string', ['Mouse pressed @ X: ', num2str(x), ', Y: ', num2str(y)]);
5. Click on WindowButtonUpFcn to populate the callback and paste the following code: 
    pos = get(hObject, 'currentpoint'); % get mouse location on figure
    x = pos(1); y = pos(2); % assign locations to x and y
    set(handles.lbl_last_action, 'string', ['Mouse released @ X: ', num2str(x), ', Y: ', num2str(y)]);
6. Press F5 (or click on run button) to start up the GUI.

Anytime when the mouse is pressed (down) or released (up), the text field is updated with the action and the location of the action. Note that if the button is released quickly after pressed, the text field may be updated too quickly as if there is no press action.

Video 


Files: 

Files can be downloaded here. Note some changes have been made to the GUI to make it more presentable.

Saturday, January 21, 2012

MATLAB GUI: Tracking mouse locations


In the first part of the series, we will create a simple GUI that will show the current position of the mouse on the GUI.

Step-by-step guide:
  1. Create new GUI using MATLAB GUIDE, save file as gui_mouse
  2. Create two static text fields and configure them as fellow
    1. rename tags to lbl_x and lbl_y
    2. set strings to 'x loc:' and 'y loc:'
    3. set FontSize to 18, and resize components 
  3. Configure figure property (double-click on figure)
    1. rename tag to fig_mouse
    2. change unit to pixels
    3. click on callback for WindowButtonMotionFcn and insert the following code
    pos = get(hObject, 'currentpoint'); % get mouse location on figure
    x = pos(1); y = pos(2); % assign locations to x and y
    set(handles.lbl_x, 'string', ['x loc:' num2str(x)]); % update text for x loc
    set(handles.lbl_y, 'string', ['y loc:' num2str(y)]); % update text for y loc 


  4. Save all files and run the GUI or M-file.
Note: anytime the mouse is moved over the GUI, it will trigger the WindowButtonMotionFcn on the figure, which will then update the labels with the current location of the mouse pointer on the figure. 

Video:




Files:

Download source files here