Azzera filtri
Azzera filtri

Set 'KeyPressFcn' to a method in the class

27 visualizzazioni (ultimi 30 giorni)
Alon Osovsky
Alon Osovsky il 24 Mag 2020
Risposto: Monika Phadnis il 27 Mag 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!

Risposte (1)

Monika Phadnis
Monika Phadnis il 27 Mag 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.

Categorie

Scopri di più su Interactive Control and Callbacks in Help Center e File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by