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