how to Simulate the keyboard press

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

Moderator: andrew

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: 5694
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: 5694
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: 5694
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 Developers and Contributors”