2

I'm trying to capture all the touch events in a UIKeyboard without breaking its functionality. I've tried:

  • HitTest:
  • UIGestureRecognizer
  • Adding a UIWindow in the top and pass events to next responder

However, none of these worked.

And it seems that we aren't allowed to sub class the UIKeyboard.

Can you think of any method that may work?

Thanks,

Peak

Update:

Lets simplify the problem: How can I add a UIView or UIWindow that passes specific touch events to the next responder(just like setting the userInteractionEnabled to NO)?

Heres my code(of course can't work...):

   - (void)sendEvent:(UIEvent *)event {
    NSLog(@"%@",event);
    //if ([self eventIsNoteworthy:event]) [self extraEventHandling:event];
    [super sendEvent:event];  // Apple says you must always call this!
}

-(UIView *)hitTest:(CGPoint)point withEvent:(UIEvent *)event {
    CGPoint hitPoint = [underneathButton convertPoint:point fromView:self];
    if ([underneathButton pointInside:hitPoint withEvent:event]) return underneathButton;
    NSLog(@"Called");
    return [super hitTest:point withEvent:event];
    //return mainWindow;
}

-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event{
    NSLog(@"Start");

    [self.nextResponder touchesBegan:touches withEvent:event];

}
PeakJi
  • 1,547
  • 14
  • 23
  • When you say touch events, do you mean the sequence certain keys were pressed? – blake305 Apr 12 '12 at 23:43
  • @blake305 No, I mean the CGPoints of the touched positions. – PeakJi Apr 16 '12 at 22:35
  • Are you wanting now to build your own keyboard, present it, and have it pass the letters pressed to the textField, and NSLog the CGPoints? – AMayes Apr 19 '12 at 20:38
  • @AMayes I can build my own keyboard, but I need to use the system keyboard here(Diffrent languages). I just want the CGPoints ;-) – PeakJi Apr 20 '12 at 21:42

2 Answers2

0

I'm not sure if it's possible or not. A couple of things come to mind. One is to recreate the UIKeyboard yourself and then capture the touches that way. Apple Docs stating it's possible.

The other would be to re-look at how you added the UIWindow overtop of the keyboard. It's my understanding that the keyboard view is somehow layered differently over your app so you might not have had the UIWindow overtop of the keyboard (ref). That link gives a solution for getting a reference to the actual UIView for the keyboard, from there you'd want to place your UIWindow onto the parent of the keyboard view.

To help, here's a script that you can use to dump out all the views to see how they're being layered:

static void dumpViews(UIView* view, NSString *text, NSString *indent) 
{
    Class cl = [view class];
    NSString *classDescription = [cl description];
    while ([cl superclass]) 
    {
        cl = [cl superclass];
        classDescription = [classDescription stringByAppendingFormat:@":%@", [cl description]];
    }

    if ([text compare:@""] == NSOrderedSame)
        NSLog(@"%@ %@", classDescription, NSStringFromCGRect(view.frame));
    else
        NSLog(@"%@ %@ %@", text, classDescription, NSStringFromCGRect(view.frame));

    for (NSUInteger i = 0; i < [view.subviews count]; i++)
    {
        UIView *subView = [view.subviews objectAtIndex:i];
        NSString *newIndent = [[NSString alloc] initWithFormat:@"  %@", indent];
        NSString *msg = [[NSString alloc] initWithFormat:@"%@%d:", newIndent, i];
        dumpViews(subView, msg, newIndent);
        [msg release];
        [newIndent release];
    }
}

Call it like this: dumpViews([[UIApplication sharedApplication] keyWindow], @"", @"");

Hope that helps some! :D

Gavin Miller
  • 43,168
  • 21
  • 122
  • 188
  • 2
    Here’s a one-liner that you can use in the debugger to achieve the same thing: `po [[[UIApplication sharedApplication] keyWindow] recursiveDescription]` – cbowns Apr 13 '12 at 00:29
  • Thanks for you answer;-) I'm sure my UIWindow is on the top of the keyboard. I've updated the question. – PeakJi Apr 13 '12 at 19:15
0

Take a look at this answer about accessing the (private view hierarchy that contains the) UIKeyboard. N.b. this is unsupported in iOS, and Apple rejects apps that do this. (Interfering with private view hierarchies inside UIKit is tantamount to using private API.)

Community
  • 1
  • 1
cbowns
  • 6,295
  • 5
  • 47
  • 64