Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: key commands for external keyboards #243

Open
wants to merge 4 commits into
base: master
Choose a base branch
from

Conversation

nathanwhy
Copy link

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
}


@objc private func didDetectKeyCommand(_ keyCommand: UIKeyCommand) {
if keyCommand.input == nil { return }
let key = "\(String(describing: keyCommand.input!))_\(keyCommand.modifierFlags)"
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not a big fan of this force unwrap - can we figure out better way for the key?

@Janneman84
Copy link

Janneman84 commented May 24, 2023

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
Copy link
Owner

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
Copy link
Author

@Janneman84 can you show me how to achieved it?

@Janneman84
Copy link

Janneman84 commented Jun 16, 2023

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

@@ -187,7 +187,6 @@ open class KeyboardManager: NSObject, UIGestureRecognizerDelegate {
}
}
callbacks[.willHide] = { [weak self] (notification) in
guard notification.isForCurrentApp else { return }
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What's the reason for removing this?

@@ -344,7 +344,7 @@ open class InputBarAccessoryView: UIView {
// MARK: - Auto-Layout Constraint Sets

private var middleContentViewLayoutSet: NSLayoutConstraintSet?
private var textViewHeightAnchor: NSLayoutConstraint?
open var textViewHeightAnchor: NSLayoutConstraint?
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Unintentional change as well?

# Conflicts:
#	Sources/KeyboardManager/KeyboardManager.swift
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants