Page 1 of 1

how to Simulate the keyboard press

Posted: Mon Jul 09, 2018 7:03 pm
by sarlaa
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.

Re: how to Simulate the keyboard press

Posted: Mon Jul 09, 2018 7:50 pm
by andrew
Perhaps the widget needs a key release event instead or press followed by release. Just a guess though, I haven't tried this.

Re: how to Simulate the keyboard press

Posted: Tue Jul 10, 2018 1:15 pm
by sarlaa
I tried your 2 propositions, still not working for me.

Re: how to Simulate the keyboard press

Posted: Tue Jul 10, 2018 2:01 pm
by andrew
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?

Re: how to Simulate the keyboard press

Posted: Tue Jul 10, 2018 3:14 pm
by sarlaa
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.

Re: how to Simulate the keyboard press

Posted: Tue Jul 10, 2018 3:33 pm
by andrew
Try:
...
var ev = new QKeyEvent(QEvent.KeyPress, Qt.Key_8, Qt.NoModifier, "8");
var input = ...
QCoreApplication.postEvent(input, ev);

Re: how to Simulate the keyboard press

Posted: Tue Jul 10, 2018 3:51 pm
by sarlaa
Works perfectly thanks !