Hope some one can help.
Is there any way of generating callback and using the direction of
rotation within a gui application?
Thanks
Rob
we wrote once a mex file and called an api function inside.
In this way we got access to a joystick.
Just an idea.
Regards,
Stefan
I hope it is possible to acces some java function that does this in a
similar way as the example below:
h=get(gcf,'Javaframe')
set(h,'FigurePeerMouseEventCallback','disp mycallback')
This example responds when the mouse is pressed.
I could not find a function that is called when the mousewheel is
moved. Maybe somebody else has more luck.
Nanne
I used it as a part of a zoom utility and it works great!
Hope this helps.
Nanne van der Zijpp
www.modelit.nl
%%%% Copy the code in the m file setMouseWheel.m %%%%%%
function setMouseWheel(fcn,HWIN)
% setMouseWheel - set callback for mouseWheel for figure
%
% INPUT
% fcn: calbback function
% HWIN: handle of figure
%
%TESTED WITH
% matlab 7.0.1
%
% Nanne van der Zijpp
% Modelit
% www.modelit.nl
if nargin<1
fcn=@testcallback;
end
if nargin<2
HWIN=gcf;
end
%get RootPane for specified fure
RootPane=getRootPane(HWIN);
setappdata(HWIN,'RootPane',RootPane); %bind RootPane to prevent if
from being destroyed
set(RootPane,'MouseWheelMovedCallback',fcn);
%--------------------
function testcallback(varargin)
dprintf('testcallback called with %d input
arguments',length(varargin));
for k=1:length(varargin)
dprintf('argument %d:',k);
disp(varargin{k});
end
%--------------------
function RootPane=getRootPane(HWIN)
%get RootPane for specified figure
if nargin<1
HWIN=gcf;
end
%create invisible dummy java object and wrap it in a java component
jobj=javax.swing.JLabel;
jobj.setOpaque(0);
[jobj,h] = javacomponent(jobj,[],HWIN);
RootPane=[];
T0=now;
while isempty(RootPane)
pause(.01);
RootPane=jobj.getRootPane;
if (now-T0)*60*1440 > 1
error('Time out while retreiving root pane');
end
end
delete(h);
your code is very useful for me.
Do you know how to extract the value of wheelRotation from the java
object, to a matlab's variable?
thank you
german
The callback you specify will be called with 2 arguments, lets call
them "obj' and 'event". The second argument is a java object that
contains information that you can retreive with the "get" command.
If you set a breakpoint and type:
get(event)
you'll see all available attributes (see below)
Some useful attributes are:
AltDown
ControlDown
ShiftDown
WheelRotation
The attribute "WheelRotation" tells you the direction of the wheel.
Hope this helps,
Nanne
EXAMPLE CALL BACK:
function mousewheelCallback(obj,event)
rotation=get(event,'WheelRotation') %-1 or 1
get(event):
ID = [507]
AltDown = off
AltGraphDown = off
Button = [0]
Class = [ (1 by 1) java.lang.Class array]
ClickCount = [0]
Component = [ (1 by 1) com.mathworks.mwswing.desk.DTRootPane array]
Consumed = off
ControlDown = off
MetaDown = off
Modifiers = [0]
ModifiersEx = [0]
Point = [463 418]
PopupTrigger = off
ScrollAmount = [3]
ScrollType = [0]
ShiftDown = off
Source = [ (1 by 1) com.mathworks.mwswing.desk.DTRootPane array]
UnitsToScroll = [-3]
WheelRotation = [-1]
When = [-2.2518e+015]
X = [463]
Y = [418]
BeingDeleted = off
ButtonDownFcn =
Children = []
Clipping = on
CreateFcn =
DeleteFcn =
BusyAction = queue
HandleVisibility = on
HitTest = on
Interruptible = on
Parent = []
Selected = off
SelectionHighlight = on
Tag =
Type = java.awt.event.MouseWheelEvent
UIContextMenu = []
UserData = []
Visible = on
thanks for the explanation.
I am using a gui, where I would like to use the mouse wheel to change
the radius of a circle.
how can I make my gui recognize the mouse wheel and call the function
you preciously sent us.
thanks,
german
The mousewheel callback is defined with the figure, similar to a
windowbuttonmove function, so anytime you move the mousewheel it will
be called (unless an object with its own mousewheel callback, such a
s a uicontrol listbox, catches the mousewheel callback).
If you don't want your circle to resize everytime you move the wheel,
There are many ways of doing this. A few suggestions:
-1- Check if the circle is the current object object, before resizing
it. This is the way it usually works with lists and tables that use
mousewheel functions. You can use the matlab function GCO. When you
click on a circle it will become the current object. Problem is that
the appearane of the gco does not change, so this is not really user
friendly because the user can't see which object is selected for
resize.
-2- in each mousewheel callback check if the cursor is over a
specific area (for example the centroid of your circle). If not,
cancel the callback. You can use the matlab function OVEROBJ, or
write your own version.
-3- select/deselect the circle for resizing. This can be done by
creating a menu item that toggles the selectable state or by
attaching a buttondown function to the circle that toggles the
selectable state. You could even set matlab's selectable state to
provide visual feedback.
Goodluck
Nanne
sorry to bother you again.
I tried to add the callback function (mousewheelCallback or
MouseWheelCallback) to the gui's callbacks code, but it does not
work.
When I rotate the mouse's wheel, the function is never called (I have
not objects usining the mousewheel).
As far as I understand, if I am using a gui, I don't need your
setMouseWheel.m code. Am I right?
I am using matlab 7 sp3, but I also tried it with matlab 6.5, in a
windows XP computer.
Do you have any idea what might be the problem?
thanks again,
german
This is what should happen:
- if no figure is present setmouseWheel will create one.
-you'll see the messages displayed by the default callback:
argument 1:
407.0055
argument 2:
java.awt.event.MouseWheelEvent[MOUSE_WHEEL,(280,318),button=0,clickCou
nt=0,scrollType=WHEEL_UNIT_SCROLL,scrollAmount=3,wheelRotation=1] on
com.mathworks.mwswing.desk.DTRootPane[,4,30,560x468,layout=com.mathwor
ks.mwswing.desk.DTRootPane$DTRootLayout,alignmentX=null,alignmentY=nul
l,border=,flags=385,maximumSize=,minimumSize=,preferredSize=]
etcetera etcetera
If you don't see this feedback or get error messages than your
version of setmouseWheel is flawed. Otherwise you should have
something to start from.
Good luck
Nanne
the setmouseWheel function is working perfect, exactly as you
describe it.
My problem is that I don't know how to combine the setmouseWheel
function with the mousewheelCallback when using a gui.
thanks
german
Maybe I should have emphasized earlier that mousewheel callbacks are
not part of standard Matlab language.
It could be a good idea to do something similar with supported
features of Matlab first.
For example you could implement a function that moves an object by
clicking and dragging. This can be done with standard Matlab commands
(buttondown and windowbuttonmove)and you'll find plenty of help and
examples around.
Once you've done this you'll see that using the Mousewheel is very
similar. The comments in message 11 in this thread should help. The
central point is that the mousewheel callback is not defined for a
specific object but for a figure that contains many objects. The
function needs to contain some code to determine which object the
function should operate on (see message 11).
I hope you'll get it working
Nanne
Which Matlab version are you working with?
For your information:
I am using the code with Matlab version 7.0.1 and also in compiled
mode (compiler v 4.1.1) and did not have problems until now.
Regards Nanne
I tried to use a callback function in a gui, which calls your
setMouseWheel function.
It works very well, but the problem is that I have problems
transfering the variables back to the gui function.
I use the following line in the setMouseWheel function
rotation=get(varargin{2},'WheelRotation');
I difine the variable rotation as global, or I also modified the
function so it can send back the variable rotation.
In both cases I can use 'rotation' in the gui's function, but it is
not updated as I turn the mousewheel. It just receives the value at
the time it was called and that's it.
do you have any idea what might be the problem?
thanks
> For your information:
> I am using the code with Matlab version 7.0.1 and also in compiled
> mode (compiler v 4.1.1) and did not have problems until now.
Interesting; it might be related with the way JAVA is handled in this
new version? Beware: I know nothing about this JAVA stuff...
Regards
Carlos