最近项目遇到一个新需求,通过键盘来控制程序,通过官方文档和在网上进行了一些调研,可以通过添加KeyCommand来监听键盘输入以实现需求。整理一些基本方法
keyCommand初始化
//基本构成input:键盘输入文本值 ,modifierFlags:可与主键设置关联,不需要设置关联时初始化0(见 UIKeyModifierFlags,主键基本类型),action:对应触发事件
UIKeyCommand.init(input: <#T##String#>, modifierFlags: <#T##UIKeyModifierFlags#>, action: <#T##Selector#>)
添加方法
1.通过addKeyCommand添加
//MARK: setKeyCommands add keyCommand
func setKeyCommands(){
let leftCommand = UIKeyCommand.init(input: UIKeyCommand.inputLeftArrow, modifierFlags: UIKeyModifierFlags.init(rawValue: 0), action: #selector(didTapKey(sender:)))
addKeyCommand(leftCommand)
let rightCommand = UIKeyCommand.init(input: UIKeyCommand.inputRightArrow, modifierFlags: UIKeyModifierFlags.init(rawValue: 0), action: #selector(didTapKey(sender:)))
addKeyCommand(rightCommand)
let escCommand = UIKeyCommand.init(input: UIKeyCommand.inputEscape, modifierFlags: UIKeyModifierFlags.init(rawValue: 0), action: #selector(didTapKey(sender:)))
addKeyCommand(escCommand)
let spaceCommand = UIKeyCommand.init(input: " ", modifierFlags: UIKeyModifierFlags.init(rawValue: 0), action: #selector(didTapKey(sender:)))
addKeyCommand(spaceCommand)
let sCommand = UIKeyCommand.init(input: "s", modifierFlags: UIKeyModifierFlags.init(rawValue: 0), action: #selector(didTapKey(sender:)))
addKeyCommand(sCommand)
let aCommand = UIKeyCommand.init(input: "a", modifierFlags: UIKeyModifierFlags.init(rawValue: 0), action: #selector(didTapKey(sender:)))
addKeyCommand(aCommand)
let dCommand = UIKeyCommand.init(input: "d", modifierFlags: UIKeyModifierFlags.init(rawValue: 0), action: #selector(didTapKey(sender:)))
addKeyCommand(dCommand)
}
2.通过重写父类keycommands添加
override var keyCommands: [UIKeyCommand]?{
return [UIKeyCommand.init(input: UIKeyCommand.inputLeftArrow, modifierFlags: UIKeyModifierFlags.init(rawValue: 0), action: #selector(didTapKey(sender:))),UIKeyCommand.init(input: UIKeyCommand.inputRightArrow, modifierFlags: UIKeyModifierFlags.init(rawValue: 0), action: #selector(didTapKey(sender:))),UIKeyCommand.init(input: UIKeyCommand.inputEscape, modifierFlags: UIKeyModifierFlags.init(rawValue: 0), action: #selector(didTapKey(sender:))),UIKeyCommand.init(input: "0", modifierFlags: UIKeyModifierFlags.command, action: #selector(didTapKey(sender:))),UIKeyCommand.init(input: "3", modifierFlags: UIKeyModifierFlags.init(rawValue: 0), action: #selector(didTapKey(sender:)))]
}
移除keycommands
if let allKeyCommands = self.keyCommands {
for command in allKeyCommands {
self.removeKeyCommand(command)
}
}
UIKeyModifierFlags,主键基本类型
public struct UIKeyModifierFlags : OptionSet {
public init(rawValue: Int)
public static var alphaShift: UIKeyModifierFlags { get } // This bit indicates CapsLock
public static var shift: UIKeyModifierFlags { get }
public static var control: UIKeyModifierFlags { get }
public static var alternate: UIKeyModifierFlags { get }
public static var command: UIKeyModifierFlags { get }
public static var numericPad: UIKeyModifierFlags { get }
}
常见keycommand枚举类型
@available(iOS 8.0, *)
public class let inputUpArrow: String
public class let inputDownArrow: String
public class let inputLeftArrow: String
public class let inputRightArrow: String
public class let inputEscape: String
@available(iOS 8.0, *)
public class let inputPageUp: String
public class let inputPageDown: String
@available(iOS 13.4, *)
public class let inputHome: String
public class let inputEnd: String
@available(iOS 13.4, *)
public class let f1: String
public class let f2: String
public class let f3: String
public class let f4: String
public class let f5: String
public class let f6: String
public class let f7: String
public class let f8: String
public class let f9: String
public class let f10: String
public class let f11: String
public class let f12: String