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!
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!
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!