Borrar filtros
Borrar filtros

Set 'KeyPressFcn' to a method in the class

27 visualizaciones (últimos 30 días)
Alon Osovsky
Alon Osovsky el 24 de Mayo de 2020
Respondida: Monika Phadnis el 27 de Mayo de 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!

Respuestas (1)

Monika Phadnis
Monika Phadnis el 27 de Mayo de 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.

Categorías

Más información sobre Interactive Control and Callbacks en Help Center y File Exchange.

Community Treasure Hunt

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

Start Hunting!

Translated by