Description
Custom keyboard commands
What MCU/Processor/Board and compiler are you using?
Esp32
What do you want to achieve?
I have created a custom keyboard using a character map. I would like to implement a backspace and if possible an OK button which will send the text on the assigned text area to the machine.
What have you tried so far?
I have used the custom character but unlike the character keys it does nothing. Id like the backspace key to delete the character on its left in case i need to correct my input. Ive also used the custom ok key and it does nothing to. I had consulted the documentarions but didnt find any. I hope i am not missing something.
Code to reproduce
I’m on the ipad now. Ill edit the code later when im on the desktop.
The code block(s) should be formatted like:
/*You code here*/
Screenshot and/or video
You can use a custom callback for the keyboard and check what character was pressed.
This is a snippet that I am using:
// bind a callback to the keyboard
lv_obj_set_event_cb(kb, cb_pkeyboard);
// custom keyboard callback
static void cb_keyboard(lv_obj_t * obj, lv_event_t event){
if(event == LV_EVENT_CLICKED){
// get the character that was pressed
const char * txt = lv_btnm_get_active_btn_text(obj);
// compare with the backspace button text
if(strcmp(txt, "<-")==0){
// assuming you are storing entered text in the `input_buffer`
if(strlen(input_buffer) > 0){
input_buffer[strlen(input_buffer)-1] = 0;
}
// compare with your OK text
}else if(strcmp(txt, "OK")==0){
// proceed to some action
}else{
input_buffer[strlen(input_buffer)] = txt[0];
}
// get a textarea object binded to the keyboard
lv_obj_t * ta = lv_kb_get_ta(obj);
// update the text threre
lv_ta_set_text(ta, input_buffer);
}
}
There could be a more elegant way without input_buffer
- using lv_ta_del_char(lv_obj_t *ta)
(effectively backspace) and lv_ta_add_char(lv_obj_t *ta, uint32_t c)
, but I didn’t test it:
static void cb_keyboard(lv_obj_t * obj, lv_event_t event){
if(event == LV_EVENT_CLICKED){
// get a textarea object binded to the keyboard
lv_obj_t * ta = lv_kb_get_ta(obj);
// get the character that was pressed
const char * txt = lv_btnm_get_active_btn_text(obj);
// compare with the backspace button text
if(strcmp(txt, "<-")==0){
lv_ta_del_char(ta);
// compare with your OK text
}else if(strcmp(txt, "OK")==0){
// proceed to some action
}else{
lv_ta_add_text(ta, txt);
}
}
}
3 Likes
I strongly suggest using the more elegant of the two approaches that @Stepan_Snigirev suggested. There is no need to have a separate buffer in addition to the text area.
I’m amazed. Big thanks @Stepan_Snigirev and @embeddedt.