Java:发送和接收KeyEvent。VK_返回_空间结果到/来自串行
我的目标有点复杂,我不确定这是否可能。但我正在为我工作的一家公司制作终端程序
所以程序是这样构建的(至少是重要部分)。
我有一个JTextArea
正在使用JSSC
库接收从序列读取的字节。这个JTextArea
有一个键侦听器,它侦听一个按键,并使用SerialPort.writeInt(event.getKeyCode());
或.getKeyChar();
将每个按键作为int发送到序列。我不记得了,但我想它是getKeyChar();
。当我在JTextArea上按Backspace
时,假设我输入了reset halt
以将控制模块重置为出厂默认值并停止它。如果我按Backspace
2次,在实际的控制模块中,它会“退格”2个字符,但在JTextArea上,每次按Backspace
时,它仍会显示reset halt
加上(我想主要修复的部分)[K
。所以
- 我输入
reset halt
李> - 我按了两次
Backspace
李> - 我的JTextArea上的结果是
reset halt[K[K
李> - 实际控制模块上的结果(我看不到结果)是
reset ha
李>
我认为这可能是我正在使用的键码,所以当我按下Backspace
时,我做了一个if语句来更改通过串行发送的内容
if(event.getKeyCode() == KeyEvent.VK_BACK_SPACE){
SerialPort.writeInt(127); //Decimal for Delete
//And (not at the same time)
SerialPort.writeInt(8); //Decimal for Backspace (from ASCII code)
}
当然,我错了,问题依然存在
因此,我的问题是:
- 是否可以通过读取序列来“更新”正在“退格”到my
JTextArea
的内容李> - 如何防止讨厌的
[K
出现在我的控制台(JTextArea
)上李>
我不需要你为我做这项工作,只是简单地把我推向正确的方向
我很抱歉没有在这个问题上添加太多代码。出于“安全原因”,我在家和我的工作区没有互联网(令人震惊)。如果你需要更多的代码,请让我知道,我会尝试添加它,如果我可以强>
我还有一个附带问题,关于我想在控制台中添加/更改的内容(JTextArea
)。我不知道它的技术术语,但我想增加控制台上键入光标的宽度,使其更可见,而不是鼠标光标,而是当您单击文本字段时显示的闪烁光标。那个光标叫什么
# 1 楼答案
这里有一个例子
字符串是使用MyTestPanel的图形对象绘制的。绘图是在MyTestPanel的paintComponent()内部完成的
此面板也会检测到KeyEvents。因为这没有任何默认行为,所以您可以自由地对给定的输入执行您想要的操作
我还没有处理水平滚动和面板动态宽度
TestFrame2
MyTestPanel
输出屏幕截图