开发者

iphone keyboard touch events

I need to be able to detect touch events on the keyboard. I have an app which shows a screen which occurs after a certain period of inactivity (i.e. no touch events) To solve this issue, I have subclassed my UIWindow and im开发者_高级运维plemented the sendEvent function, which allows me to get touch events on the whole application by implementing the method in one place. This works everywhere beside when the keyboard is presented and the user is typing on the keyboard. What I need to know is that is there a way to detect touch events on the keyboard, kind of like what sentEvent does for uiWindow. Thanks in advance.


found a solution to the problem. if you observe the following notifications, you are able to get an event when the key is pressed. I added these notifications in my custom uiwindow class so doing it at one place will allow me to get these touch events throughout the application.

[[NSNotificationCenter defaultCenter] addObserver: self selector: @selector(keyPressed:) name: UITextFieldTextDidChangeNotification object: nil];
[[NSNotificationCenter defaultCenter] addObserver: self selector: @selector(keyPressed:) name: UITextViewTextDidChangeNotification object: nil];

- (void)keyPressed:(NSNotification*)notification
{  [self resetIdleTimer];  }

anyways, hope it helps someone else.


iPhoneDev: here is what I am doing.

I have a custom UIWindow object. In this object, there is a NSTimer that is reset whenever there is a touch. To get this touch you have to override the sendEvent method of UIWindow.

this is what the sendEvent method looks like in my custom window class:

- (void)sendEvent:(UIEvent *)event
{
    if([super respondsToSelector: @selector(sendEvent:)])
    {
      [super sendEvent:event];
    }
    else
    {   
        NSLog(@"%@", @"CUSTOM_Window super does NOT respond to selector sendEvent:!");  
        ASSERT(false);
     }

     // Only want to reset the timer on a Began touch or an Ended touch, to reduce the number of timer resets.
     NSSet *allTouches = [event allTouches];
     if ([allTouches count] > 0)
     {
        // anyObject works here.
        UITouchPhase phase = ((UITouch *)[allTouches anyObject]).phase;
        if (phase == UITouchPhaseBegan || phase == UITouchPhaseEnded)
        {
           [self resetIdleTimer];
        }
     }
}

here is the resetIdleTimer:

- (void)resetIdleTimer 
{
    if (self.idleTimer)
    {
        [self.idleTimer invalidate];
    }
    self.idleTimer = [NSTimer scheduledTimerWithTimeInterval:PASSWORD_TIMEOUT_INTERVAL target:self selector:@selector(idleTimerExceeded) userInfo:nil repeats:NO];
}

after this, in the idleTimerExceeded, I send a message to the window delegates, (in this case, the appDelegate).

- (void)idleTimerExceeded
{
    [MY_CUSTOM_WINDOW_Delegate idleTimeLimitExceeded];
}

When I create this custom window object in the appDelegate, I set the appDelegate as the delegate for this window. And in the appDelegate definition of idleTimeLimitExceeded is where I do what I have to when the timer expires. They key thing is the create the custom window and override the sendEvent function. Combine this with the two keyboard notification shown above I added in the init method of the custom window class and you should be able to get 99% of all touch events on screen anywhere in the application.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜