Is there a way to detect a touch on the onscreen keyboard?

I don’t care about which key was pressed, or how long, or anything like that. All I need is a way to detect that the user has touched the screen, even if that happens to be on the part of the screen covered by a keyboard.

My goal here is to detect a lack of interaction to ‘reset’ the app to a default state if left along long enough — think a ‘kiosk mode’ application. The issue is that I can’t detect when the keyboard is touched, as the keyboard apparently intercepts all touch events, even before my customer window can handle them.

  • Custom action when clicking on UITabBarController
  • Using MDM To Configure An Enterprise App Via NSUserDefaults
  • How can I enable Xcode refactor options for an external build project?
  • Accessing a value of a class's variable from another - objective-c
  • Rounding a double value to x number of decimal places in swift
  • How can I get all image names in asset catalog group?
  • Edit:

    Considered (and dismissed) is the use of just keyboard show and hide notifications — we need to prolong screen display if the user is actively typing. Since we use UIWebViews to display certain things, we also can’t use the delegate methods of UITextViews or UITextFields.

    3 Solutions Collect From Internet About “Is there a way to detect a touch on the onscreen keyboard?”

    It sounds like detecting all touches—on the keyboard and elsewhere—would suffice. We can do that by subclassing UIApplication to override sendEvent:.

    We’ll extend the UIApplicationDelegate protocol with a new message, application:willSendTouchEvent:, and we’ll make our UIApplication subclass send the message to its delegate before handling any touch event.

    MyApplication.h

    @interface MyApplication : UIApplication
    @end
    
    @protocol MyApplicationDelegate <UIApplicationDelegate>
    - (void)application:(MyApplication *)application willSendTouchEvent:(UIEvent *)event;
    @end
    

    MyApplication.m

    @implementation MyApplication
    
    - (void)sendEvent:(UIEvent *)event {
        if (event.type == UIEventTypeTouches) {
            id<MyApplicationDelegate> delegate = (id<MyApplicationDelegate>)self.delegate;
            [delegate application:self willSendTouchEvent:event];
        }
        [super sendEvent:event];
    }
    
    @end
    

    We’ll need to make our app delegate conform to the MyApplicationDelegate protocol:

    AppDelegate.h

    #import "MyApplication.h"
    
    @interface AppDelegate : UIResponder <MyApplicationDelegate>
    // ...
    

    AppDelegate.m

    @implementation AppDelegate
    
    - (void)application:(MyApplication *)application willSendTouchEvent:(UIEvent *)event {
        NSLog(@"touch event: %@", event);
        // Reset your idle timer here.
    }
    

    Finally, we need to make the app use our new MyApplication class:

    main.m

    #import "AppDelegate.h"
    #import "MyApplication.h"
    
    int main(int argc, char *argv[])
    {
        @autoreleasepool {
            return UIApplicationMain(argc, argv,
                NSStringFromClass([MyApplication class]),
                NSStringFromClass([AppDelegate class]));
        }
    }
    

    How about using the keyboardShow and keyBoardHide Notifications to set a timer? After x seconds you return the app to your required state.

    Could even reset the timer in scrollview delegates or textField delegates if you want to.

    Take a look at:

    [[NSNotificationCenter defaultCenter] addObserver:self 
                                      selector:@selector(keyboardDidShow:) 
                                          name:UIKeyboardDidShowNotification 
                                        object:nil];    
    

    And a method like this:

    - (void)keyboardDidShow:(NSNotification *)note {
        idleTimer = [NSTimer scheduledTimerWithTimeInterval:120 
                             target:nil 
                           selector:@selector(yourMethodHere) 
                           userInfo:nil 
                            repeats:NO]; 
    }
    

    Be sure to declare and @synthesize your timer in the .h and .m file.

    Hope it helps.

    UITextField or UITextView has a delegate method to detect when user types something:

    - (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string
    {
       // user tapped a key
       //
       // reset your "idle" time variable to say user has interacted with your app
    }
    

    Remember to conform to the UITextField or UITextView protocol depending which one you use (maybe both if you’ve got both text field and text views). Also remember to mark each of your text field or text view’s delegate as your view controller.

    <UITextFieldDelegate,UITextViewDelegate>
    

    Updated Answer

    Ron, not sure if you actually googled but I found this:

    iPhone: Detecting user inactivity/idle time since last screen touch