I am trying to understand the low level processes involved in creating a KeyEvent for a pressed keyboard key in java.

  1. How and where does Java process real keystrokes?
  2. Where does a keystroke 'enter' the JVM?
  3. How does a keystroke become a KeyEvent?

My intention behind this is purely to understand the internal workings of java. As a bonus: Is the process for MouseEvents same, similar or completely different?


I've already spent some time digging source code. So this question partially aims to provide some entry points to the source and docs other than 'awt' (as awt itself is huge).

Maybe there are some exotic packages containing classes that form the JNI layer?

有帮助吗?

解决方案

How and where does Java process real keystrokes? Where does a keystroke 'enter' the JVM?

This depends on the operating system / desktop environment the JVM runs on. All modern OSes handle communication with hardware themselves, and do not (usually) allow programs direct hardware access (this used to be different on old systems like MS-DOS).

So the OS will accept keyboard input, and send it to the program that has focus. If that program is a JVM, the JVM will receive keyboard input via the usual, OS-specific mechanism, and then process it internally.

How does a keystroke become a KeyEvent?

When the JVM receives a keystroke, it will process it and decide what has to happen. For example, if a GUI component currently has input focus, the JVM will create a KeyEvent and send it to this component.


As Stephen C pointed out, if you want to dig deeper, you'll have to read the JDK source code. That is a large and complex piece of software, so expect to do some searching first.

A good starting point might be the page for the The Abstract Windowing Toolkit Group, as the AWT (Java's "old" GUI framwork, before Swing) still handles a lot of low-level details for GUI code.

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top