Java:在 GUI 中需要获取输入行,仅在控制台中工作
我和我的朋友用 Java 编写了一个带有 GUI 的程序,专为没有键盘/鼠标的触摸屏终端而设计。一切都发生在 GUI 内,大部分输入都是按下按钮。我们想要连接一个磁卡读卡器(在键盘模式下)并从卡中读取数据,然后根据其中的数据进行处理。
我编写了一个在控制台中工作的类,但是当通过 GUI 运行时,它只是挂起,直到我按 alt+tab 并在 IDE 的(Eclipse 的)控制台内单击并刷卡。我正在寻找一种无需离开 GUI 即可获取此输入的方法。
MCR 的工作方式就像您只需在键盘上键入卡信息一样 — 它会发送一行包含两轨数据的任何位置,您可以在任何地方键入同一行、txt 文档、控制台等。
相关代码如下:
import java.util.Scanner;
public class CardRead {
public static void main()
{
String raw_card_data = "";
Scanner read = new Scanner( System.in );
System.out.println("Scan card"); // changed to an outputArea.setText for GUI
raw_card_data = read.nextLine(); // works in console, not within GUI
/* insert processing here */
}
}
在GUI中,代码很简单:
cardreadbtn.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e)
{
CardRead.main();
}
});
我想也许InputStream可以工作,但我从未真正使用过它们。我用谷歌搜索并找到了这个 线程: 我尝试集成有关 IOUtils 的建议,如下所示:
InputStream is = System.in;
StringWriter writer = new StringWriter();
IOUtils.copy(is, writer, null);
raw_card_data = writer.toString();
System.out.println(raw_card_data);
IOUtils.closeQuietly(is);
IOUtils.closeQuietly(writer);
我不知道我这样做是否正确,但在控制台中(尚未在 GUI 中尝试过),它现在显示“扫描卡”并且永远不会进行打印输出 raw_card_data 或执行其他操作。 “closeQuietly”我从这里得到:
...但是,我不知道我做得是否正确。也从未使用过 apache IOUtils。
所以我被困住了,我正在寻找你们。如何在不离开 GUI 的情况下获取该卡数据?
重要提示:卡片数据中有一个可变的空格数(我们需要保留它),因此像 Scanner.next() 这样的方法将不起作用。读卡器设置为发送一行,其中两个磁道均由分隔符分隔,然后是回车符,因此 .nextLine() 可以工作。卡数据也是可变的字节/字符数。
另外:在第一个代码块中, System.out.println 位于 raw_card_data = read.nextLine() 实际上所在的 do while 循环之前(我将其省略,因为我觉得它不相关,但现在我很好奇为什么它正在这样做)。当 println 更改为 setText() 以显示到 GUI 中的 JTextArea 时,直到在控制台中输入卡片数据之后,它才会显示,即使它发生在 do while 之前并且应该在 do while 之前执行。我不明白,哈哈。
My friends and I have written a program in Java with a GUI, made for touchscreen terminals with no keyboard/mouse. Everything occurs within the GUI, and most of the input is button presses. We want to attach a magnetic card reader (in keyboard mode) and read from a card, then perform processing based on the data from it.
I wrote a class that works in the console, but when run through the GUI it just hangs until I alt+tab and click inside the IDE's (Eclipse's) console and swipe the card. What I'm looking for is a way to get this input w/o having to leave the GUI.
The MCR works as if you simply typed the card info on a keyboard-- it'll send a line containing both tracks of data anywhere you could type the same line, txt document, console, whatever.
The relevant code is as follows:
import java.util.Scanner;
public class CardRead {
public static void main()
{
String raw_card_data = "";
Scanner read = new Scanner( System.in );
System.out.println("Scan card"); // changed to an outputArea.setText for GUI
raw_card_data = read.nextLine(); // works in console, not within GUI
/* insert processing here */
}
}
in the GUI, the code is simply:
cardreadbtn.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e)
{
CardRead.main();
}
});
I thought maybe an InputStream would work, but I've never really worked with them. I googled and found this
thread :
I tried integrating the suggestion about IOUtils, as follows:
InputStream is = System.in;
StringWriter writer = new StringWriter();
IOUtils.copy(is, writer, null);
raw_card_data = writer.toString();
System.out.println(raw_card_data);
IOUtils.closeQuietly(is);
IOUtils.closeQuietly(writer);
I don't know if I'm doing that right, but in the console (haven't tried in the GUI), it now says "Scan Card" and never progresses to print out raw_card_data or do anything else. "closeQuietly" I got from here:
...but again, I dunno if I'm doing it right. Never worked with apache IOUtils either.
So I'm stuck, and I'm looking to you guys. How can I grab this card data w/o leaving the GUI to do so?
Important note: The card data has a variable # of spaces in it (which we need to preserve), so anything like Scanner.next() won't work. The card reader is set up to send a line with both tracks separated by delimiters and then a carriage return, so .nextLine() works. The card data is also a variable # of bytes/characters.
Also: In the first code block, the System.out.println is before a do while loop that raw_card_data = read.nextLine() is actually in (I left it out because I felt it's not relevant, but now I'm curious why it's doing this). When the println is changed to a setText() to display to a JTextArea in the GUI, it doesn't display until AFTER the card data is input in the console, even though it occurs before the do while and should execute before it. I don't understand, lol.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
据我了解,Java 本身不允许您直接监视键盘的输入,除非在焦点窗口上。我认为有两种方法可以解决您的问题。第一种方法是向应用程序添加一个简单的按键侦听器,然后仅流式传输输入并进行处理。第二种是使用低级键盘钩子。
之前曾与尝试做类似事情的人进行过讨论:
Java 可以看到我键盘的活动吗?
从那里我能够要使用低级钩子实现来阅读本文: http://ksquared.de/blog/2011/07/java-global-system-hook/
From what I read, Java does not natively allow you to directly monitor the keyboard for input except on the focused Window. I can see two ways that you may be able to solve your problem. The first is by adding a simple key listener to your application and then just streaming the input and processing it. The second is to use a low level keyboard hook.
There was a previous discussion with someone trying to do something similar here:
Can Java see activity of my keyboard?
and from there I was able to get to this article with a low level hook implementation: http://ksquared.de/blog/2011/07/java-global-system-hook/
我编写了一个在控制台中工作的类,但是当通过 GUI 运行时,它只是挂起,直到我按 alt+tab 并在 IDE 的(Eclipse 的)控制台内单击并刷卡。
这让我怀疑你在 Eclipse 中运行代码。当您从 IDE 内部“测试”代码时,Eclipse 基本上会模拟控制台,这就是您必须在 IDE 中使用控制台视图的原因。
当您正常运行应用程序时(即不是从 IDE 中运行),真正的控制台应该在 UI 后面保持打开状态。我还没有亲自测试过在运行 Swing 应用程序时从控制台读取数据,但它应该可以工作。
I wrote a class that works in the console, but when run through the GUI it just hangs until I alt+tab and click inside the IDE's (Eclipse's) console and swipe the card.
This makes me suspect you're running the code inside of Eclipse. When you 'test' code from inside the IDE, Eclipse basically emulates the console, which is why you have to use the console view in the IDE.
When you run the application normally (ie, not from within the IDE), a real console should remain open behind the UI. I haven't personally tested reading from the console when running a Swing app, but it should work.