收听键盘按下通知

时间:2017-07-31 23:29:59

标签: ios swift

我已经将var input, cwidRows, results, i; // divide input string into an array of strings representing each line input = stringFromTextArea; cwidRows = input.split("\n"); // put acceptable results into results array for later use results = []; for (i = 0; i < cwidRows; i++) { if (cwidRows[i].split(":")[1] !== "Y") results.push(cwidRows[i]); } 子类化,并设法为UIApplication函数设置了一个监听器。当用户触摸键盘上的某个按钮时,如何收到通知?我可以在sendEvent中获取触摸位置,是否可以将此位置转换为所选字符?

sendEvent

2 个答案:

答案 0 :(得分:0)

你根本就不能。

如果开发人员和应用程序可以访问用户密码,那将是一个巨大的安全风险。即使您可以使用私有API收听有关键命中的键盘通知(我认为这是不可能的),您的应用程序永远不会进入AppStore,它会被拒绝。

如果您自己制作键盘,则只能接收有关应用程序外键击的信息,但即使这样,系统也会使用所有密码和密码的默认键盘。

答案 1 :(得分:0)

我通过将位置转换为密钥来获取密钥 此方法仅适用于带有默认键盘的i phone 7 plus 并且有时候它返回错误的键并不完美

func  getKeyFromLocation(location : CGPoint) -> String{
    var keysLocation   = [String: CGPoint]()
    keysLocation["q"] = CGPoint(x: 29, y: 29)
    keysLocation["w"] = CGPoint(x: 68, y: 29)
    keysLocation["e"] = CGPoint(x: 107, y: 29)
    keysLocation["r"] = CGPoint(x: 146, y: 29)
    keysLocation["t"] = CGPoint(x: 185, y: 29)
    keysLocation["y"] = CGPoint(x: 224, y: 29)
    keysLocation["u"] = CGPoint(x: 263, y: 29)
    keysLocation["i"] = CGPoint(x: 302, y: 29)
    keysLocation["o"] = CGPoint(x: 351, y: 29)
    keysLocation["p"] = CGPoint(x: 390, y: 29)
    keysLocation["a"] = CGPoint(x: 60, y: 80)
    keysLocation["s"] = CGPoint(x: 99, y: 80)
    keysLocation["d"] = CGPoint(x: 138, y: 80)
    keysLocation["f"] = CGPoint(x: 177, y: 80)
    keysLocation["g"] = CGPoint(x: 216, y: 80)
    keysLocation["h"] = CGPoint(x: 255, y: 80)
    keysLocation["j"] = CGPoint(x: 294, y: 80)
    keysLocation["k"] = CGPoint(x: 333, y: 80)
    keysLocation["l"] = CGPoint(x: 372, y: 80)
    keysLocation["z"] = CGPoint(x: 99, y: 140)
    keysLocation["x"] = CGPoint(x: 138, y: 140)
    keysLocation["c"] = CGPoint(x: 177, y: 140)
    keysLocation["v"] = CGPoint(x: 216, y: 140)
    keysLocation["b"] = CGPoint(x: 255, y: 140)
    keysLocation["n"] = CGPoint(x: 294, y: 140)
    keysLocation["m"] = CGPoint(x: 333, y: 140)

    var selectedkeycode = ""
    var minDistance = CGFloat(1000.0)

    for (keycode, point) in keysLocation {
        let distance = hypot(location.x - point.x, location.y - point.y)
        if(distance < minDistance ){
            minDistance = distance
            selectedkeycode = keycode
        }

    }

    print(minDistance)

    if(minDistance > CGFloat(50.0)){
        return ""
    }
    else{
        return selectedkeycode
    }



}