0

I am trying to figure out the proper way to handle this. Basically what I want is when a user presses the c key, I want javascript to simulate that after the c key is pressed, a string of other keys are also pressed. So for example the user presses the c key, and then typed would be cool@gmail.com. c being the key they pressed, and ool@gmail.com being keys that were pressed via javascript.

What would be the best way to do this?

CodingIsFun33
  • 433
  • 3
  • 14
  • 1
    Possible [XY problem](http://xyproblem.info/). Do you actually want to simulate keypresses, or do you just want to add autocomplete functionality to a text field or something? Also, please provide your code so far. – Lionel Rowe Sep 14 '20 at 17:44
  • I need to actually simulate keypresses in this specific case. There is no code so far, I am not sure what I would need to simulate the keypresses. I simply have and am trying to figure out what I need to simulate the keys. I know JS well, have just never messed with keypresses like this. – CodingIsFun33 Sep 14 '20 at 17:46
  • Also how is this XY problem, I am literally saying what I am trying to do. Just not sure how its done. – CodingIsFun33 Sep 14 '20 at 17:47
  • 1
    Follow-up question: _why_ do you need to simulate keypresses? What's your actual use case? – Lionel Rowe Sep 14 '20 at 17:47
  • @LionelRowe this is a pretty un-usual use case, lets just put it that way. What I am trying to do is clear tho. – CodingIsFun33 Sep 14 '20 at 17:48
  • 1
    @CodingIsFun33 It's a *possible* XY problem because it's rare that a script *must* simulate key presses. I can tell you that you can't fully simulate a user's key presses because user-initiated events are flagged as trusted, whereas script-initiated events are not, and cannot, be so flagged. – Heretic Monkey Sep 14 '20 at 17:58
  • I am going to give that jquery suggestion a try, if that does not work, I may have to come up with a different way of handling this task it seems. – CodingIsFun33 Sep 14 '20 at 18:05

0 Answers0