How to implement your own keyboard in QT embedded

Source: Internet
Author: User
Tags modifiers

The latest project is based on QT. Since it is an embedded device, it is inevitable to implement the keyboard driver part based on your own platform. Of course, it belongs to the QT layer rather than the underlying character device driver.

The method mentioned here is to integrate our own keyboard driver into our program without re-compiling the QT library. Of course, we can also directly compile our own keyboard driver into the QT library.

The plug-in mechanism (plugin) provided in QT makes it easy for us to implement our own keyboard code based on our own hardware.

Procedure:

1. Create a static plug-in Library (also a dynamic library). The Library name here is optkeypad.
A. create a keyboard processing class keypadhandler that inherits from qobject and qwskeyboardhandler and implements a key function readkpddata (). This function is critical to the hardware value (hardcode) convert to the key value defined in The QT library.
B. create a plug-in class keypaddriverplugin inherited from qkbddriverplugin. This class mainly creates a keyboard processing class keypadhandler and re-implements the CREATE () and keys () functions of qkbddriverplugin, these are two virtual functions. We need to implement these two functions again in our class.
C. We made a static plug-in class, so the. Pro file is as follows.
2. How can I use this plug-in class,
A. Add the macro q_import_plugin (optkeypad) in our application (generally in Main. cpp ). Optkeypad is the database name. Note that it does not contain the prefix lib and extension. A in Linux.
B. Add qtplugin + = optkeypad TO THE. Pro file of the application, and add the library libs + =-l (PATH)-loptkeypad to the link.
C. Add the environment variable qws_keyboard for the program. Export qws_keyboard = optimuskpdhandler:/dev/input/event1 (this is important)
Note how this optimuskpdhandler comes from. The keypaddriverplugin plug-in class should create a keypadhandler keyboard processing class based on this name. If this name does not match, the processing class is not created.
3. The next key value in our QT application is the value after the readkpddata () processing of the keypadhandler class.

. Pro File

Qt + = WebKit // This must be added; otherwise, many header files cannot be found, because QT will not automatically add the WebKit Module
Template = lib
Config + = static \
Plugin \
Release
Target = optkeypad
Headers + = keypadhandler. h \
Keypaddriverplugin. h
Sources + = keypadhandler. cpp \
Keypaddriverplugin. cpp

Qmake_incdir + = ../clockcommon
Qmake_libs + =-lclockcommon

Keypaddriverplugin. h file

/* Keypaddriverplugin is an interface class, qkbddriverplugin is an abstract class, And qwskeyboardhandler is the parent class of the handler class. Create, keys, and destructor are required */
Class keypaddriverplugin: Public qkbddriverplugin
{
Q_object
Public:
Keypaddriverplugin (qobject * parent = 0 );
~ Keypaddriverplugin ();

Qwskeyboardhandler * Create (const qstring & driver, const qstring & device );
Qwskeyboardhandler * Create (const qstring & driver );
Qstringlist keys () const;
};

Class implementation file

Keypaddriverplugin: keypaddriverplugin (qobject * parent)
{

}

Keypaddriverplugin ::~ Keypaddriverplugin ()
{
}

// Optimuskpdhandler indicates the specific plug-in name.
Qstringlist keypaddriverplugin: Keys () const
{
Return qstringlist () <"optimuskpdhandler ";
}

//
// The create () functions are responsible for returning an instance
// The keypad driver. We do this only if the driver parameter matches our key.
//
Qwskeyboardhandler * keypaddriverplugin: Create (const qstring & driver, const qstring & device)
{
Pr ("keypaddriverplugin :: create ###################################: % s \ n ", driver. tolocal8bit (). constdata ());
If (driver. tolower () = "optimuskpdhandler ")
{
Pr ("before creating keypadhandler \ n ");
Return new keypadhandler (device); // create an instance of the handler class
}

Return 0;
}

Qwskeyboardhandler * keypaddriverplugin: Create (const qstring & driver)
{
If (driver. tolower () = "optimuskpdhandler ")
{
Pr ("before creating keypadhandler ");
Return new keypadhandler ();
}

Return 0;
}

// Keypadhandler. h file
Class keypadhandler: Public qobject, public qwskeyboardhandler
{
Q_object

Public:
Keypadhandler (const qstring & device = qstring ("/dev/input/event0 "));
~ Keypadhandler ();

PRIVATE:
Qsocketnotifier * m_notifier; // used to check the status of the keyboard Device File
Int kbdfd; // kbdfd is used to save the descriptor after the keyboard device file is opened.

Private slots:
Void readkpddata ();
};

// Implementation file of the keypadhandler class
Struct inputdata
{
Unsigned int dummy1;
Unsigned int dummy2;
Unsigned short type;
Unsigned short code;
Unsigned int value;
Unsigned int dummy3;
Unsigned int dummy4;
Unsigned int dummy5;
Unsigned int dummy6;
};

Keypadhandler: keypadhandler (const qstring & device)
{
Setobjectname ("Optimus keypad handler ");
This-> kbdfd =: open (device. tolocal8bit (). constdata (), o_rdonly, 0 );

If (kbdfd> 0)
{
Pr ("% s opened as keyboard input. \ n", device. tolocal8bit (). constdata ());
G_log.debugf (log_detail_trace, 0, l "% s opened as keyboard input.", device. tolocal8bit (). constdata ());
This-> m_notifier = new qsocketnotifier (kbdfd, qsocketnotifier: read, this );
Connect (this-> m_notifier, signal (activated (INT), this, slot (readkpddata ()));
}
Else
{
Pr ("cannot open % s for keyboard input. (% s )",
Device. tolocal8bit (). constdata (), strerror (errno ));
G_log.errorf (log_detail_important, 0, l "cannot open % s for keyboard input. (% s )",
Device. tolocal8bit (). constdata (), strerror (errno ));
Return;
}
}

Keypadhandler ::~ Keypadhandler ()
{
If (kbdfd> 0)
: Close (kbdfd );
}

// Key function
Void keypadhandler: readkpddata ()
{
Inputdata event;

Int n = read (kbdfd, & event, sizeof (inputdata ));
If (n! = Sizeof (inputdata ))
{
Pr ("Key pressed: N = % d \ n", N );
G_log.debugf (log_detail_trace, 0, l "Key pressed: N = % d", N );
Return;
}

Pr ("Key pressed: TYPE = % d, code = 0x % x, value = % d, % s \ n ",
Event. type, event. Code, event. Value, (event. value! = 0 )? "(Down)": "(up )");
G_log.debugf (log_detail_trace, 0, l "Key pressed: TYPE = % d, code = % d, value = % d, % s ",
Event. type, event. Code, event. Value, (event. value! = 0 )? "(Down)": "(up )");

Qt: keyboardmodifiers modifiers = QT: nomodifier;
Int Unicode = 0 xFFFF;
Int key_code = 0;

// You can set it based on your specific hardware value.
Switch (event. Code)
{
Case 0x2:
Key_code = QT: key_1;
Unicode = '1 ';
Break;
Case 0x110:
Key_code = QT: key_context1;
Unicode = 0 xFFFF;
Break;
Case 0x100:
Key_code = QT: key_back;
Unicode = 0 xFFFF;
Break;
Default:

Break;
}

This-> processkeyevent (UNICODE, key_code, modifiers, event. value! = 0, false );
}

Note: The red part is added for me, but it is not the original text of the author. If any error occurs, Please modify it.

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.