1

Is there a way how to block keyboard input in java. I would like to catch the input in the java code, but stop it from being send to OS.

Example: i have notepad opened and i can write just fine, but when i press a combination of keys java app catches that input, and now i should not be able to write with my keyboard. Is this kind of behaviour possible?

I know how to capture key presses but the keyboard blocking part is a mystery to me.

I tried googling it but i did not find any solution.

2 Answers2

2

You have to understand: java applications run with the JVM. A JVM isn't the operating system.

Therefore your ways to access resources belonging to the operating system are very limited.

In other words: there is no generic, cross plattform way of having a Java application being able to "intersect" arbitrary console user input for arbitrary other applications.

Imagine you are a person sitting in a bus - just a guy like everybody else. You have no authority to turn to fellow passengers and ask them for their passport or such things. You are just one guy in the bus, like everybody else. Same here: a Java application is lacking the means to control other processes.

As you are specifically asking about the Windows platform: there might be some options using JNI and specific native calls. See here for example.

So, to be precise: it is not possible in general, but depending on your operating system there might be ways, for example using JNI.

GhostCat
  • 137,827
  • 25
  • 176
  • 248
  • Oh thats the answer i did not wanted to hear :( . I know its other question but is it possible to do it in other languages ?? like maybye C# which in my guess should have better control over the windows system resources ?? – František Jeřábek Sep 08 '17 at 06:10
0

Yes, you can do this - I used this library: https://github.com/tulskiy/jkeymaster to successfully to register a "global" keyboard shortcut to open a window in my program that was running in the task tray.

You can't "stop" it from being sent to the OS, but you can register a keyboard shortcut, open your window, and give it focus, so that all other keystrokes go into that text box.

J. Dimeo
  • 262
  • 2
  • 10