#Simulate a keyboard press in a cmd program in java

1 messages · Page 1 of 1 (latest)

grave vortex
#

Hi, I have an exe that my java application runs when I start it (ProcessBuilder). Normally, if you would just run the exe it would open a command prompt window where you can interact with it by pressing your keyboard. I want to press the 'r' key every 2 minutes to see some data about the program. how do I do that?

cobalt larkBOT
#

<@&987246399047479336> please have a look, thanks.

cobalt larkBOT
#

While you are waiting for getting help, here are some tips to improve your experience:

Code is much easier to read if posted with syntax highlighting and proper formatting.

If nobody is calling back, that usually means that your question was not well asked and hence nobody feels confident enough answering. Try to use your time to elaborate, provide details, context, more code, examples and maybe some screenshots. With enough info, someone knows the answer for sure.

Don't forget to close your thread using the command </help-thread close:1027500463647621170> when your question has been answered, thanks.

manic vessel
#

u have to understand how the program originally got hands on the input via cmd

#

it works via System.in and System.out of that program

#

which the CMD ties to its window

#

processbuilder gives u full access to those two streams

#

so u can feed it with data and also read what it responds

grave vortex
#

alright

manic vessel
#

its sth like process.getOutputStream() and process.getInputStream()

#

which u can wrap with BufferedReader and PrintWriter for example

grave vortex
#

and how do i simulate the keyboard press?

manic vessel
#

and then easily interact with it

#

well, it depends how the program did it

#

u can just literally write it text input

#

writer.print("r")

grave vortex
#

alright

grave vortex
#

like this?

#

i made a mistake; i need to press p, wait 5 seconds and then press r

#

and then wait 2 min

manic vessel
#

and similar

#

much easier to read, lol

grave vortex
#

oh

#

lmao

#

thanks 😄

grave vortex
manic vessel
#

well, it compiles

tame jay
manic vessel
#

it doesnt look "wrong"

#

see if it works

#

if not, its wrong

#

its for sure improvable. but u should first figure out whether the approach works or not

hasty abyss