iPhone键盘触摸事件

发布于 2024-09-14 02:03:02 字数 231 浏览 12 评论 0原文

我需要能够检测键盘上的触摸事件。我有一个应用程序显示在一段时间不活动(即没有触摸事件)后出现的屏幕为了解决这个问题,我对 UIWindow 进行了子类化并实现了 sendEvent 函数,这使我可以通过以下方式获取整个应用程序上的触摸事件在一个地方实施该方法。除了出现键盘并且用户在键盘上打字之外,这在任何地方都有效。我需要知道的是,有没有一种方法可以检测键盘上的触摸事件,有点像 uiWindow 中的 sentEvent 所做的那样。提前致谢。

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 implemented 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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

无声无音无过去 2024-09-21 02:03:02

找到了问题的解决方案。如果您观察到以下通知,则您可以在按下该键时收到事件。我在自定义 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];  }

无论如何,希望它对其他人有帮助。

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.

ζ澈沫 2024-09-21 02:03:02

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% 的触摸事件。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文