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

编程入门 行业动态 更新时间:2024-10-28 20:21:30
本文介绍了如果在 uitextview 中按下 shift 键(修饰键),则检测/获取通知的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

限时送ChatGPT账号..

我想弄清楚是否有任何方法可以检测是否按下了 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屋!

更多推荐

[db:关键词]

本文发布于:2023-04-29 03:36:01,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1183859.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:按下   通知   uitextview   shift

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!