问题描述
我正在JavaFX中使用Java 8和进行项目。我想检测在 TextField 中按<退格的时间。这是我正在使用的代码:
I am doing a project in JavaFX, using Java 8 and Gluon scenebuilder. I want to detect when backspace is pressed inside a TextField. This is the code I am using:
public void keyPressed(KeyEvent kEvent) { if (kEvent.getCode() == KeyCode.BACK_SPACE) { System.out.println("Backspace pressed"); }
此代码位于控制器文件中,名为 FXMLDocumentController ,它控制GUI xml文件 FXMLDocument 。您可以从下图中看到,只要在 TextField 中输入密钥,就会调用该函数。这适用于所有字母/数字,但不适用于退格。
This code is inside the controller file, called FXMLDocumentController, which controls the GUI xml file FXMLDocument. You can see from the image below the function is called whenever a key is typed inside TextField. This works with all the letters/numbers, but not with backspace.
理论上它应该可以工作,但它没有。
Theoretically it should work, but it doesn't.
如何管理退格按钮的输入?
编辑:
请注意,在窗口本身(即<$ c)上将这个确切的函数放在元素的根上$ c> AnchorPane )有效。问题是在 TextField 中读取退格的按下。您可以在下图中看到我放置功能的位置:
Notice that putting this exact function on the root of the elements, on the "Window itself" (which is the AnchorPane) works. The problem is with reading the pressing of the backspace inside the TextField. You can see in the image below where I've put the function:
推荐答案
你应该使用你的<$ c按键上的$ c> keyPressed 方法或上的。
You should use your keyPressed method in either on key pressed or on key released.
在,它声明:
考虑操作键。
这篇关于JavaFx中BACKSPACE的键码事件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!