tags:

views:

25

answers:

1

How can I tell what keys the user pressed into a textView?

And before you ask since it sounds similar to a keylogger, I'm making a typing app and I need to know if what they entered of the correct matching key to what they were prompted.

Thanks!

+1  A: 

You should set the delegate of the UITextView to one of your classes. (in IB or programmatically, does not matter)

In your delegate, you can put the following function, or something similar:

-(BOOL)textView:(UITextView *)textView shouldChangeTextInRange:(NSRange)range
                  replacementText:(NSString *)text
{
    if ( [text length] == 0 ) return YES; // always allow deletion of characters

    NSString *new = [textView.text stringByReplacingCharactersInRange:range
                                                           withString:text];

    if ( [new length] > 100 ) // PUT IN YOUR MAGIC CONDITION HERE
    {
        return NO; // don't allow the edit to happen
    }

    return YES; // by default, allow the edit to happen
}

this will only limit input to 100 chars, but you can make it as complicated as you see fit.

edit ps, you asked "what key the user pressed", but since we also have copy&paste and auto-correction, this may give a text which is longer than 1 char!

mvds
i linked it into my files owner (view controler) is that good?
Yes, if you have made you own view controller class containing this function, and set the class of the controller to your own class in IB, it should work.
mvds
Tell me if im doing this right. I have 2 classes app delagate and view controller. I dragged my textView's delagate into files owner. I copy pasted that method into my view controller and called it like so.`[self textView:typingText shouldChangeTextInRange:5 :@"replace with this"];`I assumed the arguments are the name of the textview you want to affect, the amount of characters in the textview or the range you want to affect and the text you want to have put it in when the text doesnt match paramaters.
No you're seeing it the wrong way around now: the UITextView will call the `shouldChangeTextInRange` when a key is pressed. put in a `NSLog("shouldChangeText called!");` to see if and when it's called. The connection you made is ok **if and only if the file's owner is your view controller!** (which you can see in the 'type' column in IB, when looking at the list view, or in the attributes inspector) Changing the text in the textview is done using `textView.text = @"hello world";`
mvds
I get it now! so i was trying to call a method that is called when the text is changed!EDIT: I added a nslog to the method but it doesnt print anything so it isnt being called...
Great! Make sure you have the exact right name for the function (*and* it's arguments), and make sure the delegate points to the right class, there's not much more I can do from here. You are not mixing up UITextField and UITextView are you? Those are different! And remember to always save your changes in IB before re-compiling the thing.
mvds
i'll work on it myslef and see what i can do.