Set 'KeyPressFcn' to a method in the class

36 Ansichten (letzte 30 Tage)
Alon Osovsky
Alon Osovsky am 24 Mai 2020
Beantwortet: Monika Phadnis am 27 Mai 2020
Hi there!
I have a method in a class that sets the "key press function" of a figure, and I just can't figure a way to set that function to be a method from the same class.
In my program, I have written something like this:
classdef main
methods
function setFunction(obj)
% The function that sets the callback function.
set(gcf, 'KeyPressFcn', @obj.functionToSet) % The part that i'm trying to figure out!
end
function functionToSet(obj, event)
% This function is the funtion that I want to be called when a key is pressed.
disp(event.Key) % prints the pressed key
end
end
end
I can guess that the solution is simple. Thanks in advance!

Antworten (1)

Monika Phadnis
Monika Phadnis am 27 Mai 2020
The 'KeyPressFcn' returns 'KeyData' object specifying information about the key pressed. You can define this as the third argument in the 'functionToSet' method.
This code worked for me:
function functionToSet(obj,event,keyData)
disp(keyData.Key) % prints the pressed key
end
You can also check the documentation for various properties KeyData objects holds:
Hope this helps.

Kategorien

Mehr zu Environment and Settings finden Sie in Help Center und File Exchange

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by