how to Simulate the keyboard press

Discussion forum for C++ and script developers who are using the QCAD development platform or who are looking to contribute to QCAD (translations, documentation, etc).

Moderator: andrew

Forum rules

Always indicate your operating system and QCAD version.

Attach drawing files, scripts and screenshots.

Post one question per topic.

Post Reply
sarlaa
Active Member
Posts: 47
Joined: Mon Aug 28, 2017 4:39 pm

how to Simulate the keyboard press

Post by sarlaa » Mon Jul 09, 2018 7:03 pm

Hi,

I want to simulate the keyboard press, I do like this :

Code: Select all

var appWin = EAction.getMainWindow()
  var ev = new QKeyEvent(QEvent.KeyPress, Qt.Key_8 ,Qt.NoModifier)
  var input = this.getDock().findChild("edtPolLength");
  input.setFocus()
  QCoreApplication.postEvent(appWin, ev);
The input get focus but the key digit is not wrote.

Thanks.

User avatar
andrew
Site Admin
Posts: 9036
Joined: Fri Mar 30, 2007 6:07 am

Re: how to Simulate the keyboard press

Post by andrew » Mon Jul 09, 2018 7:50 pm

Perhaps the widget needs a key release event instead or press followed by release. Just a guess though, I haven't tried this.

sarlaa
Active Member
Posts: 47
Joined: Mon Aug 28, 2017 4:39 pm

Re: how to Simulate the keyboard press

Post by sarlaa » Tue Jul 10, 2018 1:15 pm

I tried your 2 propositions, still not working for me.

User avatar
andrew
Site Admin
Posts: 9036
Joined: Fri Mar 30, 2007 6:07 am

Re: how to Simulate the keyboard press

Post by andrew » Tue Jul 10, 2018 2:01 pm

Perhaps there is a better way instead of simulating a key press event? Why not just appending the desired character / text to the input of the line edit widget?

sarlaa
Active Member
Posts: 47
Joined: Mon Aug 28, 2017 4:39 pm

Re: how to Simulate the keyboard press

Post by sarlaa » Tue Jul 10, 2018 3:14 pm

This is what we have for now, but doing like this there is others scenarios than append, like replace with text when input text initial is selected ...
So for having the exact default behavior of input, why we serach to launch progamatically the key press.

User avatar
andrew
Site Admin
Posts: 9036
Joined: Fri Mar 30, 2007 6:07 am

Re: how to Simulate the keyboard press

Post by andrew » Tue Jul 10, 2018 3:33 pm

Try:
...
var ev = new QKeyEvent(QEvent.KeyPress, Qt.Key_8, Qt.NoModifier, "8");
var input = ...
QCoreApplication.postEvent(input, ev);

sarlaa
Active Member
Posts: 47
Joined: Mon Aug 28, 2017 4:39 pm

Re: how to Simulate the keyboard press

Post by sarlaa » Tue Jul 10, 2018 3:51 pm

Works perfectly thanks !

Post Reply

Return to “QCAD Programming, Script Programming and Contributing”