在Keypress上停止NSRunLoop

时间:2011-01-06 00:21:56

标签: objective-c command-line runloop

我正在Mac OS X中编写一个命令行基础工具,并希望该工具退出按键,例如'q'。该代码正在启动一个异步请求,用于从远程服务器检索数据。这需要NSRunLoop。至少这是我理解的我需要做的事情。

有人能告诉我如何在特定的按键上停止runloop吗?

以下是代码段。

int main (int argc, const char * argv[]) {

BOOL keepRunning = YES;
NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init];

Requestor *myRequestor = [[Requestor alloc] init];
[myRequestor GetData];

NSRunLoop *runLoop;
runLoop = [NSRunLoop currentRunLoop];

while (keepRunning && [runLoop runMode:NSDefaultRunLoopMode beforeDate:[NSDate distantFuture]]);

[pool drain];
return 0;
}

谢谢!

3 个答案:

答案 0 :(得分:3)

我自己没有这样做,但我希望你能使用[[NSFileHandle fileHandleWithStandardInput] readInBackgroundAndNotify]并注册接收NSFileHandleReadCompletionNotification通知。如果您收到'q',请执行您需要清理的内容并致电exit()

答案 1 :(得分:0)

好吧,我从现有代码开始的初步理论和实验并没有对可用代码产生太多影响。

我正在想象你正在寻找的东西就像在Windows上你可以在命令行shell窗口中运行一些东西,当这个过程结束时,它会说“按Q键到继续...”。当你向前移动窗口时(如果它不在最前面),然后按Q键,窗口关闭。

您是打算从主应用程序调用此命令行工具,还是此命令行工具是最终用户将直接与之交互的内容? (例如,如果是后者,他们会在终端窗口中调用它,嗯,那么我认为Ken的代码可能与我的代码结合起来进行以下操作。请注意,在目前的形式中,这仅在您按下之后有效Q然后点击Return?

#import <Cocoa/Cocoa.h>

@interface Requestor : NSObject <NSApplicationDelegate> {
    BOOL            gotData;
    NSFileHandle    *stdIn;
}
- (void)getData;
- (void)requestorGotData:(id)sender;
@end

@implementation Requestor

- (id)init {
    if (self = [super init]) {
        gotData = NO;
        stdIn = nil;
        [NSApp setDelegate:self];
    }
    return self;
}

- (void)getData {
    NSLog(@"getting data.........");
    gotData = NO;

    [self performSelector:@selector(requestorGotData:)
               withObject:nil
               afterDelay:5.0];
}

断裂

- (void)requestorGotData:(id)sender {
    NSLog(@"got data");
    gotData = YES;
    NSLog(@"Press 'Q' key to continue...");
    stdIn = [[NSFileHandle fileHandleWithStandardInput] retain];
    [[NSNotificationCenter defaultCenter]
       addObserver:self
          selector:@selector(fileHandleReadCompletion:)
              name:NSFileHandleReadCompletionNotification
            object:stdIn];
    [stdIn readInBackgroundAndNotify];
}

- (void)fileHandleReadCompletion:(NSNotification *)notification {
    NSLog(@"fileHandleReadCompletion:");
    NSData *data = [[notification userInfo]
                    objectForKey:NSFileHandleNotificationDataItem];
    NSLog(@"data == %@", data);
    NSString *string = [[[NSString alloc]
                             initWithData:data
                        encoding:NSUTF8StringEncoding] autorelease];
    if (string) {
        string = [string stringByTrimmingCharactersInSet:
                  [NSCharacterSet whitespaceAndNewlineCharacterSet]];
        if ([[string lowercaseString] isEqualToString:@"q"]) {
            [stdIn closeFile];
            [stdIn release];
            [[NSNotificationCenter defaultCenter] removeObserver:self];
            [NSApp terminate:nil];
        } else {
            [stdIn readInBackgroundAndNotify];
        }
    }
}
@end

断裂

int main (int argc, const char * argv[]) {
    NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init];

    [NSApplication sharedApplication];

    Requestor *requestor = [[Requestor alloc] init];
    [requestor getData];

    [NSApp run];

    [requestor release];

    [pool drain];
    return 0;
}

答案 2 :(得分:0)

如果你还没有考虑过libcurses,也许这会对你有帮助。使用它来捕捉按键非常简单,但我不是100%关于它是否可以在没有使用整个终端窗口的情况下使其工作。

诅咒比特只是:

#include <ncurses.h>

initscr();

/* snip */

char c;
while (c = getch()) {
  if (c == 'q') {
    // Put your cleanup and shutdown logic here
  }

  /* any other keypresses you might want to handle */
}

编辑|你可能不想把这个紧密的循环放在你的运行循环中......只要每次runloop结束时调用getch()