답변:
MrDaniel이 제공 한 영감을 바탕으로 간단한 카운터를 프로그래밍하기로 결정했습니다.
이를위한 소스 코드에서 UI를 빼고 xib
; Foundation 및 AppKit 프레임 워크 ( GitHub의 전체 소스 및 Xcode 프로젝트 )를 사용합니다.
DBAppDelegate.h
//
// DBAppDelegate.h
// CocoaActivityCounter
//
// Created by Daniel Beck on 29.07.2012.
// Copyright (c) 2012 Daniel Beck. All rights reserved.
//
#import <Cocoa/Cocoa.h>
static id monitorLeftMouseDown;
static id monitorRightMouseDown;
static id monitorKeyDown;
@interface DBAppDelegate : NSObject <NSApplicationDelegate>
@property (assign) IBOutlet NSWindow *window;
@property (strong) IBOutlet NSTextView *logView;
@property (weak) IBOutlet NSToolbarItem *toolbarStartButton;
@property (weak) IBOutlet NSToolbarItem *toolbarStopButton;
@property (weak) IBOutlet NSToolbarItem *toolbarClearButton;
@property (weak) IBOutlet NSTextField *keyPressCounterLabel;
@property (weak) IBOutlet NSTextField *leftMouseCounterLabel;
@property (weak) IBOutlet NSTextField *rightMouseCounterLabel;
@property (readwrite) NSDateFormatter *logDateFormatter;
@property (readwrite) NSNumber *keyPressCounter;
@property (readwrite) NSNumber *leftMouseCounter;
@property (readwrite) NSNumber *rightMouseCounter;
@property (readwrite) BOOL loggingEnabled;
- (IBAction)stopButtonPressed:(id)sender;
- (IBAction)startButtonPressed:(id)sender;
- (IBAction)clearButtonPressed:(id)sender;
- (void)logMessageToLogView:(NSString*)message;
- (BOOL)validateToolbarItem:(NSToolbarItem *)theItem;
@end
DBAppDelegate.m
//
// DBAppDelegate.m
// CocoaActivityCounter
//
// Created by Daniel Beck on 29.07.2012.
// Copyright (c) 2012 Daniel Beck. All rights reserved.
//
#import "DBAppDelegate.h"
#import <AppKit/NSEvent.h>
@implementation DBAppDelegate
@synthesize logView;
@synthesize toolbarStartButton;
@synthesize toolbarStopButton;
@synthesize keyPressCounterLabel;
@synthesize leftMouseCounterLabel;
@synthesize rightMouseCounterLabel;
@synthesize toolbarClearButton;
@synthesize loggingEnabled;
@synthesize keyPressCounter;
@synthesize leftMouseCounter;
@synthesize rightMouseCounter;
- (void)applicationDidFinishLaunching:(NSNotification *)aNotification {
self.loggingEnabled = NO;
self.logDateFormatter = [[NSDateFormatter alloc] init];
[self.logDateFormatter setTimeStyle:NSDateFormatterMediumStyle];
self.keyPressCounter = [NSNumber numberWithInt:0];
self.leftMouseCounter = [NSNumber numberWithInt:0];
self.rightMouseCounter = [NSNumber numberWithInt:0];
}
- (BOOL)applicationShouldTerminateAfterLastWindowClosed:(NSApplication *)theApplication {
return YES;
}
-(void)logMessageToLogView:(NSString*)message {
[logView setString: [[logView string] stringByAppendingFormat:@"%@: %@\n", [self.logDateFormatter stringFromDate:[NSDate date]], message]];
}
- (IBAction)stopButtonPressed:(id)sender {
if (!self.loggingEnabled) {
return;
}
self.loggingEnabled = false;
[NSEvent removeMonitor:monitorLeftMouseDown];
[NSEvent removeMonitor:monitorRightMouseDown];
[NSEvent removeMonitor:monitorKeyDown];
}
- (IBAction)startButtonPressed:(id)sender {
if (self.loggingEnabled) {
return;
}
self.loggingEnabled = true;
monitorLeftMouseDown = [NSEvent addGlobalMonitorForEventsMatchingMask:NSLeftMouseDownMask handler:^(NSEvent *evt) {
[self logMessageToLogView:[NSString stringWithFormat:@"Left mouse down!"]];
self.leftMouseCounter = [NSNumber numberWithInt:(1 + [self.leftMouseCounter intValue])];
}];
monitorRightMouseDown = [NSEvent addGlobalMonitorForEventsMatchingMask:NSRightMouseDownMask handler:^(NSEvent *evt) {
[self logMessageToLogView:@"Right mouse down!"];
self.rightMouseCounter = [NSNumber numberWithInt:(1 + [self.rightMouseCounter intValue])];
}];
monitorKeyDown = [NSEvent addGlobalMonitorForEventsMatchingMask:NSKeyDownMask handler:^(NSEvent *evt) {
[self logMessageToLogView:[NSString stringWithFormat:@"Key down: %@ (key code %d)", [evt characters], [evt keyCode]]];
self.keyPressCounter = [NSNumber numberWithInt:(1 + [self.keyPressCounter intValue])];
}];
}
- (IBAction)clearButtonPressed:(id)sender {
self.keyPressCounter = [NSNumber numberWithInt:0];
self.leftMouseCounter = [NSNumber numberWithInt:0];
self.rightMouseCounter = [NSNumber numberWithInt:0];
[self.logView setString:@""];
}
- (BOOL)validateToolbarItem:(NSToolbarItem *)theItem {
if ([theItem isEqualTo:toolbarStartButton]) {
return !self.loggingEnabled;
}
if ([theItem isEqualTo:toolbarStopButton]) {
return self.loggingEnabled;
}
if ([theItem isEqualTo:toolbarClearButton]) {
return !self.loggingEnabled;
}
return YES;
}
@end
툴바에 사용 된 아이콘은 Tango Desktop Project 의 아이콘 입니다.
Typingstats 는 총 키 입력 수 및 기타 다양한 메트릭을 표시합니다. 포인팅 장치 클릭 수는 계산하지 않습니다.
클릭 및 버튼 누름 카운터 프로그램은 마우스 및 키보드 클릭 이벤트를 수신하고 계산할 수있는 Cocoa Objective-C 프로그램을 작성하여 가능합니다.
살펴볼 클래스는 NSEvent입니다. 특히 addGlobalMonitorForEventsMatchingMask : handler : 클래스 메소드는 매우 유용합니다. 다음과 같은 이벤트를 모니터링하도록 제공하기 때문에 :
NSLeftMouseUp
NSRightMouseUp
NSOtherMouseUp
NSLeftMouseDown
NSRightMouseDown
NSOtherMouseDown
NSKeyDown