0

不幸的是,我在尝试收听按键事件时遇到了问题。我已经为您完成了最小的示例,并检查您是否可以重现此问题。

要重现它,请按 alt-tab 两次(从图中聚焦,然后再次聚焦),然后按住一个键一段时间(即 3 秒),然后松开它。现在按下任意字母键,您会看到它要么触发长按键的释放,要么根本不会触发任何事件。

我认为这与 java 助记符有关,因为 ctrl、alt、cmd 等其他键总是会触发按下事件……但这只是一个猜测。我在 mac 上使用 matlab,问题发生在 HG1 和 HG2 上。有谁知道如何避免这个问题?

这是测试功能:

function tabPlotIssue()

figH=figure(1);

if feature('UseHG2')
  pressListener=addlistener(figH,'WindowKeyPress',...
    @keyPressFcn);                              
  releaseListener=addlistener(figH,'WindowKeyRelease',...
    @keyRelease);                            
else             
  pressListener=addlistener(figH,'WindowKeyPressEvent',...
    @keyPressFcn);
  releaseListener=addlistener(figH,'WindowKeyReleaseEvent',...
    @keyRelease);                            
end              

function keyPressFcn(a,b)
  disp('Pressed a key:')
  if feature('UseHG2')  
    mChar = b.Character;                      
  else
    mChar = b.Source.CurrentCharacter;
  end
  disp(mChar)
  % pressListener.Enabled = 'off'; % This is not needed to reproduce
  % the issue, but it is good in case you don't want to trigger
  % pressed key many times and make the visualization harder.
end

function keyRelease(a,b)
  disp('Released a key:')
  if feature('UseHG2')   
    mChar = b.Character;
  else
    mChar = b.Source.CurrentCharacter;
  end
  disp(mChar)
  % pressListener.Enabled = 'on'; % This is not needed to reproduce
  % the issue, but it is good in case you don't want to trigger
  % pressed key many times and make the visualization harder.
end

end

显示问题的输出:

>> tabPlotIssue
>> Pressed a key: % Here I pressed alt-tab
>> Released a key: % Here I released alt-tab
>> Pressed a key: % Here I pressed q for a short time
q
>> Released a key: % Here I pressed w
q
>> Released a key: % and so on
q
>> Released a key:
q
>> Released a key:
q
>> Released a key:
q
>> Pressed a key: % Pressed alt
>> Released a key: % Released alt
>> Released a key: % Released any other key
>> Released a key: % and so on
>> Released a key:

更新:

我已将 keyRelease 函数更改为如下,但它一直停止监听事件触发。我注意到它甚至会停止收听 cmd+w,这个数字几乎对所有触发器都没有响应。如果你能告诉我,我将不胜感激

function keyRelease(a,b)
  disp('Released a key:')
  if feature('UseHG2')
    mChar = b.Character;
  else
    mChar = b.Source.CurrentCharacter;
  end
  disp(mChar)
  % pressListener.Enabled = 'on'; % This is not needed to reproduce
  % the issue, but it is good in case you don't want to trigger
  % pressed key many times and make the visualization harder.
  delete(pressListener)
  delete(releaseListener) 
  if feature('UseHG2')                                                      
    pressListener=addlistener(figH,'WindowKeyPress',...           
      @keyPressFcn);                              
    releaseListener=addlistener(figH,'WindowKeyRelease',...       
      @keyRelease);                            
  else                                                                      
    pressListener=addlistener(figH,'WindowKeyPressEvent',...
      @keyPressFcn);
    releaseListener=addlistener(figH,'WindowKeyReleaseEvent',...
      @keyRelease);                            
  end                                                                       
end
4

1 回答 1

0

这可能与 alt 是在按下 5 次时在 mac 上暂停键盘的键有关。

系统偏好设置>通用访问>鼠标和触控板(或鼠标)>鼠标键>关闭。

于 2017-11-26T05:50:07.930 回答