Implementation of QT standard keyboard input in Embedded Linux

Source: Internet
Author: User

Author: Liu Hongtao,Hua Qing vision embedded college lecturer.

When running QTE on an embedded platform, the keyboard is usually not a standard keyboard, but a general button for external expansion of the embedded device. There are two methods to implement the QTE keyboard input:

 

(1) Write a general key driver, then open up a QT thread to read the key value, and send the key value through the signal. The target that needs to receive the keyboard input, declare the slot function, and receive the keyboard signal.
(2) write the key driver into a standard keyboard driver to make the QTE feel like it is dealing with the standard keyboard.

The above two methods give features. Most of my projects use 1st methods, which is intuitive and easy to control. However, in some cases, 2nd methods are required.

It is easier to implement the 1st methods. The following describes the implementation process of the 2nd methods.

The process of entering a standard keyboard can be divided into two steps:

(1) Find a standard USB keyboard and test whether the QTE can correctly set a standard keyboard
(2) Write a key driver to simulate standard keyboard input.

Step 1:

● QTE configuration supports standard USB keyboard
When configuring the QTE library, add the following parameters supported by the keyboard:
./Configure ...... -QT-KBD-USB ......
● The configuration kernel supports USB keyboard input
● After the USB keyboard is inserted, an event device node is generated, such as/dev/event2.
● Set environment variables for the keyboard devices associated with QTE
Export qws_keyboard = USB:/dev/event2
● Compile a QT test code for receiving Keyboard Events.

Class mydialog: Public qdialog
{
......
Protected:
Virtual void keypressevent (qkeyevent * k );
};

Void mydialog: keypressevent (qkeyevent * K)
{
Qdebug ("in press event % x", K-> key ());
}

● Test keyboard input

When F1 ~ F12:
In press event 1000030
In press event 1000031
In press event 1000032
In press event 1000033
In press event 1000034
In press event 1000035
In press event 1000036
In press event 1000037
In press event 1000038
In press event 1000039
In press event generation 3A

Check qt help key_f1 = 0x1000030

It indicates that the keypressevent of qdialog can receive the keyboard signal it can obtain, that is, the connection between the QTE and the USB keyboard is correct.

2. Implementation Process in Step 1:

Refer to the/driver/USB/input/usbkbd. C program to complete keyboard simulation. The main idea of the program is to write an input device driver that supports ev_key. The key code is extracted below.

● Complete input device registration and cancellation

Struct input_dev * input_dev;
Static unsigned char usb_kbd_keycode [256] = {
0, 0, 0, 0, 30, 48, 46, 32, 18, 33, 34, 35, 23, 36, 37, 38,
50, 49, 24, 25, 16, 19, 31, 20, 22, 47, 17, 45, 21, 44, 2, 3,
4, 5, 6, 7, 8, 9, 10, 11, 28, 1, 14, 15, 57, 12, 13, 26,
27, 43, 43, 39, 40, 41, 51, 52, 53, 58, 59, 60, 61, 62, 63, 64,
65, 66, 67, 68, 87, 88, 99, 70,119,110,102,104,111,107,109,106,
105,108,103, 69, 98, 55, 74, 78, 96, 79, 80, 81, 75, 76, 77, 71,
72, 73, 82, 83, 86,127,116,117,183,184,185,186,187,188,189,190,
191,192,193,194,134,138,130,132,128,129,131,137,133,135,136,113,
115,114, 0, 0, 0,121, 0, 89, 93,124, 92, 94, 95, 0, 0, 0,
122,123, 90, 91, 85, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
29, 42, 56,125, 97, 54,100,126,164,166,165,163,161,115,114,113,
150,158,159,128,136,177,178,176,142,152,173,140
};

/* Initialize */

Static int button_init (void)
{
......
Input_dev = input_allocate_device (); // allocate the input device
Input_dev-> evbit [0] = bit (ev_key) | bit (ev_led) | bit (ev_rep );
// Ev_key is the keyboard event to be supported
For (I = 0; I <255; I ++)
Set_bit (usb_kbd_keycode [I], input_dev-> keybit );
// Set the supported keyboard code, which can be registered based on the actual situation
Input_register_device (input_dev); // register the input device

}

/* Cancel */

Static void _ exit button_cleanup (void)
{
......
Input_unregister_device (input_dev); // cancel the input device
}

● Complete keyboard value acquisition and input event submission during interrupt handling

Static irqreturn_t button_irq (int irq, void * dev_id, struct pt_regs * regs)
{
......
Input_report_key (input_dev, 59, 1); // simulate press the F1 key
Input_report_key (input_dev, 59, 0 );
Input_sync (input_dev );
}
/* In kernel include/Linux/input. h
# Define key_f1 59
*/

The above provides a brief process. For details about the implementation process, refer to the/driver/USB/input/usbkbd. c file and pay attention to the key shake and other issues.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.