How to detect keyboard input in lvgl simulator?

Description

What MCU/Processor/Board and compiler are you using?

lvgl simulator

What do you want to achieve?

Keyboard entry

What have you tried so far?

Code to reproduce

Add a code snippet which can run in the simulator. It should contain only the relevant code that compiles without errors when separated from your main code base.

The code block(s) should be formatted like:

int main()
{
    int ch;
    while (1)
    {
        if (_kbhit())//If a key is pressed, the ﹐ kbhit() function returns true
        {                  
            ch = _getch(); //Use the function ﹐ getch() to get the key value pressed
            std::cout << ch << std::endl;
            if (ch == 27)
                break;
        }
        Sleep(10); /*Just to let the system breath*/
    }
}

system:windows 10 1909
compiler:msvc+clang
I just want to check the input of the keyboard and finish the interface switching. I read ‘lv_drives/keyboard.h’. But I didn’t understand it. I hope I can provide a demo.thank you.

The keyboard driver in lv_drivers is designed to work with SDL. You would have to create a custom driver for Windows.

I haven’t tested this code snippet, but it should be a good starting point for your purposes.

bool keyboard_read(lv_indev_drv_t * indev_drv, lv_indev_data_t * data)
{
    (void) indev_drv;      /*Unused*/
    static uint32_t last_key;
    data->state = _kbhit() ? LV_INDEV_STATE_PR : LV_INDEV_STATE_REL;
    if(data->state == LV_INDEV_STATE_PR)
        last_key = _getch();
    data->key = last_key;
    return false;
}

. . .
    lv_group_t *g = lv_group_create();
    /* Add your objects to the group */
    lv_indev_drv_t real_kb_drv;
    lv_indev_drv_init(&real_kb_drv);
    real_kb_drv.type = LV_INDEV_TYPE_KEYPAD;
    real_kb_drv.read_cb = keyboard_read;
    lv_indev_t * real_kb_indev = lv_indev_drv_register(&real_kb_drv);
    lv_indev_set_group(real_kb_indev, g);
int main(int argc, char **argv)

{

    /*Initialize LittlevGL*/

    lv_init();

    /*Initialize the HAL (display, input devices, tick) for LittlevGL*/

    hal_init();


    //lv_group_t *g = lv_group_create();

    /* Add your objects to the group */

    lv_indev_drv_t real_kb_drv;

    keyboard_init();

    real_kb_drv.type = LV_INDEV_TYPE_KEYPAD;

    real_kb_drv.read_cb = keyboard_read;

    lv_indev_data_t kbDate;

    while (1)

    {

        std::cout << " is pressed?" << keyboard_read(&real_kb_drv, &kbDate) << " ";

        std::cout << "value: " << kbDate.key << std::endl;

        Sleep(10); /*Just to let the system breath*/

    }
 is pressed?0 value: 51
 is pressed?0 value: 51
 is pressed?0 value: 51
 is pressed?0 value: 52
 is pressed?0 value: 52
 is pressed?0 value: 52

This is valid. It just doesn’t check whether the keyboard is pressed or not.
But it’s not a big problem,It can also be realized with nowkeyval and lastkeyval variables

You shouldn’t call the keyboard_read function directly. It’s designed to be registered as a standard LittlevGL keypad driver, so that you can interact with it using standard keypad events or signals.

Tested by me _getch(); It is invalid.
Therefore, you must use the function in lv_driver ->keyboard.h->
bool keyboard_read(lv_indev_drv_t * indev_drv, lv_indev_data_t * data)to read the data.
I don’t know if it’s my msvc2019 + clang reason