1

I would like to catch a KeyBoardInterrupt by a User (CTRL + C) in Java in order to aboard a password input from a user in Java in the Console. In Python there exists the easy possibility to just catch a KeyBoardInterrupt (as an exception). As I know there is nothing equivalent in Java but I would really dislike to use a shutdownhook (which were the suggestions I read in other threads) as this would destroy the Main-Thread anyway and make the whole project 10,000 times more difficult.

This means I would rather find a solution like an Exception I can catch or at least a Code-Workaround around the Use of the Java-Shutdownhook...

I would really love a solution which is still system-independent but the most I need it on Linux.

Thanks in advance

Daniel Pryden
  • 59,486
  • 16
  • 97
  • 135
Cedric
  • 408
  • 1
  • 4
  • 18
  • I wanted a solution without a shutdown hook to keep the main thread alive. This is why I don't consider it a duplicate – Cedric Nov 30 '18 at 06:03
  • This question is too broad without specifying a platform. Java does not provide a platform-independent way to do this, so you'll have to use one or more platform-dependent ways. (Or a third-party library, but questions asking for third-party libraries are off-topic on StackOverflow.) – Robin Green Dec 02 '18 at 20:45
  • I edited it now, but I would like to have this in Linux but a systemindependent solution I would of course appreciate more... Anyways: thanks in advance for any solution you can provide – Cedric Dec 03 '18 at 12:38
  • 2
    I removed a few tags I think were irrelevant and added Linux (since this appears platform dependent, you may need to write separate question for other platforms). You could add trivial sample code, which uses shutDownhook (or perhaps nothing, just comments), as it's easier to get answers to questions where you have sample code for the answerer to work from, even if it is trivial. – hyde Dec 03 '18 at 12:45

0 Answers0