paescebu / CustomKeyboardKit

Creating custom In App Keyboards with SwiftUI has never been easier!
GNU General Public License v3.0
203 stars 20 forks source link

[Question] playing different feedback sounds i.e input and delete #18

Closed xzilja closed 8 months ago

xzilja commented 8 months ago

Hi 👋

Not sure if issues is correct place to ask this, as it is more of a question, but is it possible to somehow define which feedback sound needs to be played? As of now, it seems like "Input" sound is the only available option, but playing around with ios default keyboard there are 3 distinctive sounds for

  1. Clicking on letters
  2. Clicking on space / shift / done
  3. Clicking on delete / backspace

Would it be possible to add an argument to playSystemFeedback function that allows to define which sound should be played for custom keyboards?

paescebu commented 8 months ago

@xzilja Thanks for the suggestion!

I thought about this a lot. Theoretically theres APIs on Apple's side to select which sound ID can be played. But I don't want to switch to that yet, cause it feels a bit hacky to rely on. Also UIDevice.current.playInputClick() (which CustomKeyboardKit currently uses under the hood) handles automagically if the user has enabled/disabled keyboard input sounds and haptic feedback in the system settings. Whereas any other way I would lose the convenience. Or at least those are the limitations that i am currently aware of.

But theoretically you can just not use the SystemFeedbackHandler and instead play your custom preferred sounds upon button clicks. And use something along those lines inside:

enum NativeSystemSound: UInt32 {

    case click    = 1123
    case delete   = 1155
    case modifier = 1156

    func play() {
        AudioServicesPlaySystemSound(self.rawValue)
    }
}

Where usage would look like:

Button("Hello") {
    NativeSystemSound.click.play()
}
xzilja commented 8 months ago

Thank you for alternative solution, that will do the trick 👌 and I see why you are hesitant adding this in as is, makes sense.