nathantannar4 / InputBarAccessoryView

A simple and easily customizable InputAccessoryView for making powerful input bars with autocomplete and attachments
MIT License
1.15k stars 229 forks source link

feat: key commands for external keyboards #243

Open nathanwhy opened 1 year ago

nathanwhy commented 1 year ago

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
}
Janneman84 commented 1 year ago

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.

nathantannar4 commented 1 year ago

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

nathanwhy commented 1 year ago

@Janneman84 can you show me how to achieved it?

Janneman84 commented 1 year ago

@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.