generated from modularizer/HelloWorldKeyboard
-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathKeyboardService.kt
More file actions
177 lines (148 loc) · 6.83 KB
/
KeyboardService.kt
File metadata and controls
177 lines (148 loc) · 6.83 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
package com.example.basickeyboard
import android.inputmethodservice.InputMethodService
import android.util.TypedValue
import android.view.View
import android.widget.Button
import android.widget.LinearLayout
class KeyboardService : InputMethodService() {
// Define a mapping of keys to rows
private val emojis = "👍😎💩"
private val lock = "\uD83D\uDD12"
private val abcLayout = arrayOf(
arrayOf("#", "1", "2", "3", "4", "5", "6", "7", "8", "9", "0", " ⌫ "),
arrayOf("@", "q", "w", "e", "r", "t", "y", "u", "i", "o", "p", ":)"),
arrayOf(lock, "a", "s", "d", "f", "g", "h", "j", "k", "l", "'", " ↵"),
arrayOf(" ⇧ ", "z", "x", "c", "v", "b", "n", "m", ",", ".", "?", "!" ),
arrayOf("123%","(", " ", ")", emojis)
// Add other special keys or rows as needed
)
private val capsLayout = arrayOf(
arrayOf("@", "#", "1", "2", "3", "4", "5", "6", "7", "8", "9", "0", " ⌫ "),
arrayOf("Q", "W", "E", "R", "T", "Y", "U", "I", "O", "P", "(", ")"),
arrayOf(lock, "A", "S", "D", "F", "G", "H", "J", "K", "L", "\"", " ↵ "),
arrayOf(" ⇧ ", "Z", "X", "C", "V", "B", "N", "M", ",", ".", "?", "!" ),
arrayOf("123%", "👍", " ", ":)", emojis)
// Add other special keys or rows as needed
)
// Define rows of keys
private val numLayout = arrayOf(
arrayOf("#", "^", "!", "/", "*", "⌫"),
arrayOf("$", "7", "8", "9", "%", "~"),
arrayOf("(", "4", "5", "6", "+", ")"),
arrayOf("[", "1", "2", "3", "-", "]"),
arrayOf("abc", " 0 ", " . ", " = ", emojis + " ")
// Add other special keys or rows as needed
)
private val emojiLayout = arrayOf(
// Row 1: Most Popular Emotions & Gestures
arrayOf("😂", "😍", "😎", "💀", "😇", "😢", "🥳", "😡", "⌫"),
// Row 2: More weird faces and expressions
arrayOf("😈", "🤡", "🤠", "🤑", "🤓", "🤖", "👽", "👾", "👻"),
// Row 2: Activities & Celebrations
arrayOf(":)", "👀", "🙈", "💩", "💃", "🎉", "👅", "🚴♀️", "!"),
// Row 3: Animals, Nature & Weather
arrayOf("<3", "🚗", "🐱", "😤", "🙏", "🌳", "🔥", "❄️", "?"),
// Row 4: Food, Objects & Symbols
arrayOf("123%", "🍕", " ", "👍", "❤️", "abc")
)
private val keyboards = mapOf(
"abc" to abcLayout,
lock to capsLayout,
"123%" to numLayout,
emojis to emojiLayout
)
private var currentKeyboardName = "abc"
private var currentKeyboard = keyboards[currentKeyboardName]!!
private var nextKeyboard: String? = null
override fun onCreateInputView(): View {
// Initialize the keyboard layout
return makeKeyboardLayout(currentKeyboard)
}
private fun switchKeyboard(keyboardName: String, temporary: Boolean = false) {
if (temporary) {
nextKeyboard = currentKeyboardName
}else{
nextKeyboard = null
}
currentKeyboardName = keyboardName
currentKeyboard = keyboards[currentKeyboardName]!!
setInputView(onCreateInputView())
}
private fun makeKeyboardLayout(keyboard: Array<Array<String>>): LinearLayout {
val baseLayout = layoutInflater.inflate(R.layout.keyboard_layout, null) as LinearLayout
keyboard.forEach { row ->
val rowLayout = LinearLayout(this)
rowLayout.layoutParams = LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.WRAP_CONTENT)
rowLayout.orientation = LinearLayout.HORIZONTAL
// Calculate the total weight for the row based on the length of the labels
val totalWeight = row.map { it.length }.sum().toFloat()
row.forEach { keyLabel ->
val key = Button(this)
key.text = keyLabel
// Set the text size of the button
key.setTextSize(TypedValue.COMPLEX_UNIT_SP, 20f) // 18sp font size, change as needed
key.setOnClickListener {
val trimmedLabel = keyLabel.trim()
when (trimmedLabel) {
"⇧" -> switchKeyboard(if (currentKeyboardName == lock) "abc" else lock, currentKeyboardName == "abc")
"123%" -> switchKeyboard("123%")
"abc" -> switchKeyboard("abc")
lock -> switchKeyboard(if (currentKeyboardName == lock) "abc" else lock)
emojis -> switchKeyboard(emojis)
"→|" -> inputText('\t'.toString())
"⌫" -> handleBackspace()
"↵" -> inputText("\n")
"" -> inputText(" ")
else -> inputText(keyLabel)
}
}
// Set OnLongClick Listener
key.setOnLongClickListener {
val trimmedLabel = keyLabel.trim()
when (trimmedLabel) {
"⇧" -> Unit
"123%" -> Unit
"abc" -> Unit
lock -> Unit
emojis -> Unit
"⌫" -> clearText()
"↵" -> Unit
else -> switchKeyboard(if (currentKeyboardName == lock) "abc" else lock, true)
}
true
}
key.setPadding(0, 0, 0, 0)
// Calculate weight for each key based on its label length
val keyWeight = (2 + keyLabel.length.toFloat()) / totalWeight
val keyLayoutParams = LinearLayout.LayoutParams(0, 130, keyWeight)
keyLayoutParams.setMargins(-4, -4, -4, -4)
key.layoutParams = keyLayoutParams
rowLayout.addView(key)
}
baseLayout.addView(rowLayout)
}
return baseLayout
}
private fun inputText(text: String) {
val inputConnection = currentInputConnection
inputConnection?.commitText(text, 1)
if (nextKeyboard != null) {
switchKeyboard(nextKeyboard!!)
}
}
private fun handleBackspace() {
val inputConnection = currentInputConnection
val selectedText = inputConnection?.getSelectedText(0)
if (selectedText.isNullOrEmpty()) {
// No text is selected, so delete the character before the cursor
inputConnection?.deleteSurroundingText(1, 0)
} else {
// Text is selected, so delete the selection
inputConnection?.commitText("", 1)
}
}
private fun clearText() {
val inputConnection = currentInputConnection
inputConnection?.deleteSurroundingText(100, 0)
}
}