-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathkeyboard.py
253 lines (207 loc) · 10.1 KB
/
keyboard.py
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
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
import sys
from os import path
import gi
gi.require_version("Gtk", "4.0")
from gi.repository import GLib, Gtk, Gdk, Gio
from characters import CHARS, find_mapped_char, char_location, BACKSPACE, ENTER, SHIFT, SPACE
from keybutton import KeyButtonSingleText, KeyButtonDualText
class KeyboardApp(Gtk.Application):
def __init__(self):
super().__init__(application_id="com.mrwormhole.virtual-keyboard")
GLib.set_application_name("Virtual Keyboard")
style_path: str = path.abspath(path.join(path.dirname(__file__), "data", "style.css"))
self.css_provider = Gtk.CssProvider()
self.css_provider.load_from_path(style_path)
Gtk.StyleContext.add_provider_for_display(Gdk.Display.get_default(), self.css_provider, Gtk.STYLE_PROVIDER_PRIORITY_APPLICATION)
self.textarea: Gtk.Entry = Gtk.Entry(hexpand=True) # Multi-line support for "↵ Enter" requires TextView+ScrollableWindow
self.textarea.add_css_class("input-box")
self.grid: Gtk.Grid = Gtk.Grid(row_spacing=5, column_spacing=5, focusable=False, column_homogeneous=True)
self.current_langauge: str = "TH"
self.enabled_shift: bool = False
def tint(self, char: str):
location = char_location(char)
if location is not None:
button = self.grid.get_child_at(location[1], location[0])
if button.has_css_class("keyboard-activating"):
button.remove_css_class("keyboard-activating")
else:
button.add_css_class("keyboard-activating")
def on_key_released(self, event_controller: Gtk.EventControllerKey, keyval: int, keycode: int, state: Gdk.ModifierType) -> bool:
if keyval == Gdk.KEY_Return:
self.tint(ENTER)
return True
if keyval == Gdk.KEY_BackSpace:
self.tint(BACKSPACE)
return True
if keyval == Gdk.KEY_space:
self.tint(SPACE)
return True
# lowercase it first so that UK mappings are used correctly
keyval = Gdk.keyval_to_lower(keyval)
char = chr(Gdk.keyval_to_unicode(keyval))
mapped_char = find_mapped_char(char, self.current_langauge)
if mapped_char != "":
self.tint(char)
return True
return False
def on_key_pressed(self, event_controller: Gtk.EventControllerKey, keyval: int, keycode: int, state: Gdk.ModifierType) -> bool:
if keyval == Gdk.KEY_Escape:
self.window.close()
return True
if keyval == Gdk.KEY_Return:
self.tint(ENTER)
return True
if keyval == Gdk.KEY_Shift_L or keyval == Gdk.KEY_Shift_R:
if self.current_langauge.endswith("_"):
self.current_langauge = self.current_langauge.rstrip("_")
else:
self.current_langauge = f"{self.current_langauge}_"
self.enabled_shift = not self.enabled_shift
self.generate_grid_buttons(self.current_langauge)
return True
cursor_position: int = self.textarea.get_position()
if keyval == Gdk.KEY_BackSpace and cursor_position != 0:
buff: Gtk.EntryBuffer = self.textarea.get_buffer()
buff.delete_text(cursor_position - 1, 1)
self.tint(BACKSPACE)
return True
if keyval == Gdk.KEY_space:
buff: Gtk.EntryBuffer = self.textarea.get_buffer()
buff.insert_text(cursor_position, " ", -1)
self.textarea.set_position(cursor_position + 1)
self.tint(SPACE)
return True
# lowercase it first so that UK mappings are used correctly
keyval = Gdk.keyval_to_lower(keyval)
char = chr(Gdk.keyval_to_unicode(keyval))
mapped_char = find_mapped_char(char, self.current_langauge)
if mapped_char != "":
buff: Gtk.EntryBuffer = self.textarea.get_buffer()
buff.insert_text(cursor_position, mapped_char, -1)
self.textarea.set_position(cursor_position + 1)
self.tint(char)
return True
return False
def generate_grid_buttons(self, language: str):
default_chars = CHARS["UK"]
for i in range(len(default_chars)):
for j in range(len(default_chars[i])):
widget: Gtk.Widget | None = self.grid.get_child_at(j, i)
if widget is not None:
self.grid.remove(widget)
char, mapped_char = default_chars[i][j], CHARS[language][i][j]
actionable_chars = [BACKSPACE, ENTER, SHIFT, SPACE]
btn: Gtk.Button = KeyButtonDualText(char, mapped_char)
if mapped_char in actionable_chars:
btn: Gtk.Button = KeyButtonSingleText(mapped_char)
btn.connect("clicked", self.on_keybutton_clicked, mapped_char)
if mapped_char == ENTER:
# enlarge the "enter" button
self.grid.attach(btn, j, i, 2, 1)
continue
elif mapped_char == SHIFT:
# adjust the style of "shift" button
if self.enabled_shift and not btn.has_css_class("keyboard-activating"):
btn.add_css_class("keyboard-activating")
if self.current_langauge.lstrip("_") != language.lstrip("_"):
# switching between languages should toggle off "shift" button
btn.remove_css_class("keyboard-activating")
self.grid.attach(btn, j, i, 1, 1)
def change_language(self, action: Gio.SimpleAction, param: GLib.VariantType):
action_name: str = action.get_name()
language: str = action_name.split("-")[1]
self.generate_grid_buttons(language)
self.current_langauge = language
self.enabled_shift = False
def on_quick_copy_button_clicked(self, _: Gtk.Button):
clipboard = Gdk.Display.get_default().get_clipboard()
clipboard.set(self.textarea.get_text())
def on_quick_delete_button_clicked(self, _: Gtk.Button):
buff: Gtk.EntryBuffer = self.textarea.get_buffer()
buff.delete_text(0, buff.get_length())
def on_keybutton_clicked(self, _: Gtk.Button, text: str):
if text == ENTER:
return
if text == SHIFT:
if self.current_langauge.endswith("_"):
self.current_langauge = self.current_langauge.rstrip("_")
else:
self.current_langauge = f"{self.current_langauge}_"
self.enabled_shift = not self.enabled_shift
self.generate_grid_buttons(self.current_langauge)
return
cursor_position: int = self.textarea.get_position()
if text == BACKSPACE and cursor_position == 0:
return
if text == BACKSPACE and cursor_position != 0:
buff: Gtk.EntryBuffer = self.textarea.get_buffer()
buff.delete_text(cursor_position - 1, 1)
return
if text == SPACE:
text = " "
# Append text and move cursor
buff: Gtk.EntryBuffer = self.textarea.get_buffer()
buff.insert_text(cursor_position, text, -1)
self.textarea.set_position(cursor_position + 1)
def do_activate(self):
self.window: Gtk.ApplicationWindow = Gtk.ApplicationWindow(application=self, title="Virtual Keyboard")
self.window.set_default_size(400, 300)
controller: Gtk.EventController = Gtk.EventControllerKey.new()
controller.connect("key-released", self.on_key_released)
controller.connect("key-pressed", self.on_key_pressed)
controller.set_propagation_phase(Gtk.PropagationPhase.CAPTURE)
self.window.add_controller(controller)
# Create header bar
header_bar = Gtk.HeaderBar()
self.window.set_titlebar(header_bar)
# Create 2 actions
action: Gio.SimpleAction = Gio.SimpleAction.new("select-TH", None)
action.connect("activate", self.change_language)
self.window.add_action(action)
action: Gio.SimpleAction = Gio.SimpleAction.new("select-TR", None)
action.connect("activate", self.change_language)
self.window.add_action(action)
# Create a new menu, containing actions
menu: Gio.Menu = Gio.Menu.new()
menu.append("Select Thai", "win.select-TH")
menu.append("Select Turkish", "win.select-TR")
# Create a popover button
popover = Gtk.PopoverMenu()
popover.set_menu_model(menu)
hamburger_button = Gtk.MenuButton()
hamburger_button.set_popover(popover)
hamburger_button.set_icon_name("open-menu-symbolic")
# Quick copy button
quick_copy_button = Gtk.Button(focusable=False)
quick_copy_button.connect("clicked", self.on_quick_copy_button_clicked)
quick_copy_button.set_icon_name("edit-copy")
# Quick delete button
quick_delete_button = Gtk.Button(focusable=False)
quick_delete_button.connect("clicked", self.on_quick_delete_button_clicked)
quick_delete_button.set_icon_name("edit-delete")
# Add menu buttons to the header bar
header_bar.pack_start(hamburger_button)
header_bar.pack_start(quick_copy_button)
header_bar.pack_start(quick_delete_button)
# Vbox to hold textarea and the buttons
container: Gtk.Box = Gtk.Box(orientation=Gtk.Orientation.VERTICAL, spacing=6)
container.set_margin_start(20)
container.set_margin_end(20)
container.set_margin_top(10)
container.set_margin_bottom(10)
self.window.set_child(container)
# Box to hold the textarea
textarea_container: Gtk.Box = Gtk.Box(orientation=Gtk.Orientation.HORIZONTAL)
textarea_container.append(self.textarea)
container.append(textarea_container)
# Grid to hold the buttons
container.append(self.grid)
self.generate_grid_buttons(self.current_langauge)
self.window.present()
if __name__ == "__main__":
app: KeyboardApp = KeyboardApp()
try:
exit_status = app.run(sys.argv)
sys.exit(exit_status)
except KeyboardInterrupt:
sys.exit(0)