簡體   English   中英

如何檢測用戶何時更換鍵盤?

[英]How to detect when user changes keyboards?

有什么方法可以檢測用戶何時更改鍵盤類型,在這種情況下特別是表情符號鍵盤?

您可以使用UITextInputMode來檢測currentInputMode的當前語言-表情符號被視為一種語言。 文檔

UITextInputMode類的實例代表當前的文本輸入模式。 您可以使用此對象來確定當前用於文本輸入的主要語​​言。

您可以像這樣測試表情符號鍵盤:

NSString *language = [[UITextInputMode currentInputMode] primaryLanguage];
BOOL isEmoji = [language isEqualToString:@"emoji"];
if (isEmoji)
{
   // do something
}

您可以通過UITextInputCurrentInputModeDidChangeNotification通知輸入模式更改。 當前輸入模式更改時,此消息將發布。

這是一個簡單的應用程序,每當模式更改時,該應用程序都會打印NSLog

- (void)viewDidLoad
{
    [super viewDidLoad];
    [[NSNotificationCenter defaultCenter] addObserver:self
         selector:@selector(changeInputMode:) 
             name:UITextInputCurrentInputModeDidChangeNotification object:nil];}
}

-(void)changeInputMode:(NSNotification *)notification
{
    NSString *inputMethod = [[UITextInputMode currentInputMode] primaryLanguage];
    NSLog(@"inputMethod=%@",inputMethod);
}  

或者,如果您更喜歡Swift

import UIKit

class ViewController: UIViewController 
{

    override func viewDidLoad() {
        super.viewDidLoad()

        NSNotificationCenter.defaultCenter().addObserver(self, 
       selector: "changeInputMode:", 
           name: UITextInputCurrentInputModeDidChangeNotification, object: nil)
    }

    func changeInputMode(notification : NSNotification)
    {
        let inputMethod = UITextInputMode.currentInputMode().primaryLanguage
        println("inputMethod: \(inputMethod)")
    }


}

斯威夫特4:

NotificationCenter.default.addObserver(self,
     selector: #selector(FirstViewController.changeInputMode(_:)),
     name: NSNotification.Name.UITextInputCurrentInputModeDidChange, object: nil)

func changeInputMode(_ notification: Notification) 
    {
        let inputMethod = UITextInputMode.activeInputModes.description
        print("keyboard changed to \(inputMethod.description)")
    }

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM