Re: two quick questions: keychain and events
Re: two quick questions: keychain and events
- Subject: Re: two quick questions: keychain and events
- From: Carlos Weber <email@hidden>
- Date: Sun, 24 Jun 2001 12:07:06 -1000
On Sunday, June 24, 2001, at 10:25 , Aaron Tuller wrote:
2. Is there any way I can know if any events have taken place in my
app? I want to pop up an alert if there's been no activity (in my app
only) after a certain amount of time. Also, it would be helpful if I
could do this not just after the app launched but anytime during the
execution of my program. So like:
Relative newbie disclaimer: someone who really knows could probably come
up with something better. But something like this might suit you.
// --------------------------------
// Interface File: TrackerApp.h
// --------------------------------
#import <Cocoa/Cocoa.h>
@interface TrackingApp : NSApplication
{
BOOL waitingForTimeout;
}
@end
// ------------------------------------
// Implementation File: TrackerApp.m
// -------------------------------------
#import "TrackingApp.h"
@implementation TrackingApp
- (NSEvent *)nextEventMatchingMask:(unsigned int)mask
untilDate:(NSDate *)expiration
inMode:(NSString *)mode
dequeue:(BOOL)flag
{
NSEventType typeWeGot;
NSEvent *theEvent = [super nextEventMatchingMask:mask
untilDate: expiration
inMode: mode
dequeue: flag];
typeWeGot = [theEvent type];
NSLog(@"mouse down = %d, waiting = %d", (int)(typeWeGot ==
NSLeftMouseDown),
(int)(waitingForTimeout));
if ((typeWeGot != NSLeftMouseDown) && !waitingForTimeout) {
[self performSelector: @selector(timeout)
withObject:nil
afterDelay: 5.0];
waitingForTimeout = YES;
} else if (typeWeGot == NSLeftMouseDown) { // we got a mouse down so
stop the timer
[NSObject cancelPreviousPerformRequestsWithTarget:self
selector: @selector(timeout)
object:nil];
waitingForTimeout = NO;
}
return theEvent;
}
- (void)timeout
{
NSLog(@"Nothing happened for 5 seconds. Where is everybody????");
waitingForTimeout = NO;
}
@end
Start by creating a new cocoa app in PB. Use IB to subclass
NSApplication (TrackerApp) and create the files TrackerApp.h and
TrackerApp.m, to be added to your project. Tell IB that the File's Owner
should have the TrackerApp subclass.
Now in PB, paste the above stuff into the .h and .m files. Select your
project's target and make sure that the application settings reflect the
fact that TrackerApp is your principal class rather than NSApplication.
Off you go.
(My example only checks for mouse down events; also I set the delay for
5 seconds because I didn't feel like staring at my screen for 30
seconds waiting for some text to appear in PB!)