Kivy Virtual Keyboard very slow on RaspberryPi 3B+

57 views
Skip to first unread message

Piyush

unread,
Jan 13, 2020, 8:46:02 AM1/13/20
to Kivy users support
Dear KivyGurus,
Would appreciate if anyone can have any pointers on this one. We have coded Kivy's Virtual Keyboard with Python 3.7,  on our Raspberry Pi 3B+ but it is very slow. In comparison if we install something like Matchbox or florence keyboard on Raspbian desktop, they work fluently. The CPU is may be around 50% utilized and the 1 GB RAM is also sufficiently free. 
It has been slow right from the moment we started using it.
Is there any specific setting we need to be aware of ? I apologize in advance if the information I share is not enough but do let me know and I'll share as much as possible.  we are not doing anything fancy but using but using these 4 functions (took them from good Samaritans on stackoverflow i think). 
from kivy.uix.vkeyboard import VKeyboard

self._keyboard = Window.request_keyboard(self._keyboard_closed, self, 'text')
if self._keyboard.widget:
self.vkeyboard = self._keyboard.widget
self.vkeyboard.background_color = [0.8, 0.82, 0.83, 1]
self.vkeyboard.font_size=20
self.vkeyboard.layout = 'keyboard.json'
self.vkeyboard.text_color = [0, 0, 0.83, 1]


def _keyboard_closed(self):
print('My keyboard have been closed!')
self._keyboard.unbind(on_key_down=self._on_keyboard_down)
self._keyboard = None

def _on_keyboard_down(self, keyboard, keycode, text, modifiers):
print('The key', keycode, 'have been pressed')
print(' - text is %r' % text)
print(' - modifiers are %r' % modifiers)
if keycode[1] == 'escape':
keyboard.release()


def process_key_on(self, touch):
if not touch:
return
x, y = self.to_local(*touch.pos)
key = self.get_key_at_pos(x, y)
if not key:
return

key_data = key[0]
displayed_char, internal, special_char, size = key_data
line_nb, key_index = key[1]

# save pressed key on the touch
ud = touch.ud[self.uid] = {}
ud['key'] = key

# for caps lock or shift only:
uid = touch.uid
if special_char is not None:
# Do not repeat special keys
if special_char in ('capslock', 'shift', 'layout', 'special'):
if self._start_repeat_key_ev is not None:
self._start_repeat_key_ev.cancel()
self._start_repeat_key_ev = None
self.repeat_touch = None
if special_char == 'capslock':
self.have_shift = False
self.have_special = False
self.have_capslock = not self.have_capslock
uid = -1
elif special_char == 'shift':
self.have_capslock = False
self.have_special = False
self.have_shift = not self.have_shift
uid = -1
elif special_char == 'special':
#self.have_special = True
self.have_capslock = False
self.have_shift = False
self.have_special = not self.have_special
uid = -1
elif special_char == 'layout':
self.change_layout()

# send info to the bus
b_keycode = special_char
b_modifiers = self._get_modifiers()
if self.get_parent_window().__class__.__module__ == \
'kivy.core.window.window_sdl2' and internal:
self.dispatch('on_textinput', internal)
else:
self.dispatch('on_key_down', b_keycode, internal, b_modifiers)

if special_char is not None and special_char != 'shift' and self.have_shift:
self.have_shift = False
self.active_keys = []
# elif special_char is not None and special_char != 'special' and self.have_special:
# self.have_special = False
# self.active_keys = []
else:
# save key as an active key for drawing
self.active_keys[uid] = key[1]

self.refresh_active_keys_layer()

def process_key_up(self, touch):
uid = touch.uid
if self.uid not in touch.ud:
return

# save pressed key on the touch
key_data, key = touch.ud[self.uid]['key']
displayed_char, internal, special_char, size = key_data

# send info to the bus
b_keycode = special_char
b_modifiers = self._get_modifiers()
self.dispatch('on_key_up', b_keycode, internal, b_modifiers)

if special_char in ('shift', 'capslock','special'):
uid = -1

if uid in self.active_keys:
self.active_keys.pop(uid, None)
if special_char == 'shift' and self.have_shift:
self.active_keys[-1] = key
elif special_char == 'special' and self.have_special:
#self.have_special = False
self.active_keys[-1] = key
if special_char == 'capslock' and self.have_capslock:
self.active_keys[-1] = key
self.refresh_active_keys_layer()

VKeyboard.process_key_on = process_key_on
VKeyboard.process_key_up = process_key_up
Reply all
Reply to author
Forward
0 new messages