InputBarAccessoryView icon indicating copy to clipboard operation
InputBarAccessoryView copied to clipboard

feat: key commands for external keyboards

Open nathanwhy opened this issue 1 year ago • 4 comments

What does this implement/fix? Explain your changes.

Support for external keyboard to send messages by pressing command + enter. On the Mac catalyst, it also supports keyboard shortcuts.

Does this close any currently open issues?

No

Where has this been tested?

Devices/Simulators: … iPad mini 6 iOS Version: … iOS 16.2 Swift Version: … 5.7

How to use:

messageInputBar.inputTextView.observeKeyInput("\r", modifiers: .command, discoverabilityTitle: nil) {
    // send message with command + enter
}

nathanwhy avatar Apr 16 '23 07:04 nathanwhy

In my app I made it so pressing physical return key sends message and shift+return adds a new line. I believe this is how other chat apps work too.

If interested I can show code of how I achieved this.

Janneman84 avatar May 24 '23 19:05 Janneman84

I think this will break addKeyCommand

https://developer.apple.com/documentation/uikit/uiviewcontroller/1621439-addkeycommand

I don't think this change is necessary, since key commands can already be adde

nathantannar4 avatar May 25 '23 08:05 nathantannar4

@Janneman84 can you show me how to achieved it?

nathanwhy avatar Jun 16 '23 06:06 nathanwhy

@Janneman84 can you show me how to achieved it?

extension InputTextView {
    
    open override var keyCommands: [UIKeyCommand]? {
        return [
            //hitting return on hardware keyboard will send the message instead of carriage return
            //to go to next line press shift+return
            UIKeyCommand(input: "\r", modifierFlags: [], action: #selector(keyCommand_return)),
        ]
    }
    
    @objc private func keyCommand_return() {
        //trigger send press here
    }
}

I use an extension but you can probably implement the override directly in InputTextView.

However if you plan on it calling didPressSendButtonWith I would need to know wether this is triggered through button touch or keyboard return press. This because my send button is the photo button when there is no text entered. It opens the camera when pressed, but I don't want it to open the camera when you hit return on the keyboard. Hence I need to be able to make the distinction.

Janneman84 avatar Jun 16 '23 22:06 Janneman84