0

I am trying to make a program that will run infinitely until I press a button. This program will run in the background so there is no display open at all times. while(!certainButtonIsPressed) { //Do Something } How do I make it so that while certainButtonIsPressed is valid while not making a KeyBoardListener class? Is this possible without a Key listener of some sort?

Thanks!

3
  • Do you really need your program to run in the background (detached from the console), or are you just looking to have it run and check for keypresses without using a UI? Commented Mar 27, 2014 at 3:02
  • Ideally I would like it to run in the background because I am using it as a prank so I would like the person not able to see the program running. Commented Mar 27, 2014 at 3:04
  • 1
    Then what @HovercraftFullOfEels says is true. This is hard to do in java since you'll need something that plugs into the OS keyboard event handling code. Reaching down into system internals is not something java is good at, by design. Commented Mar 27, 2014 at 3:09

1 Answer 1

2

You ask:

I am trying to make a program that will run infinitely until I press a button. This program will run in the background so there is no display open at all times. while(!certainButtonIsPressed) { //Do Something } How do I make it so that while certainButtonIsPressed is valid while not making a KeyBoardListener class? Is this possible without a Key listener of some sort?

So basically what you're trying to do is to trap all the keypresses of platform from a background process, and this is something that core Java cannot do without use of platform-specific native code that you provide, either through some library that you've obtained, or by meshing your Java program with some key-trapping utility. So if your question is, can this be done via just Core Java? And the answer is: no.

If you are looking for non-Java platform specific solutions, then you will need to give further details. My recommendation though is not to use Java for a task that can be performed much more easily and fully with another language, perhaps a scripting language such as AutoIt, if this were for a Windows environment.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.