0

In my program, I want to replicate the user pressing a key on the keyboard and mouse clicks.

Think about it like an auto-typer where the program types something in a word document over and over at various delays.

Any ideas on how to do this?

Thanks for your time.

mrg95
  • 2,371
  • 11
  • 46
  • 89

3 Answers3

0

For simulating key presses you can use the QKeyEvent class which describes a key event and post that using QCoreApplication::postEvent.

QKeyEvent *event = new QKeyEvent ( QEvent::KeyPress, Qt::Key_Enter);
qApp->postEvent (receiver, event)

You can set the global position of the mouse by

QCursor::setPos(100,100);

Simulating the mouse events are like:

QMouseEvent * event = new QMouseEvent ((QEvent::MouseButtonPress), QPoint(500,500),
    Qt::LeftButton,
    Qt::LeftButton,
    Qt::NoModifier   );

qApp->postEvent((QObject*)this,(QEvent *)event);
Nejat
  • 31,784
  • 12
  • 106
  • 138
0

I would suggest QTest namespace key and mouse simulation functions, e.g.

QTest::keyClick()
QTest::mouseClick()

You'll find documentation here

mkrasowski
  • 207
  • 1
  • 11
0

Look at my answer in the next topic. Answer has a link to OS depended implementation of Keyboard / Mouse event generation. It can be helpful for you.

Community
  • 1
  • 1
Kastaneda
  • 739
  • 1
  • 8
  • 15