Inject a keystroke in java

妖精的绣舞 提交于 2020-01-14 10:44:12

问题


I'm looking for a way to inject a keystroke into the OS keyboard input buffer, like when you click a button the program inserts one (or more) keyboard strokes. I wanted to do this in java because I want to run this in (win,linux and osx). I guess that I'll have to make use of the JNI, do anyone have some ideas?

Thanks all stackoverflowers ;)


回答1:


My guess is that the java.awt.Robot class will do this for you:

new Robot().keyPress(...);

http://download.oracle.com/javase/6/docs/api/java/awt/Robot.html#keyPress(int)

java.awt.Robot "is used to generate native system input events for the purposes of test automation, self-running demos, and other applications where control of the mouse and keyboard is needed."




回答2:


Check java Robot . I believe this is what you are looking for.

Also check this out. Example



来源:https://stackoverflow.com/questions/3585000/inject-a-keystroke-in-java

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!