Simple Application of inputmethod for interception of QT underlying Input

Source: Internet
Author: User

I recently used screen saver in my project. I woke up the screen saver after pressing the button or touch screen. In my previous article, I wrote only the qwsserver class with Screen Saver. If we find the method to wake up with touch, if the finger is touched by an action button, the screen that the horse sees after the screen is restored is not the interface before the screen is screensaver. This cannot happen when the screen is awakened in this project, therefore, it is required that the touch screen events and Keyboard Events cannot be sent to the application window for implementation. For example, after I wake up the screen saver, the first sentence to enter the function execution is to send a signal, install the Event Filter on the app. The event filter filters filter out the events and install the time filter in the app:

1. First, reload the filter function, define a signal in the screen saver in the front, filter the triggered mouse or keyboard event when the program enters the screen saver, and then remove the filter. Because the qwsserver class is directly defined in QT and is a virtual base class, it must be reloaded before it can be used. It can be used only after the SAVE () and restore () functions in this class are reloaded, this part explains in the screen saver section that to implement the signal slot function, this class must be inherited in multiple ways. It inherits the qwsserver class and the qobject class before it can implement the function of the signal slot.

Bool mainwindos: eventfilter (qobject * target, qevent * event)
{

If (Event-> type () = qevent: mousebuttondblclick
| Event-> type () = qevent: mousebuttonpress
| Event-> type () = qevent: mousebuttonrelease
| Event-> type () = qevent: mousemove
| Event-> type () = qevent: mousetrackingchange | event-> type () = qevent: keypress
| Event-> type () = qevent: keyrelease)
{
Qdebug ("a keyboard or mouse event ");
Event-> ignore ();
Return true;
}
Else
Return qwidget: eventfilter (target, event );

}

Install the Event Filter on the qapplication object

Qapp-> removeeventfilter (this );

Because qapp is directed to the qapplication object.

 

You can also filter specific key values in the Event Filter.

Bool process_base: eventfilter (qobject * target, qevent * event)
{
// Qdebug ("Event Filter entered % d", I );
If (Event-> type () = qevent: keypress)

{
Qkeyevent * keyevent = static_cast <qkeyevent *> (event );
If (keyevent-> key () = QT: key_1)

{
Key1_state_flag = true;
Qdebug ("key_1 is pressed ");
Return true;
}
Else if (keyevent-> key () = QT: key_2)
{
Key2_state_flag = true;
Qdebug ("key_2 is pressd ");
Return true;
}
Else if (keyevent-> key () = QT: key_3)
{
Key3_state_flag = true;
Qdebug ("key_3 is pressed ");
Return true;
}

}
Else if (Event-> type () = qevent: keyrelease)
{
Qkeyevent * keyevent = static_cast <qkeyevent *> (event );
If (keyevent-> key () = QT: key_1 & key1_state_flag = true)

{
Key1_state_flag = false;
Qdebug ("key_1 is released ");
Return true;
}
Else if (keyevent-> key () = QT: key_2 & key2_state_flag = true)
{
Key2_state_flag = false;
Qdebug ("key_2 is released ");

Return true;
}
Else if (keyevent-> key () = QT: key_3 & key3_state_flag = true)
{
Key3_state_flag = false;
Qdebug ("key_3 is released ");

Return true;
}

}
Else
Return qwidget: eventfilter (target, event );
// Qdebug ("Event Filter out % d", I );
}

In theory, this triggered Screen Saver. After the event was passed to the application, I worried that it was not executed. I don't know why it didn't reach my goal.

2. I have considered other methods. I have reloaded the nitify () function and the qapplication class to implement the screen saver function. Because keyboard and touch screen events go through notify () however, in this case, we can modify the screen saver interval, and it is difficult or unfamiliar to overload functions.

3. When reading the help document of QT, I saw an inputmethod class, which contains a mouse and a keyboard concern. It took me a day to read the inputmethod class in the source code, in fact, two filter functions are overloaded.

Bool myinputmethod: Filter (const qpoint &, int state, int wheel)

Bool myinputmethod: Filter (INT Unicode, int keycode, int modifiers, bool ispress, bool autorepeat)

I don't know how to filter the functions, but I can see in the source code of QT that there is a mouse or keyboard event. Then I wake up the screen saver and then judge the return values of the two functions, then, the event is distributed to the window.

Source code:

void
QWSServer::sendKeyEvent(
int
unicode,
int
keycode, Qt::KeyboardModifiers modifiers,
2532                              bool
isPress, bool
autoRepeat)
2533 {
2534     qws_keyModifiers = modifiers;
2535   
2536     if
(isPress) {
2537         if
(keycode != Qt::Key_F34 && keycode != Qt::Key_F35)
2538             qwsServerPrivate->_q_screenSaverWake();
2539     }
2540   
2541 #ifndef QT_NO_QWS_INPUTMETHODS
2542   
2543     if
(!current_IM || !current_IM->filter(unicode, keycode, modifiers, isPress, autoRepeat))
2544         QWSServerPrivate::sendKeyEventUnfiltered(unicode, keycode, modifiers, isPress, autoRepeat);
2545 #else
2546     QWSServerPrivate::sendKeyEventUnfiltered(unicode, keycode, modifiers, isPress, autoRepeat);
2547 #endif
2548 }
2549   

We can see their execution sequence. Therefore, when reloading the two filter functions, you only need to set the flag and set the flag position when entering the screen saver, each mouse and keyboard event goes through two filters first, and the flag is used to determine whether the mouse or keyboard event is the first time after screen saver, so it is convenient to perform automatic shutdown here, in addition, only the keyboard mapped to QT can read the key value. When the unicoude parameter in the keyboard filter is printed, the ASCII code of the corresponding key value is displayed, in this way, the bottom layer of QT can easily obtain Keyboard Events and corresponding key values, saving the trouble of writing drivers.

 

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.