Botão do mouse e contador de teclas do Mac OS X

8

Existem técnicas para Linux e Windows , mas existe uma maneira de contar eventos de mouse e teclado no Mac OS X? Estou interessado em fazer análises estatísticas da minha atividade diária.

    
por Jashank 11.03.2012 / 10:13

4 respostas

15

Com base na inspiração fornecida por MrDaniel , decidi programar um pequeno contador simples.

Ocódigo-fonteparaisso,menosainterfacedousuáriodefinidacomoxib;usaframeworksFoundationeAppKit(fontescompletaseprojetoXcodeem GitHub ):

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

Os ícones usados na barra de ferramentas são do Projeto do Tango Desktop .

    
por 29.07.2012 / 18:08
3

WhatPulse é executado no Windows, OS X e Linux.

Obrigado ao Vsauce por apontar este aqui.

    
por 27.07.2012 / 22:23
2

Typingstats exibe um número total de toques de tecla e várias outras métricas. Ele não conta os cliques do dispositivo apontador.

    
por 29.07.2012 / 19:43
1

Um programa de contador de clique e botão é possível por meio da gravação de um programa Cocoa Objective-C que pode receber e contar eventos de cliques do mouse e do teclado.

A turma a analisar é NSEvent especificamente o método de classe addGlobalMonitorForEventsMatchingMask: handler: deve ser muito útil. Desde que oferece para monitorar eventos como:

NSLeftMouseUp

NSRightMouseUp

NSOtherMouseUp

NSLeftMouseDown

NSRightMouseDown

NSOtherMouseDown

NSKeyDown

    
por 27.07.2012 / 19:43