Win a copy of Kotlin in Action this week in the Kotlin forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

running a program until Ctrl + C is pressed or enter is pressed  RSS feed

 
Jacob Sonia
Ranch Hand
Posts: 183
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Please tell me how can i execute my loop until Ctrl + C or enter is pressed?
 
Jeanne Boyarsky
author & internet detective
Sheriff
Posts: 37222
519
Eclipse IDE Java VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ctrl-C normally interrupts a program. For enter, you would need to check if the user entered something on each loop.
 
Jacob Sonia
Ranch Hand
Posts: 183
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I still didn't get it, if the user entered something like a or b, then it won't be of much value to me, only enter or "Ctrl
+ c" would be of value to me, i just want to check if the user pressed enter or interrupted the program using Ctrl + c
 
fred rosenberger
lowercase baba
Bartender
Posts: 12542
48
Chrome Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The ctrl-c is likely to be swallowed by the OS, and kill the JVM. it probably won't work for your program.

You haven't said if this is a GUI or a command line. you need some kind of keyboard listener. Whenever a key is pressed, you need to check and see if it was the <enter> key. If not, continue your loop. if so, break out.
 
Andrew Monkhouse
author and jackaroo
Marshal Commander
Posts: 12142
255
C++ Firefox Browser IntelliJ IDE Java Mac Oracle
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
For ctrl-c you might want to look at Runtime's addShutdownHook.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!