iphone键盘触摸事件
我需要能够检测键盘上的触摸事件。我有一个应用程序,它显示了一段时间不活动后出现的屏幕(即没有触摸事件)为了解决这个问题,我已经将我的UIWindow分类并实现了sendEvent函数,该函数允许我在整个应用程序中获取触摸事件在一个地方实施该方法。这可以在键盘出现并且用户在键盘上键入时在任何位置工作。我需要知道的是,有没有办法在键盘上检测触摸事件,就像sentEvent为uiWindow所做的一样。提前致谢。iphone键盘触摸事件
找到了解决问题的办法。如果您观察到以下通知,则可以在按下按键时获得事件。我在自定义的uiwindow类中添加了这些通知,所以在一个地方做这些通知将允许我在整个应用程序中获取这些触摸事件。
[[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]; }
无论如何,希望它可以帮助别人。
iPhoneDev:这是我正在做的。
我有一个自定义的UIWindow对象。在这个对象中,有一个NSTimer在触摸时会被重置。为了获得这种触摸,你必须重写UIWindow的sendEvent方法。
这是什么的SendEvent方法看起来像我的自定义窗口类:
- (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];
}
}
}
这里是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];
}
在此之后,在idleTimerExceeded,我将消息发送到窗口委托,(在这种情况下,appDelegate)。
- (void)idleTimerExceeded
{
[MY_CUSTOM_WINDOW_Delegate idleTimeLimitExceeded];
}
当我创建在此的appDelegate自定义窗口对象,我设置的appDelegate为代表此窗口。并且在idleTimeLimitExceeded的appDelegate定义中,我在做计时器到期时所要做的事情。他们的关键是创建自定义窗口并重写sendEvent函数。将此与上面显示的两个键盘通知相结合,我将其添加到自定义窗口类的init方法中,并且您应该能够在应用程序中的任意位置获得屏幕上99%的所有触摸事件。
你可以发布检查屏幕无效的代码吗?我也这样做。只是想知道你在appdelegate上使用计时器来检查用户是否活跃... – iPhoneDev 2010-11-27 14:23:32
请参阅下面的回复。 – Bittu 2010-11-30 19:05:43