Java文本框KeyListener监听Ctrl组合键异常问题及修正请求
Alright, let's work through fixing your code and answering your question about the Java equivalent of C's fflush!
First, let's tackle the obvious syntax errors and output issues in your code:
1. Fix Syntax & Logic Errors
Your code has a couple of typos and minor logic gaps that are causing weird output:
charter chis a typo — it should bechar ch(Java's character type ischar, notcharter).- Your
keyEventClasshas an extra closing};at the end, which will cause a compile error. - When pressing Ctrl+non-printable characters (like Ctrl+Enter or Ctrl+Backspace), you'll get weird symbols instead of a readable description. We can fix that by checking if the character is printable.
2. Simplify with KeyAdapter (Optional but Cleaner)
Instead of implementing all three methods of KeyListener (most of which you're not using), you can extend KeyAdapter and only override the keyTyped method. This makes the code shorter and cleaner.
3. Java Equivalent of fflush(stdout)
In Java, System.out uses a buffered stream. By default, println() flushes the buffer automatically, but if you ever need to force a flush (like if you're using print() instead), you can call System.out.flush() — that's exactly the equivalent of C's fflush(stdout).
Corrected Code Example
Here's the fixed version using KeyAdapter (recommended):
// In UIclass.java jtextArea = new JTextArea(); jtextArea.addKeyListener(new KeyAdapter() { @Override public void keyTyped(KeyEvent ke) { char ch = ke.getKeyChar(); if (ke.isControlDown()) { // Handle printable vs non-printable Ctrl characters if (Character.isPrintable(ch)) { System.out.println("You Pressed : Ctrl+" + ch); } else { // Show a readable name for non-printable keys System.out.println("You Pressed : Ctrl+" + KeyEvent.getKeyText(ke.getKeyCode())); } } else if (ke.isAltDown()) { System.out.println("You pressed : Alt+" + ch); } else { System.out.println("You pressed : " + ch); } // Force flush the output buffer (like fflush in C) System.out.flush(); } });
If you prefer to keep your separate keyEventClass, here's the fixed version:
class KeyEventClass implements KeyListener { public KeyEventClass() {} @Override public void keyPressed(KeyEvent ke) {} @Override public void keyReleased(KeyEvent ke) {} @Override public void keyTyped(KeyEvent ke) { char ch = ke.getKeyChar(); // Fixed typo from 'charter' to 'char' if (ke.isControlDown()) { if (Character.isPrintable(ch)) { System.out.println("You Pressed : Ctrl+" + ch); } else { System.out.println("You Pressed : Ctrl+" + KeyEvent.getKeyText(ke.getKeyCode())); } } else if (ke.isAltDown()) { System.out.println("You pressed : Alt+" + ch); } else { System.out.println("You pressed : " + ch); } System.out.flush(); // Equivalent to fflush(stdout) } } // Then in UIclass.java: jtextArea = new JTextArea(); jtextArea.addKeyListener(new KeyEventClass());
This should fix your weird output issues and give you the flush functionality you need!
内容的提问来源于stack exchange,提问作者Harshad Khandagale

