File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes Beginning Java and the fly likes Monitor Keyboard event Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login

Win a copy of REST with Spring (video course) this week in the Spring forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Monitor Keyboard event" Watch "Monitor Keyboard event" New topic

Monitor Keyboard event

Andrew Ao

Joined: Sep 10, 2001
Posts: 16
i m looking for a way to monitor keyboard event in a text program - not GUI.
thank you,
Cindy Glass
"The Hood"

Joined: Sep 29, 2000
Posts: 8521
All java events are part of the AWT and are related to GUIs.
The best that you can hope for is to try and write some low level operating system dependant stuff. Perhaps you can "borrow" something from the event package and adapt it to what you want.
Of course an event has to be "against" something. Normally that is like a Frame or a TextField or a Button or something. So you are looking for a keyboard event against the Command Prompt?
I really have not a clue.
Andrew Ao

Joined: Sep 10, 2001
Posts: 16
as far as i know, jave doesn't seem to have a method of "monitoring keystrokes". i think i might need to use jni.
thank you,
Michael Ernest
High Plains Drifter

Joined: Oct 25, 2000
Posts: 7292

Hm, well you could extend an event structure, as Cindy mentioned, as save yourself the bother of going through JNI (which, by the way, won't help).
All you need to do is treat the class that gathers input from the keyboard as an event source. Being an event source means you register/deregister listeners, and on some change in your own state you construct an object that encapsulates the "event," and send it to your listeners in the form of a parameter.
If I get some time tonight, I'll write this out, but it's completely doable without a native construct.

Make visible what, without you, might perhaps never have been seen.
- Robert Bresson
Andrew Ao

Joined: Sep 10, 2001
Posts: 16
sorry, but maybe i didn't make it clear. let me provide some background info.
i am writing the front end of an ordertaking app using java. due to its usage and need for fast entry, gui is NOT recommended. the user will be using a keyboard to enter orders and traverse around the application.
i was looking around, and try to find a way to intercept user keystroke. but is a "blocking function." that's why i figure i might need to use native code to monitor keystrokes.
first, i should mention that my background is in the middle tier. so, i don't know much about UI. but here is what i am doing.
"recreate" text ui components using HMVC approach. monitor the keyboard input using native c code, and manage user's interaction using chain of command patterns.
if anyone has a better idea, please let me know. please drop a line at if you reply to this because this thread could get buried in javaranch.
It is sorta covered in the JavaRanch Style Guide.
subject: Monitor Keyboard event
It's not a secret anymore!