2017-07-31 55 views
0

我已经子类UIApplication和管理,以建立一个侦听器sendEvent功能。如何在用户触摸键盘上的某个按钮时收到通知?我可以在sendEvent得到触摸位置,是有可能这个位置转换为选定的字符?听键盘按通知

import Foundation 
import UIKit 

    @objc(Scale) class Scale: UIApplication { 

     override func sendEvent(_ event: UIEvent) { 
      if event.type != .touches { 
       super.sendEvent(event) 
       return 
      } 

      if let touches2 = event.allTouches { 
       for touch in touches2.enumerated() { 
        if(touch.element.view == ViewController.self().forceLabel){ 
         print("text touched ") 
        } 
        if(touch.element.phase == UITouchPhase.began) { 
         //Stuff 
         //print("=====================================") 
         //print (touch.element.view as Any) 
         print("**********************") 
         print(touch.element.location(in: touches2.first?.view)) 
         print("**********************") 
        } 
        if(touch.element.phase == UITouchPhase.ended) { 
         //Stuff 
         print(";") 
        } 
        //print(touch.element.phase) 
       } 
       if let touch = touches2.first { 

       } 
      } 
      super.sendEvent(event) 
     } 
    } 
+0

你英语句子被严重破坏,甚至没有将它们与期限分开。我不知道你的iOS应用程序的子类是什么意思。 –

+0

这样的乐于助人,非常有建设性,非常哇。 @ElTomato – LinusGeffarth

回答

0

我疥癣的位置转换为关键 这种方法对于我的手机7加上默认的键盘 只会工作,拿到钥匙,不健全有时它返回错误的关键

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 
    } 



} 
0

你只是简单的不能。

这将是一个巨大的安全风险,如果开发商,因此应用程序将有机会获得用户的密码。即使你能听使用私有API有关键命中(其中据我而言是不可能的)键盘的通知,您的应用程序将永远不会进入AppStore的,它会被拒绝。

你只能收到有关您的应用程序之外键点击信息,如果你让你自己的键盘,但即使是这样,系统使用的默认键盘所有密码和密码。

+0

其实是应用研究和将不会被发布到AppStore的 –

+0

即使它不会被公开,就我而言,有没有你可以用它来实现这个私有API。我所知道的,唯一的方法,成功地打破了iPhone的密码是使用运动传感器和机器学习,你可以读到它[这里](http://www.idownloadblog.com/2017/04/12/researchers-demonstrate-通行码检测用的方法,其用途,你的电话运动传感器/) –