【问题标题】:How to exit a loop when user presses Command + period on the keyboard用户按下键盘上的Command +句点时如何退出循环
【发布时间】:2013-04-27 07:28:55
【问题描述】:

我的实用程序应用在另一个应用上执行文本编辑操作。 用户在 NSPanel 中设置参数并单击“确定”后,NSPanel 将关闭并迭代一个 while 循环,在目标应用程序中执行必要的文本编辑。

我想为用户提供使用 Command + . 停止操作的选项。 我不确定如何在循环迭代时检查用户是否按住键。

while 循环已简化。

- (IBAction)fileNameTrimAppend:(id)sender {
    [self activateTargetApp];  // applescript that brings target app to front
    [self openFileRenameDialog]; // applescript to open the file naming dialog

   // set variables to use in the loop from the user input
    [self close];   // close the input NSPanel

    while ([self fileRenameDialogOpen]) {  // applescript returns false when editing is done       
       if (command +"." is held down){  // need help here
             return;
          }

       // text mod is done here.
       // paste the mod text into target app field
       // move to the next field for editing
     }
}

【问题讨论】:

    标签: objective-c macos cocoa


    【解决方案1】:

    假设这是两个不同的进程,可以使用addGlobalMonitorForEventsMatchingMask观察键盘。

    参见this link 进行类似讨论。

    【讨论】:

    • 对不起 Skotch,我以为我做到了。
    【解决方案2】:

    除非您打算让应用程序在运行时挂起(和沙滩球),否则您需要将工作分成更小的块,并一次处理一个块,让 runloop 处理介于两者之间的事件。您可以使用 -performSelector:afterDelay:0 来安排下一个块的运行。

    如果您确实打算在其工作时阻止所有内容,则需要自己考虑以可重入方式运行 runloop。 runloop 文档应该有更多信息,尽管您可能需要挖掘一下:http://developer.apple.com/library/mac/#Documentation/Cocoa/Conceptual/Multithreading/RunLoopManagement/RunLoopManagement.html

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2011-11-07
      • 1970-01-01
      • 2020-09-21
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多