Mac OS X的鼠标按钮和按键计数器


Answers:


15

基于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


1
如何在Mac OS X中打开它?
约翰·史密斯

1
对鼠标很好用,但不能捕获10.10中的按键:(
Mecki,2015年

@Mecki当我注意到时,我将其添加到了回购描述中。不幸的是,我不知道为什么,可能与通用访问API的按应用访问限制有关,并且二进制文件未签名。或者他们甚至完全杀死了这个。
丹尼尔·贝克

在Mac OS X 10.9.5上,它适用于鼠标,但也不能捕获按键。梅基,你知道原因了吗?谢谢。
加宽W 2015年

@JiakuanW我有一段时间在GitHub存储库中获得了PR,声称可以解决此问题(未经测试)。
丹尼尔·贝克


2

Typingstats显示击键总数和各种其他指标。它不计算指向设备的点击次数。


你自己尝试过吗?它会根据您的实际情况改变键盘布局吗?还是总是美国?
丹尼尔·贝克

App Store应用程序,在加拿大和其他地方可能不可用。
贾斯汀

1

通过编写可接收并计算鼠标和键盘点击事件的Cocoa Objective-C程序,可以实现点击和按下按钮的计数器程序。

要看的类是NSEvent,特别是addGlobalMonitorForEventsMatchingMask:handler:类方法应该被证明是非常有用的。由于它提供监视事件,例如:

NSLeftMouseUp

NSRightMouseUp

NSOtherMouseUp

NSLeftMouseDown

NSRightMouseDown

NSOtherMouseDown

NSKeyDown


3
请尝试以实际使用户更接近其目标的方式回答。只是告诉他学习编程不是。例如,您可以提供相关的代码片段或函数调用,这是实际解决方案的要旨。尽管它仍然对所有人都没有用,但它可以被其他人用作提供可行解决方案的基础。
丹尼尔·贝克

好话,丹尼尔·贝克(Daniel Beck)在建议使用“可可辅助功能编程指南”时似乎采取了错误的方法,在进一步阅读后,我被指向NSEvent类,这似乎可以完成工作……
MrDaniel
By using our site, you acknowledge that you have read and understand our Cookie Policy and Privacy Policy.
Licensed under cc by-sa 3.0 with attribution required.