How do I write this in swift? Related to UIKeyCommand

110 views Asked by At

How do I convert this into swift? This code takes the keyCommand:

 export function isBackspace(keyCode: number): boolean {
      return keyCode === 8;
    }
    
    export function isDeleteForward(
      keyCode: number,
      ctrlKey: boolean,
      shiftKey: boolean,
      altKey: boolean,
      metaKey: boolean,
    ): boolean {
      if (IS_APPLE) {
        if (shiftKey || altKey || metaKey) {
          return false;
        }
        return isDelete(keyCode) || (keyCode === 68 && ctrlKey);
      }
      if (ctrlKey || altKey || metaKey) {
        return false;
      }
      return isDelete(keyCode);
    }

I have converted like this in swift:

public func isBackspace(command: UIKeyCommand) -> Bool {
  return command.input == "\u{8}"
}
public func isDeleteBackward(command: UIKeyCommand, altKey: Bool, ctrlKey: Bool, commandKey: Bool ) -> Bool {
  if altKey || commandKey {
    return false
  }

  return isBackspace(command: command) || (command.input == "H" && ctrlKey)
}

But when I press just "delete" button on keyBoard it doesn't do anything? Am I missing something here?Is the conversion right?

This is my UIKeyCommand code:

override public var keyCommands: [UIKeyCommand]? {

    let commands = [
      UIKeyCommand(input: "\u{8}", modifierFlags: .alternate, action: #selector(selectKeyTapped(command:))),
      UIKeyCommand(input: "\u{8}", modifierFlags: .command, action: #selector(selectKeyTapped(command:))),
      UIKeyCommand(input: "\u{8}", modifierFlags: .control, action: #selector(selectKeyTapped(command:))),
      UIKeyCommand(input: "\u{8}", modifierFlags: [], action: #selector(selectKeyTapped(command:)))
    ]
    if #available(iOS 15, *) {
      commands.forEach { $0.wantsPriorityOverSystemBehavior = true }
    }
    return commands
  }
@objc func selectKeyTapped(command: UIKeyCommand) {
    guard let textView = textView else { return }
    if isDeleteBackward(command: command, altKey: true, ctrlKey: true, commandKey: true) {
      if isBackspace(command: command) {
        deleteCharacter(isBackwards: true)
      } else {
        deleteCharacter(isBackwards: true)
      }
    }
  }
0

There are 0 answers