This is the core class for the creation of your own custom keyboard
The input method service class that can be extended to configure the keyboard behaviour, respond to keyboard requests and listen to keyboard events.
Notes:
class CustomKeyboardService : KeyboardService() {
var lateinit sharedPreferences: SharedPreferences
override fun onCreate() {
sharedPreferences = getSharedPreferences("default", MODE_PRIVATE)
super.onCreate()
eventBus.configuration.subscribe {
when (it) {
is AutoCorrectionChanged -> {
sharedPreferences.edit().putBoolean(“autoCorrect”, it.enabled).apply()
}
}
}
}
override val appIcon get() = R.drawable.ic_logo
override fun createConfiguration() =
KeyboardConfiguration(
typing = TypingConfiguration(
autoCorrect = sharedPreferences.getBoolean(“autoCorrect”, true)
),
style = StyleConfiguration(
forceTheme = SystemThemes.lightTheme,
forceDarkTheme = SystemThemes.darkTheme
)
)
}
Actions | Description |
---|---|
createConfiguration | Invoked when the keyboard requires a configuration |
onAppsButtonClicked | Invoked when the user clicks the app icon |
reloadConfiguration | Requests to reload the configuration |
setUserWords | Set words to the user dictionary |
setInputValidator | Set an input validator |
updateFlaggedWords | Sets a list of flagged words |
appIcon | Icon to be displayed on the top-left corner |
leadingTopBarView | View on the top left part of the keyboard |
trailingTopBarView | View on the top right part of the keyboard |
configuration | Retrieves the current keyboard configuration |
eventBus | Retrieves the EventBus instance |
open fun createConfiguration() : KeyboardConfiguration
Override this method to provide a custom configuration to the keyboard. See KeyboardConfiguration for details.
Notes:
Example
var currentTheme = SystemThemes.lightTheme
override fun createConfiguration() =
KeyboardConfiguration(
style = StyleConfiguration(
forceTheme = currentTheme
)
)
open fun onAppsButtonClicked()
Override this method to perform an action when the user taps the app icon.
Example:
override fun onAppsButtonClicked() {
val intent = Intent(this, MainActivity::class.java)
intent.flags = Intent.FLAG_ACTIVITY_NEW_TASK
startActivity(intent)
}
fun reloadConfiguration()
Call this method to reload the keyboard configuration. This will invoke the createConfiguration method.
Example:
var currentTheme = SystemThemes.lightTheme
private fun forceDarkTheme() {
currentTheme = SystemThemes.darkTheme
reloadConfiguration()
}
fun setUserWords(words: List<String>)
Set a list of words to be used as the user dictionary for the current configured language.
Notes:
Example:
private fun setUserDictionary() {
setUserWords(
sharedPreferences.getStringSet(“userDictionary”, emptySet()).toList()
)
}
fun setInputValidator(validator: InputValidator?)
Sets an input validator for key taps and emojis. See InputValidator for details.
Example:
setInputValidator(object : InputValidator.DefaultInputValidator() {
override fun validateInput(label: String, keyCode: Int) =
when (label) {
"\uD83D\uDD95" -> false
"\uD83D\uDCA9" -> false
else -> true
}
})
fun updateFlaggedWords(locale: String, file: File, restartEngine: Boolean)
Updates the flagged words for a locale.
Notes:
super.onCreate
has been invoked.Example:
override fun onCreate() {
...
super.onCreate()
...
updateFlaggedWordsFromAssets(“en-GB”, false)
updateFlaggedWordsFromAssets(“en-US”, true)
...
}
private fun updateFlaggedWordsFromAssets(locale: String, restartEngine: Boolean) {
val sharedPreferenceKey = “asset_flagged_$locale”
if (!sharedPreferences.getBoolean(sharedPreferenceKey, false)) {
val sourceAsset = "flagged/$locale.txt"
val destination = File(filesDir, "flagged_words_$locale.txt")
try {
resources.assets.open(sourceAsset).apply {
copyTo(FileOutputStream(destination))
close()
}
updateFlaggedWords(locale, destination, restartEngine)
} catch (exception: IOException) {
Log.e("SampleKeyboard", "Error copying asset file: ${exception.message}")
}
sharedPreferences.edit().putBoolean(sharedPreferenceKey, true).apply()
}
}
open val appIcon : @DrawableRes Int?
Override this field’s getter to set the app icon displayed on the top left corner.
Notes:
Example:
override val appIcon get() = R.drawable.ic_logo
override val leadingTopBarView: View?
Override this field to set a leading top bar view displayed on the top left corner.
This view replaces appIcon
.
override val trailingTopBarView: View?
Override this field to set a trailing top bar view displayed on the top right corner.
This view is compatible with the leadingTopBarView
, but please be aware that the space for the predictions view might be significantly reduced.
This view and appIcon
are compatibles.
val configuration : KeyboardConfiguration
Retrieves the current configuration in use by the keyboard. See KeyboardConfiguration for details. This is a read-only field.
Example:
val lightTheme = SystemThemes.lightTheme
val darkTheme = SystemThemes.darkTheme
private fun switchTheme() {
val wasLightTheme = configuration.style.forceTheme == lightTheme
currentTheme = if (wasLightTheme) darkTheme else lightTheme
reloadConfiguration()
}
val eventBus : EventBus
Access the EventBus object in order to listen to keyboard events. See EventBus for details about the events emitted by the keyboard SDK.
Notes:
Example:
private fun setupEvents() {
eventBus.configuration.subscribe {
when (it) {
is ConfigurationEvent.AutoCorrectionChanged -> {
sharedPreferences.edit().putBoolean(“autoCorrect”, it.enabled).apply()
}
}
}
}