【问题标题】:Matlab GUI: override behaviour of the spacebar keyMatlab GUI:覆盖空格键的行为
【发布时间】:2014-12-18 22:26:37
【问题描述】:

我使用 GUIDE 在 matlab 中构建了一个 GUI,并使用我图的 WindowKeyPressFcn 回调函数为某些操作添加了键盘快捷键。我的问题是我想使用空格键作为快捷键之一,但它已经有一个预设的用法:它激活当前选定的控件(即与单击选定的按钮相同)。

虽然我可以通过回调在空格键上很好地触发我想要的操作,但我发现无法删除这个额外的不需要的功能。结果是执行了两个操作 - 我编程的一个和意外按下的按钮,这会造成混乱。底线是我不能使用空格键作为快捷方式。有什么办法可以关闭此功能或以某种方式绕过它?也许在我的回调处理后会阻止按键到达 GUI?

我更喜欢记录在案的 Matlab 方式,但如果不可能,也欢迎使用 java hack。

【问题讨论】:

    标签: matlab user-interface keyboard-events


    【解决方案1】:

    AFAIK 不需要的行为更多地与操作系统相关,而不是与 Matlab 相关,我不知道如何删除它,但是您可以解决它。

    执行此操作的一种方法是在您的按钮回调中检查最后一次按下的键是什么,您可以通过将 KeyPressFcn 添加到您的按钮来执行此操作。我在下面创建了一个简单的示例:

    function spacebarTest
      % create a figure
      hFig = figure ( 'KeyPressFcn', @KeyPress );
      % create a uicontrol
      uicontrol ( 'style', 'pushbutton', 'Callback', @PushButton, 'KeyPressFcn', @KeyPress );
      % store the last key pressed information in the fiure handle
      userData.lastKey = '';
      set ( hFig, 'userData', userData );
    end
    function KeyPress ( obj, event )
      % obtain the handle for the figure
      hFig = ancestor ( obj, 'figure' );
      % extract the user data 
      userData = get ( hFig, 'userData' );
      % store the last key in the user data
      userData.lastKey = event.Key;
      % update the figure handle user data
      set ( hFig, 'userData', userData );
      disp ( 'spacebar shortcut' );
    end
    function PushButton ( obj, event )
      % get the figure handle 
      hFig = ancestor ( obj, 'figure' );
      % extract the user data
      userData = get ( hFig, 'userData' );
      % check if spacebar was the last key pressed
      if strcmp ( userData.lastKey, 'space' ); return; end
      disp ( 'running callback - push me' );
    end
    

    这样做的一个问题是它仍然“看起来”像您的按钮已被按下,尽管回调尚未运行。要解决此问题,您需要停止获得焦点的按钮,您需要将 uicontrol 按钮替换为 JButton (java) 并使用 FocusGainedCallback

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2013-08-11
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多