A new method for simulating the mouse on the keyboard in QT

Source: Internet
Author: User

 

During this time, I used the QT development project and encountered some problems. After several days of hard work, I finally got it done, so I wrote down the solution.
1. First, the problem of flickering transparent windows in front of the video
At the beginning, I used layout management to add four widgets to a dialog. However, after the dialog is set to transparent, the video frames are rerefreshed and re-painted. Therefore, when the widget is constructed with no parent object, it is regarded as a window and transparent, and the flickering problem is solved successfully.
2. Questions about keyboard mouse Simulation
I found some methods on the Internet, but mainly used eventfilter. Add the following code in it:
Case QT: key_w:
POs. sety (Pos. Y ()-10 );
Qcursor: setpos (POS );
Break;
Case QT: key_l:

Qmouseevent * mevnpress;
Qmouseevent * mevnrelease;
Mevnpress = new qmouseevent (qevent: mousebuttonpress, POs, QT: leftbutton, QT: nobutton, QT: nomodifier );
Mevnrelease = new qmouseevent (qevent: mousebuttonrelease, POs, QT: leftbutton, QT: nobutton, QT: nomodifier );
Qcoreapplication: postevent (OBJ, mevnpress );
Qcoreapplication: postevent (OBJ, mevnrelease );
Break;
However, this is not a good solution for me. I have to add the above code for each dialog and modify the code according to each dialog. In short, it is not universal.
Inspired by x11eventfilter, wineventfilter is found. Because the underlying messages all pass through this function, we can intercept Keyboard Events, and then post the corresponding mouse events in the postmessage. OK!
The Code is as follows:
Bool wineventfilter (MSG * MSG, long * result)
{
Qdebug ("OK ");
If (MSG-> message = wm_keydown)
{
If (MSG-> wparam = vk_f2) // intercept F2 keyboard messages
{

Qpoint Pos;
Pos = qcursor: pos ();
Point point;
Point. x = pos. X ();
Point. Y = pos. Y ();
Hwnd H = windowfrompoint (point); // obtain the window handle at the mouse position
Screentoclient (H, & Point); // convert the global coordinates of the mouse to the local coordinates of the relative window
// Send the right-click message
Postmessage (H, wm_rbuttondown, mk_lbutton, makelong (point. X, point. y); // right-click and press
Postmessage (H, wm_rbuttonup, mk_lbutton, makelong (point. X, point. y); // right-click to release

}
}

Return false;
}
Sometimes it is better to go deeper at the bottom layer.

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.