Presentation is loading. Please wait.

Presentation is loading. Please wait.

Written by: Itzik Ben Shabat Technion - Israel Institute of Technology Faculty of Mechanical Engineering Laboratory for CAD & Lifecycle Engineering Lab.

Similar presentations


Presentation on theme: "Written by: Itzik Ben Shabat Technion - Israel Institute of Technology Faculty of Mechanical Engineering Laboratory for CAD & Lifecycle Engineering Lab."— Presentation transcript:

1 Written by: Itzik Ben Shabat Technion - Israel Institute of Technology Faculty of Mechanical Engineering Laboratory for CAD & Lifecycle Engineering Lab 6 : Input

2 Written by: Itzik Ben Shabat Tutorial contents Callbacks and Function Handles Keyboard input processing Implementation notes - keyboard Mouse input processing Implementation notes - mouse Simple Example Updating the Display Lab exercise Technion - Israel Institute of Technology Faculty of Mechanical Engineering Laboratory for CAD & Lifecycle Engineering

3 Written by: Itzik Ben Shabat Callbacks and Function Handles Callbacks are functions which execute when a specific event occurs (keyboard press, mouse click etc.) Function handle callbacks must define at least two input arguments in the function definition: where did the event happen? - The handle of the object generating the callback (the source of the event) what happened? - The event data structure (can be empty for some callbacks) MATLAB passes these two arguments implicitly whenever the callback executes For further information on the subject refer to matlab documentation ‘figure properties’figure properties Technion - Israel Institute of Technology Faculty of Mechanical Engineering Laboratory for CAD & Lifecycle Engineering

4 Written by: Itzik Ben Shabat Keyboard Inputs Keyboard KeyPressFcn - Executes whenever you press a keyboard key while the figure is in focus WindowKeyPressFcn - Executes whenever a key press occurs KeyReleaseFcn - Executes whenever you release a keyboard key while the figure is in focus WindowKeyReleaseFcn - Executes whenever a key release occurs Technion - Israel Institute of Technology Faculty of Mechanical Engineering Laboratory for CAD & Lifecycle Engineering

5 Written by: Itzik Ben Shabat Implementation Notes - Keyboard CallBack definitions In main set(Figureh,'KeyPressFcn',@KeyboardCB); Define the CB function function [ ] = KeyboardCB(hObject,event) hObject,event are implicit (MATLAB extracts them automatically) Extracting information: Which key was pressed? event.key – returns a char or a string of the key Technion - Israel Institute of Technology Faculty of Mechanical Engineering Laboratory for CAD & Lifecycle Engineering

6 Written by: Itzik Ben Shabat Mouse Inputs Mouse WindowButtonDownFcn - Executes whenever you press a mouse button while the pointer is in the figure window ButtonDownFcn - Executes whenever you press a mouse button while the pointer is in the figure window, but not over a child object WindowButtonMotionFcn - Executes whenever you move the pointer within the figure window WindowButtonUpFcn - Executes whenever you release a mouse button WindowScrollWheelFcn - Executes when the mouse wheel is scrolled Technion - Israel Institute of Technology Faculty of Mechanical Engineering Laboratory for CAD & Lifecycle Engineering

7 Written by: Itzik Ben Shabat Implementation Notes - Mouse Callbak definitions are the same as for keyboard Extracting information: Where did you press the mouse button? get(handle,'CurrentPoint'); returns [x y] position as a 2 element vector. Note that [x y] are in handle coordinates (figure/axis) Which mouse button did you press? get(fig_handle,'SelectionType'); 1. normal: left mouse button 2. extend: right mouse button on a 2-button mouse; middle mouse button on a 3-button mouse 3. alt: left + right buttons on a 2-button mouse; right mouse button on a 3-button mouse 4. open: double mouse click Technion - Israel Institute of Technology Faculty of Mechanical Engineering Laboratory for CAD & Lifecycle Engineering

8 Written by: Itzik Ben Shabat Simple Example Copy the following code into a new.m file and save it as ‘main5.m’ function [ ] = main5( ) %main5 demonstrates how to process keybord and mouse inputs clear all; close all; clc; %Initialize Variables Figureh=0; Axesh=0; Objecth=0; bgcolor='k'; %code Figureh=figure('NumberTitle','off','Name','Itzik Clock',... 'Position',[200 200 width height],'Color',bgcolor); %bg is set to red so we know that we %can only see the axes Axesh=axes('Color','c','Position',[0 0 1 1],'XTick',[],'YTick',[]); daspect([1 1 1]); end Technion - Israel Institute of Technology Faculty of Mechanical Engineering Laboratory for CAD & Lifecycle Engineering

9 Written by: Itzik Ben Shabat Simple Example Write a keyboard call back function that will do the following When a key is pressed display the text ‘I pressed ‘KeyChar’ key’ When a mouse click occurs displays the X,Y coordinates of the cursor in the window Technion - Israel Institute of Technology Faculty of Mechanical Engineering Laboratory for CAD & Lifecycle Engineering

10 Written by: Itzik Ben Shabat Simple Example Add the following to main5.m set(Figureh,'KeyPressFcn',@KeyboardCB); set(Figureh,'WindowButtonDownFcn',@MouseDownCB); Add the following functions function [ ] = KeyboardCB(hObject,event) %Example print of the key which was pressed fprintf('I pressed %c key\n',event.Key); End function [ ] = MouseDownCB(hObject,event) %Example print X,Y coordinates when mouse is clicked CursorPos=get(hObject,'CurrentPoint'); WhichButton=get(hObject,'SelectionType'); disp(['You clicked X:',num2str(CursorPos(1)),', Y:',num2str(CursorPos(2))]); end Technion - Israel Institute of Technology Faculty of Mechanical Engineering Laboratory for CAD & Lifecycle Engineering

11 Written by: Itzik Ben Shabat Updating The Display When the CallBacks generate a change in the graphics we must refresh\redraw the scene drawnow drawnow causes figure windows and their children to update, and flushes the system event queue. Any callbacks generated by incoming events (e.g., mouse or key events) are dispatched before drawnow return Technion - Israel Institute of Technology Faculty of Mechanical Engineering Laboratory for CAD & Lifecycle Engineering

12 Written by: Itzik Ben Shabat Lab Exercise Use the code you wrote for main5 and save it to a new.m file and call it ‘yourname_lab5_sub.m’ Change the program to do the following plot a marker in the location of the curser whenever you click it When ‘r’ is pressed delete all markers (hint explore cla ) Tips– don’t forget to use hold all to save the previously defined properties Don’t forget to define axis limits to avoid autofit Optional – change marker face color according to the mouse button which was pressed (for example – yellow for left click, red for right click and blue for center click) Technion - Israel Institute of Technology Faculty of Mechanical Engineering Laboratory for CAD & Lifecycle Engineering

13 Written by: Itzik Ben Shabat Lab Exercise Yor Final Callback functions should look something like this Technion - Israel Institute of Technology Faculty of Mechanical Engineering Laboratory for CAD & Lifecycle Engineering

14 Written by: Itzik Ben Shabat Final Notes Highly recommended – DO NOT CREAT GRAPHICH OBJECTS IN INPUT CALLBACK FUNCTIONS! ! ! This example given here are for the simplicity. A good implementation is to create a DrawingCB function – all drawings are done in it Example file will be published after this tutorial Technion - Israel Institute of Technology Faculty of Mechanical Engineering Laboratory for CAD & Lifecycle Engineering

15 Written by: Itzik Ben Shabat Sources and Refrences Images Keyboard - http://itdisplay.com/wp-content/uploads/2013/06/16-e1372093154520.jpg- http://itdisplay.com/wp-content/uploads/2013/06/16-e1372093154520.jpg Mouse - https://encrypted-tbn0.gstatic.com/images?q=tbn:ANd9GcSM86svPd1lC- AUojJy6MyUo4z-ZnUZ6jh6lJvXItQF53-LnZPvUwhttps://encrypted-tbn0.gstatic.com/images?q=tbn:ANd9GcSM86svPd1lC- AUojJy6MyUo4z-ZnUZ6jh6lJvXItQF53-LnZPvUw Mathworks Documentation Center - http://www.mathworks.com/help/documentation-center.html http://www.mathworks.com/help/documentation-center.html Technion - Israel Institute of Technology Faculty of Mechanical Engineering Laboratory for CAD & Lifecycle Engineering


Download ppt "Written by: Itzik Ben Shabat Technion - Israel Institute of Technology Faculty of Mechanical Engineering Laboratory for CAD & Lifecycle Engineering Lab."

Similar presentations


Ads by Google