detect/get notification if shift key (modifier-key) pressed in uitextview

放肆的年华 提交于 2019-12-09 13:19:11

问题


I'm trying to figure out if there is any way in which we can detect if shift key is pressed or if any notification is sent when shift key is pressed in UIKeyboard

- (BOOL) textView:(UITextView *)textView shouldChangeTextInRange:(NSRange)range replacementText:(NSString *)string

does not get called for shift key press since it is a modifier key.


回答1:


Don't know whether you need to know it in combination with other text or only detect the single shift tap, but here is my solution for detecting the former:

in my case this is only a single character within:

- (BOOL)textView:(UITextView *)textView shouldChangeTextInRange:(NSRange)range replacementText:(NSString *)inText

this converts uppercase text to lowercase and sets the shiftPressed Value (ignored if the text is non letter text)

//Shift Detection
BOOL shiftPressed = NO;

//Detect non letter characters
NSCharacterSet *letterCharacterSet = [NSCharacterSet letterCharacterSet];
NSString *trimmedText = [text stringByTrimmingCharactersInSet:letterCharacterSet];

if ([text length] == 1) {  
    if (![trimmedText length]) 
    {            
        NSString *upperCaseString = [text uppercaseString];
        if ([upperCaseString isEqualToString:text]) {
            NSString *lowerCaseString = [text lowercaseString];
            text = lowerCaseString;
            shiftPressed = YES;
        }
    }
}  


来源:https://stackoverflow.com/questions/5560630/detect-get-notification-if-shift-key-modifier-key-pressed-in-uitextview

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!