Keypressed event from a button?

Is it possible to generate a keypressed event when clicking on an entity? (simulating that that key was really pressed), or is this something that can only be done from a real keyboard?
I know that it is possible to assign a function directly to the onclick event of the entity, but I want to know it if this is possible

This page suggests that you can: https://www.quora.com/In-JavaScript-how-can-I-simulate-a-keystroke

Not sure why you would want to though?

okay. Thank you. I get it. I have redesigned everything to solve it in another way so I will rephrase my question from a different topic