Set 'KeyPressFcn' to a method in the class

48 ビュー (過去 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 Center および File ExchangeInteractive Control and Callbacks についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by