views:

830

answers:

3

I am writing a little utill for me on ruby using qt. How to access global mouse movement and keyboard input events?

+2  A: 

Use QWidget::grabMouse() and note that its a very dangerous function, use it with care.

cartman
Why is it dangerous?
pez_dispenser
+3  A: 

If you need to track mouse movement when no buttons are clicked, you'll want to turn on mouse tracking on the widget(s) you wish to track the mouse on. The function QWidget::setMouseTracking(), available on all QWidgets, will let you do this.

To capture mouse movements, you'll need to capture QMouseMoveEvents. There are two ways to do this:

  • If you are defining your own widget, then reimplement QWidget::mouseMoveEvent().
  • If you are using a stock widget, then you can create an event filter class and install an event filter on the widget you'd like to track mouse movements. See QObject::installEventFilter().

For official Qt documentation, click the links on the functions of interest.

swongu
A: 

For mouse position, did you try QCursor::pos() ?

Ariya Hidayat