如果在 uitextview 中按下 shift 键(修饰键),则检测/获取通知 [英] detect/get notification if shift key (modifier-key) pressed in uitextview

查看:43
本文介绍了如果在 uitextview 中按下 shift 键(修饰键),则检测/获取通知的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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

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

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

推荐答案

不知道你是需要结合其他文字来了解还是只检测单移点击,但这里是我检测前者的解决方案:

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

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

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;
        }
    }
}  

这篇关于如果在 uitextview 中按下 shift 键(修饰键),则检测/获取通知的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆