【发布时间】:2011-09-10 23:23:06
【问题描述】:
当我在 Web 视图中按向上键时,它会记录它,但不会触发 stringByEvaluatingJavaScriptFromString 中定义的 JS 警报,有什么想法吗?谢谢
- (void)applicationDidFinishLaunching:(NSNotification *)aNotification
{
[webView setMainFrameURL:@"http://google.com"];
[webView setAutoresizingMask:NSViewWidthSizable | NSViewHeightSizable];
}
-(id)init {
if( !(self = [super init]) ){
return nil;
}
NSEvent * (^monitorHandler)(NSEvent *);
monitorHandler = ^NSEvent * (NSEvent * theEvent){
switch ([theEvent keyCode]) {
case 123: // Left arrow
NSLog(@"Left behind.");
break;
case 124: // Right arrow
NSLog(@"Right as always!");
break;
case 125: // Down arrow
NSLog(@"Downward is Heavenward");
break;
case 126: // Up arrow
[webView stringByEvaluatingJavaScriptFromString:@"alert('yo')"];
NSLog(@"Up, up, and away!");
break;
default:
break;
}
// Return the event, a new event, or, to stop
// the event from being dispatched, nil
return theEvent;
};
// Creates an object we do not own, but must keep track
// of so that it can be "removed" when we're done
eventMon = [NSEvent addLocalMonitorForEventsMatchingMask:NSKeyDownMask
handler:monitorHandler];
return self;
}
【问题讨论】:
标签: javascript objective-c cocoa webview