2011-01-15 6 views
5

Mac 용 코드를 배우려고합니다. 나는 잠시 동안 자바 녀석이었습니다. 그래서 나는 내가 겪고있는 문제가 코코아에 대한 간단한 오해라고 생각합니다.addGlobalMonitorForEventsMatchingMask 만 마우스 위치를 반환합니다.

-(IBAction)beginEventMonitor:(id)sender { 
    _eventMonitor = [NSEvent addGlobalMonitorForEventsMatchingMask:(NSLeftMouseUpMask) 
    handler:^(NSEvent *incomingEvent) { 
    //NSWindow *targetWindowForEvent = [incomingEvent window]; 
    NSLog(@"Got a mouse click event at %@", NSStringFromPoint([incomingEvent locationInWindow])); 
    }]; 
} 

-(IBAction)stopEventMonitor:(id)sender { 
    if (_eventMonitor) { 
    [NSEvent removeMonitor:_eventMonitor]; 
    _eventMonitor = nil; 
    } 
} 

이 마우스 클릭은 글로벌 수준에서 일어날 때 말해 간단한 훅은 다음과 같습니다

나는 다음과 같은 코드를 가지고있다. 처리기가 작동하지만 incomingEvent의 내용이 아무것도 설정되지 않은 것처럼 보입니다. 내가 찾을 수있는 유일한 유용한 정보는 클릭 한 순간의 마우스 위치와 클릭 한 창의 windowId입니다.

더 자세한 정보를 얻을 수 없습니까? 모니터를 올바르게 설치하지 않습니까? 나는 어떤 창을 클릭했는지 알 수 있기를 정말로 원하지만, 마우스 위치 나 windowId를 유용하게 사용할 수있는 방법을 찾을 수조차 없다.

답변

6
당신은 예를 들어 (레오파드의 새로운)를 CGWindow API를 사용하여 창에 대한 자세한 내용은 검색 할 수 있습니다

:

CGWindowID windowID = (CGWindowID)[incomingEvent windowNumber]; 
CFArrayRef a = CFArrayCreate(NULL, (void *)&windowID, 1, NULL); 
NSArray *windowInfos = (NSArray *)CGWindowListCreateDescriptionFromArray(a); 
CFRelease(a); 
if ([windowInfos count] > 0) { 
    NSDictionary *windowInfo = [windowInfos objectAtIndex:0]; 
    NSLog(@"Name: %@", [windowInfo objectForKey:(NSString *)kCGWindowName]); 
    NSLog(@"Owner: %@", [windowInfo objectForKey:(NSString *)kCGWindowOwnerName]); 
    //etc. 
} 
[windowInfos release]; 

이 정보를 많이 있습니다 (CGWindow.h에서 보거나 사용할 수에 대한 문서를 참조를 키). 또한 한 창 (다른 창에 부분적으로 덮여있는 경우에도 작동)의 스크린 샷을 만드는 기능도 있습니다.

+0

은 마우스 이벤트와 함께 훌륭하게 작동합니다. keyDown 이벤트에 대해 알고 있습니까? –

관련 문제