Set 'KeyPressFcn' to a method in the class

조회 수: 36 (최근 30일)
Alon Osovsky
Alon Osovsky 2020년 5월 24일
답변: Monika Phadnis 2020년 5월 27일
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!

답변 (1개)

Monika Phadnis
Monika Phadnis 2020년 5월 27일
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.

카테고리

Help CenterFile Exchange에서 Environment and Settings에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by