matlab:KeyPressFcn和WindowKeyPressFcn之间的区别 [英] matlab: difference between KeyPressFcn and WindowKeyPressFcn

查看:999
本文介绍了matlab:KeyPressFcn和WindowKeyPressFcn之间的区别的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

KeyPressFcn 的Matlab文档定义是在图形窗口具有焦点时,通过按键调用的回调函数."

The Matlab documentation definition for KeyPressFcn is a "callback function invoked by a key press that occurs while the figure window has focus."

类似地, WindowKeyPressFcn 的定义是在图形窗口或其任何子级具有焦点时,通过按键调用的回调函数."

Similarly, the definition for WindowKeyPressFcn is a "callback function invoked by a key press that occurs while either the figure window or any of its children has focus."

据我了解,图形的子级是诸如轴,绘图对象和注释对象之类的实体.如果这些子对象中的一个具有焦点,则其父图形可能也具有焦点(至少其句柄将由gcf返回).因此,我看不到KeyPressFcnWindowKeyPressFcn之间的任何实际区别,除了前者(暗示其名称缺少术语Window)可能在没有有效数字(例如操作时)时也生效在命令窗口中(尽管此推测与回调定义不同,后者明确调用了图形窗口的存在).

As far as I understand, the children of figures are entities like axes, plot objects, and annotation objects. If one of these children has focus, then its parent figure presumably also has focus (at least its handle would be returned by gcf). Therefore, I don't see any practical distinction between KeyPressFcn and WindowKeyPressFcn other than that the former, by implication of its name lacking the term Window, might also take effect when no figures are active, such as when operating in the command window (though this conjecture is in disagreement with the callbacks definition, which explicitly invokes the presence of a figure window).

有人可以解释这两个回调函数之间的区别吗?何时优先使用一个回调函数呢?

Could someone pleas explain the difference between these two callback functions and when use of one over the other might be preferred?

推荐答案

正如您在问题中所建议的,区别在于重点.

As you suggest in your question the difference is in focus.

    仅当图形具有焦点(而不是其子级)时才评估
  • KeyPressFcn.
  • 另一方面,
  • WindowKeyPressFcn将在图形或其任何子级都具有焦点时进行评估.
  • KeyPressFcn is evaluated only when the figure has focus (but not its children).
  • WindowKeyPressFcn, on the other hand, is evaluated whenever the figure or any of its children has focus.

这可以用以下代码说明:

This can be illustrated with following code:

function test_keypress_vs_windowkeypress

h.hf = figure();
h.edit = uicontrol('Style', 'edit', 'Units', 'Normalized',...
    'Position', [0.2, 0.2, 0.6, 0.6]);

% set callbacks
set(h.hf, 'KeyPressFcn', @wintest);
set(h.edit, 'KeyPressFcn', @edittest);

function wintest(h, e)
    disp('window button press');

function edittest(h, e)
    disp('editbox button press');

该函数创建带有丑陋编辑框(也具有KeyPressFcn)的图形(具有KeyPressFcn).
现在,如果您:

The function creates a figure (that has a KeyPressFcn) with an ugly edit box (has a KeyPressFcn too).
Now if you:

    编辑框具有焦点时,
  • 按任意键将评估edittest回调.
  • 具有焦点时,
  • 按任意键将评估wintest回调.
  • 将窗口回调更改为WindowKeyPressFcn,然后在编辑框具有焦点时按一个键-两者回调都将被评估(首先是图形回调,然后是编辑框回调).
  • press any key when the edit box has focus only the edittest callback is evaluated.
  • press any key when the figure has focus only the wintest callback is evaluated.
  • change the window callback to be WindowKeyPressFcn and press a key while the edit box has focus - both callbacks will be evaluated (first the figure callback then the edit box callback).

这篇关于matlab:KeyPressFcn和WindowKeyPressFcn之间的区别的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆