Custom Sound using KeyboardKit SwiftUI

I’m using keyboardkit and I’m unable to change default keyboard sound. how can I do that ?

Hey @maazali30,

I’ve checked out KeyboardKit’s docs, and found APIs that may help you.
I haven’t played around with it but it seems what you need.

Checkout KeyboardKit’s feedback engine wherein you can create a custom handler that triggers audio feedback on tap.

Since you wanted to create a custom sound, you can either try creating a custom static audio feedback engine and use it as the audio feedback on your custom handler, or you can try out using AudioFeedback’s .custom(id: UInt32) enum case.

When you choose to do the former (custom audio feedback engine), you can use an audio playing API that would play your sound media on the feedback trigger, but note that key presses may be fast so your audio should also be short (like 0.1millis short), or add more handling that would cancel an existing playing audio media and replay the audio when the next keyboard feedback is received.

thank you for your kind response.

I’m able to generate a custom audio using this code

Button("Play Custom Audio") {
     StandardAudioFeedbackEngine.shared.trigger(AudioFeedback.custom(id: 1125))
}

but I’m unable to find a way to make it into a default audio for all buttons on the keyboard. I tried to do it with keyboardFeedbackHandler but failed:

class MyKeyboardFeedbackHandler: StandardKeyboardFeedbackHandler {
    
    override func triggerFeedback(for gesture: KeyboardGesture, on action: KeyboardAction) {
        if gesture == .press {
            StandardAudioFeedbackEngine.shared.trigger(AudioFeedback.custom(id: 1125))
        }
        super.triggerFeedback(for: gesture, on: action)
    }
}

class MyKeyboardViewController: KeyboardInputViewController {

    override func viewDidLoad() {
        keyboardFeedbackHandler =  MyKeyboardFeedbackHandler(settings: KeyboardFeedbackSettings())
        super.viewDidLoad()
    }
}

any idea ?

1 Like

@inaki can you please look into it.