Ajinkya Jumbad
Ajinkya Jumbad

Reputation: 95

Capture Keystrokes when program is not active

I code in java. I wrote a keylistener for the frame and it prints all the keystrokes when the frame is active, but when i minimize it or deactivate it, the program obviously stops and no keystrokes are printed. I wanted to make a small game where i enter a key and using the robot class, it presses another set of keys but this game is in flash. any idea as to how i would capture keystrokes when window is deactivated.

edit: I only code in java so is it possible using only java or at most combining it with native machine... i use windows

Upvotes: 0

Views: 124

Answers (1)

asteri
asteri

Reputation: 11572

By its nature, Java is sandboxed by the JVM, so you will have to incorporate some kind of native methods. There already exists a very flexible and helpful library to accomplish this under open source, called JNativeHook. It's very easy to hook in, especially if you're already familiar with Swing event handlers. Same basic concept, except it leverages native code written in C. It supports all of the basic operating systems (Windows, Mac, *Nix).

Upvotes: 2

Related Questions