【问题标题】:Make MATLAB accept KeyPress only once让 MATLAB 只接受一次 KeyPress
【发布时间】:2016-07-28 06:00:32
【问题描述】:

我目前正在尝试在 MATLAB 中编写一个实验。作为其中的一部分,它应该接受并记录一个密钥响应,1 或 0。问题是我只需要在一个特定的时间段记录密钥,并在实验的其他部分被忽略。响应必须与响应时间一起记录,并且应该只记录一次,最早的一次(这样一旦用户按下一个键,程序就不会记录后续的)。

到目前为止,我已经尝试了很多方法。这些可能是笨拙的解决方法,但我不擅长面向对象编程。

一种选择是使用

set(gcf,'KeyPressFcn',@KeyDownListener)

KeyDownListener 在哪里

function KeyUpListener(key_hand, key_obj, starting_time)

     toc(starting_time)
     key_pressed = key_obj.Key; return; end

但是,有两个问题:1)我正在努力尝试将此函数的值返回给调用脚本; 2) 一旦 MATLAB 读取了这段 set(...) 代码,它就会不断捕获每个按下的键。所以,基本上,如果有 100 次试验(每个试验由 5 个阶段组成,其中按键应该只在第 4 阶段被接受)在循环中的实验中,set(...) 将在第一次运行时被忽略在它第一次出现之前的第 1-3 阶段,但随后会出现在从第二阶段开始的所有运行中,在每个阶段,1-5。

然后我尝试将调用脚本和被调用函数都放入从外部脚本调用的另一个函数中,这样一旦控件返回到更高级别的脚本,我就会放入另一个

set(gcf,'KeyPressFcn',@mute)

mute 函数不执行任何操作。这似乎适用于问题 2,但它仍然不允许我获取按键回调的值,并且,由于我使用 pause(..) 允许用户有时间进行响应,它不会中断第一个键按下时,它会等待括号中为 pause 分配的全部时间。

【问题讨论】:

  • 您能否提供一个最小的工作示例?我觉得pause 不会是正确的方式。如何设置一个允许键捕获并仅在给定阶段打开的变量
  • @Finn 你好,芬兰人!要么我对算法不是特别有创造力,要么没有“暂停”就不太方便,否则需要启动几个“定时器”功能。我倾向于弄乱计时器,所以我尽量将它们保持在最低限度。

标签: matlab key response


【解决方案1】:

传递变量:

我建议使用 setappdatagetappdata 在 GUI 的回调之间传递变量。
您也可以阅读Share Data Among Callbacks 了解有关这方面的更多信息。

分配和禁用回调:

要禁用回调函数,您不需要将回调重新分配给什么都不做的函数,您可以简单地分配一个空数组:

% assign the 'KeyDownListener' function and pass one parameter ('var1') with it
set( h.fig, 'KeyPressFcn',{@KeyDownListener,var1}) 

% then later when you don't need it anymore:
% Disable the 'KeyPressFcn listener, assign 'empty' to it
set( h.fig, 'KeyPressFcn',[])

完整示例:

下面是一个最小的 GUI,它演示了如何捕获单个按键(和时间),然后将收集到的数据保存到应用程序的 appdata 空间中,并通过“显示”功能再次收集它们(您还可以对收集的数据做任何您想做的事情)。

SingleKeyPressDemo.m 的代码:

function h = SingleKeyPressDemo

% create a minimal figure with a button and 2 text fields
h.fig = figure ;
h.btn = uicontrol('Style','Pushbutton','String','Start capture',...
                  'units','Normalized','Position',[0.1 0.6 0.8 0.3],...
                  'Callback',@StartKeyCapture) ;
h.txtKey = uicontrol('Style','text','String','Key pressed:',...
                  'units','Normalized','Position',[0.1 0.4 0.8 0.1]) ;
h.txtTime = uicontrol('Style','text','String','Elapsed time:',...
                  'units','Normalized','Position',[0.1 0.3 0.8 0.1]) ;

% assign a callback to the KeyRelease event to intercept passing the
% eventdata to the command window
set(h.fig,'KeyReleaseFcn',@KeyReleased)

% initialise appdata variables to hold the captured key and the time
setappdata( h.fig , 'CapturedKey' , [] )
setappdata( h.fig , 'Elapsed_time' , [] )

% save the handle structure
guidata( h.fig , h)


function StartKeyCapture(hobj,~)
    h = guidata( hobj ) ;   % retrieve the handle structure
    StartTime = tic ;       % Start a stopwatch

    % assigne the callback funtion, passing the stopwatch in parameter
    set(h.fig,'KeyPressFcn',{@KeyDownListener,StartTime})

    % disable the button until a key is pressed (also makes it loose the
    % focus, which is handy otherwise the button keeps the focus and hides
    % the 'KeyPressedFcn'
    set( h.btn , 'Enable','off')


function KeyDownListener(hobj, key_obj, starting_time)
    % Detect key pressed and time elapsed
    Elapsed_time = toc(starting_time) ;
    key_pressed = key_obj.Key;

    h = guidata( hobj ) ;                                   % retrieve the handle structure
    setappdata( h.fig , 'CapturedKey' , key_pressed ) ;     % save the captured key
    setappdata( h.fig , 'Elapsed_time' , Elapsed_time ) ;   % save the elapsed time


    set(h.fig,'KeyPressFcn',[]) % remove listener so new key press will not trigger execution
    set( h.btn , 'Enable','on') % re-enable the button for a new experiment

    % (optional) do something after a key was pressed
    display_results(h.fig) ;


function display_results(hobj)
    h = guidata( hobj ) ;   % retrieve the handle structure

    % retrieve the saved data
    CapturedKey  = getappdata( h.fig , 'CapturedKey' ) ;
    Elapsed_time = getappdata( h.fig , 'Elapsed_time' ) ;

    % update display
    set( h.txtKey  , 'String', sprintf('Key pressed: %s',CapturedKey) ) ;
    set( h.txtTime , 'String', sprintf('Elapsed time: %f ms',Elapsed_time) ) ;

function  KeyReleased(~,~)
% do nothing
% The only purpose of this function is to intercept the KeyReleased event
% so it won't be automatically passed on to the command window.

在 Matlab 中以编程方式创建 GUI 非常冗长,重点关注 StartKeyCaptureKeyDownListener 两个函数中的代码来实现您的要求。

此示例将生成以下 GUI:


补充说明:

我还建议不要在 GUI 应用程序中使用 gcf。当调试或在控制台中打开图形时,这是一个方便的快捷方式,但在 GUI 中,对其自身元素的调用应尽可能独立。 MATLAB 提供了保存所有 GUI 元素(所有 uicontrol,包括主图)句柄的方法,因此您可以在需要时显式地调用它们。这降低了出错的风险(假设您的用户也在同一个 MATLAB 会话中运行其他图形,如果您的回调触发并在用户摆弄另一个图形时调用 gcf,您将尝试在不同的图形上执行代码超出预期的数字......这很容易导致错误)。

阅读guidata 上的文档以获取更多信息(和/或观察我在上面的示例中是如何使用它的)。


编辑:

为避免每次按下时焦点都回到命令窗口,您还必须为相应的KeyRelease事件定义一个回调函数,否则该事件将自动转发到命令窗口(这将占用焦点)。

一个干净的方法是简单地添加回调分配

set(h.fig,'KeyReleaseFcn',@KeyReleased)

在图形定义中一劳永逸(您不必在每个实验中设置/撤消此回调),然后定义一个空函数function KeyReleased(~,~),它什么都不做。这种方式在上面修改后的代码示例中实现。

另一种不使用额外空函数的方法是在赋值时简单地定义回调:

set(h.fig,'KeyReleaseFcn','disp([])')

这样您就不需要有一个空的KeyReleased 函数。

但是请注意,回调函数必须被定义(内联或在代码后面)。简单地将empty 分配给回调是行不通的。 (例如,以下两个选项都将无法拦截事件并将其转发到命令窗口:

set(h.fig,'KeyReleaseFcn','') % not working
set(h.fig,'KeyReleaseFcn',[]) % not working either

【讨论】:

  • @Hoki 谢谢!我还不确定它是否有效,因为它是更大事物的一部分,但它的一部分绝对是我需要的。然而,有一个细微的差别。当我按下一个键时,焦点总是离开窗口,并传递到命令行。您能否提出一种使焦点保持在窗口上的方法?
  • @CASUS_BELLI,我已经修改了答案以防止命令窗口获得焦点。
猜你喜欢
  • 1970-01-01
  • 2014-09-27
  • 2014-09-17
  • 1970-01-01
  • 2018-08-20
  • 1970-01-01
  • 2016-04-05
  • 2011-07-12
  • 1970-01-01
相关资源
最近更新 更多