如果在 uitextview 中按下 shift 键(修饰键),则检测/获取通知

dip*_*ips 5 shift uitextview modifier-key uikeyboard

我试图弄清楚是否有任何方法可以检测是否shift按下了键,或者在shift按下键时是否发送了任何通知UIKeyboard

- (BOOL) textView:(UITextView *)textView shouldChangeTextInRange:(NSRange)range replacementText:(NSString *)string
Run Code Online (Sandbox Code Playgroud)

不会因为shift按键而被调用,因为它是一个修饰键。

b00*_*tsy 2

不知道您是否需要结合其他文本来了解它,还是只检测单次Shift点击,但这是我检测前者的解决方案:

就我而言,这只是其中的一个字符:

- (BOOL)textView:(UITextView *)textView shouldChangeTextInRange:(NSRange)range replacementText:(NSString *)inText
Run Code Online (Sandbox Code Playgroud)

这会将大写文本转换为小写并设置 shiftPressed 值(如果文本是非字母文本则忽略)

//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;
        }
    }
}  
Run Code Online (Sandbox Code Playgroud)