Keyobard get_active_btn_text()

Works when using lv_btnmatrix but not lv_keyboard.
Looking to capture the “Enter” key.

for lv_btnmatrix…

def kb_cb(obj, event):

	if event == lv.EVENT.CLICKED:
		txt = obj.get_active_btn_text()
		print("%s was pressed"%txt)

This should do the trick:

# Create a button with a label 

def kb_cb(obj, event):
	if event == lv.EVENT.CLICKED:
		txt = lv.btnmatrix.__cast__(obj).get_active_btn_text()
		print("%s was pressed"%txt)

kb = lv.keyboard(scr)

Thank you…
Would never of figure that it out in a million years!

In the future this would be solved without casting (on v8):

Now using v8.1…
trying …
obj = event.get_target()
txt = lv.btnmatrix.get_active_btn_text(obj)
print("%s was pressed"%txt)

AttributeError: type object ‘btnmatrix’ has no attribute ‘get_active_btn_text’

txt = lv.btnmatrix.get_btn_text(obj)
now get…
TypeError: argument has wrong type

help please !

scr = lv.scr_act()

def kb_cb(event):
    if event.get_code() == lv.EVENT.PRESSED: # v8 needs PRESSED not CLICKED
        btnm = lv.btnmatrix.__cast__(event.get_target())
        btn = btnm.get_selected_btn()
        txt = btnm.get_btn_text(btn)
        print("%s was pressed"%txt)

kb = lv.keyboard(scr)
kb.add_event_cb(kb_cb, lv.EVENT.ALL, None)

Online simulator example

Strangely this didn’t seem to work on the simulator… I still had to cast it manually to a btnmatrix, otherwise I got attribute errors.

One would think there would be a much simpler
way to get a keyboard key pressed.

I’ll give this a try…
Thank you again embeddedt


The weird casting is required because in this case get_target() returns an object of type lv_keyboard, and not lv_btnmatrix.

Although lv_keyboard inherits from lv_btnmatrix, it was decided that every child class should reimplement all its parent methods (except from lv_obj methods)

So, I would say this is a bug in LVGL API where lv_btnmatrix methods are not reimplemented in lv_keyboard.

@embeddedt / @kisvegabor - Do you agree?

Yes, I agree. The keyboard should have API function to get the pressed buttons.

I think we can simply add wrappers to lv_btnmatrix_get_selected_btn and lv_btnmatrix_get_btn_text.

Fixed on latest version.
Online example