英文:
how do I get the character and process it in this getch class from https://stackoverflow.com/questions/1864076/equivalent-function-to-cs-getch-in-java
问题
这是我使用的 C++ 代码,然后链接中提供的代码,我渴望实现它,但是在我当前使用的程序中没有看到它在哪里返回一个字符:
x = _getch(); //在不等待按下回车键的情况下获取输入
// 在输入中断线程后
interrupted.store(true);
我使用用户输入来中断我的 C++ 线程,所以我将 x 赋予字符值以进行输入处理... 我正在寻找相同的功能...
如何在这个链接提供的 Java 代码中实现相同的功能:
import java.awt.event.KeyListener;
import java.awt.event.KeyEvent;
import javax.swing.*;
class getch {
public static void getCh() {
final JFrame frame = new JFrame();
synchronized(frame) {
frame.setUndecorated(true);
frame.getRootPane().setWindowDecorationStyle(JRootPane.FRAME);
frame.addKeyListener(new KeyListener() {
public void keyPressed(KeyEvent e) {
synchronized(frame) {
frame.setVisible(false);
frame.dispose();
frame.notify();
}
}
public void keyReleased(KeyEvent e) {
}
public void keyTyped(KeyEvent e) {
}
});
frame.setVisible(true);
try {
frame.wait();
} catch(InterruptedException e1) {
}
}
}
}
似乎没有返回值,我很难看出代码在哪里“获取和存储”字符。另外,在我的 Java 主程序中如何调用这个函数,因为我正在将这个程序转换为 Java?
英文:
Here is the code C++ i use and then that is provided in the link, and I am eager to implement it but do not see where it returns a character in my current program I use:
x = _getch(); //take input without waiting for an enter key pressed
// upon input interrupt thread
interrupted.store(true);
I use the user input it to interrupt my thread in C++, and so I assign x the char value for input processing .. I am looking for the same functionality..
how do I do that with this java code from the link:
import java.awt.event.KeyListener;
import java.awt.event.KeyEvent;
import javax.swing.*;
class getch
{
public static void getCh()
{
final JFrame frame = new JFrame();
synchronized(frame)
{
frame.setUndecorated(true);
frame.getRootPane().setWindowDecorationStyle(JRootPane.FRAME);
frame.addKeyListener(new KeyListener()
{
public void keyPressed(KeyEvent e)
{
synchronized(frame)
{
frame.setVisible(false);
frame.dispose();
frame.notify();
}
}
public void keyReleased(KeyEvent e)
{
}
public void keyTyped(KeyEvent e)
{
}
});
frame.setVisible(true);
try
{
frame.wait();
}
catch(InterruptedException e1)
{
}
}
}
}
It seems there is no return and I am having trouble seeing where the code is "grabbing and storing" the character. Also how am I calling this in my main in java as I am converting this program?
专注分享java语言的经验与见解,让所有开发者获益!
评论