Receive iPhone keyboard events

14,073

Solution 1

Use NSNotificationCenter

[[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
{
  NSLog([[notification object]text]);
}

Solution 2

I wrote about catching events using a little hack of UIEvent in my Blog

Please refer to : Catching Keyboard Events in iOS for details.

From the above mentioned blog:

The trick is in accessing GSEventKey struct memory directly and check certain bytes to know the keycode and flags of the key pressed. Below code is almost self explanatory and should be put in your UIApplication subclass.

#define GSEVENT_TYPE 2
#define GSEVENT_FLAGS 12
#define GSEVENTKEY_KEYCODE 15
#define GSEVENT_TYPE_KEYUP 11

NSString *const GSEventKeyUpNotification = @"GSEventKeyUpHackNotification";

- (void)sendEvent:(UIEvent *)event
{
    [super sendEvent:event];

    if ([event respondsToSelector:@selector(_gsEvent)]) {

        // Key events come in form of UIInternalEvents.
        // They contain a GSEvent object which contains 
        // a GSEventRecord among other things

        int *eventMem;
        eventMem = (int *)[event performSelector:@selector(_gsEvent)];
        if (eventMem) {

            // So far we got a GSEvent :)

            int eventType = eventMem[GSEVENT_TYPE];
            if (eventType == GSEVENT_TYPE_KEYUP) {

                // Now we got a GSEventKey!

                // Read flags from GSEvent
                int eventFlags = eventMem[GSEVENT_FLAGS];
                if (eventFlags) { 

                    // This example post notifications only when 
                    // pressed key has Shift, Ctrl, Cmd or Alt flags

                    // Read keycode from GSEventKey
                    int tmp = eventMem[GSEVENTKEY_KEYCODE];
                    UniChar *keycode = (UniChar *)&tmp;

                    // Post notification
                    NSDictionary *inf;
                    inf = [[NSDictionary alloc] initWithObjectsAndKeys:
                      [NSNumber numberWithShort:keycode[0]],
                      @"keycode",
                      [NSNumber numberWithInt:eventFlags], 
                      @"eventFlags",
                      nil];
                    [[NSNotificationCenter defaultCenter] 
                        postNotificationName:GSEventKeyUpNotification 
                                      object:nil
                                    userInfo:userInfo];
                }
            }
        }
    }
}

Solution 3

Not a simple answer, but I think you have two approaches available.

  1. subclass the input components (UITextView, UITextField, etc) as you've done with the UIWindow.

  2. Create a application wide UITextViewDelegate (and UITextFieldDelegate) and assign all your input field delegates to it.

Share:
14,073
iamMobile
Author by

iamMobile

Updated on June 25, 2022

Comments

  • iamMobile
    iamMobile almost 2 years

    Is there anyway to trap all keyboard events across my application? I need to know if user is entering anything using keyboard across my application (Application has multiple views). I was able to capture touchEvents by subclassing UIWindow but unable to capture keyboard events.

  • iamMobile
    iamMobile almost 15 years
    This is little tricky now and involves lots of work. I was hoping to see some App level event to capture
  • Adam Lear
    Adam Lear over 12 years
  • nacho4d
    nacho4d over 12 years
    I don't understand what I got -1. Yes, the link is to a post of my blog but it fully answers this question. It catches ALL keyboard events including Shift, Cmd, Ctrl, Alt. If it is because of the use of Private APIs, I clearly stated it is a hack. I am not tricking anybody here.
  • Coyote
    Coyote over 11 years
    Probably because your answer does not answer the question, it simply points to an external source which can disappear, get broken or simply change beyond this question. As Anna Lear points out, read the section she pointed to. The answer point to the following as being the proper rules to follow: - you paraphrase the content of the linked item (possibly omitting details or examples) - you identify the author (yourself, MSDN, etc) - someone could benefit from the answer without reading the linked item at all - you include information to let the reader decide if clicking the link is worthwhile
  • pcans
    pcans over 11 years
    Maybe not a well formed answer, but this this is exactly what I was looking for. Thanks.