JavaFx中BACKSPACE的键码事件 [英] Keycode event for BACKSPACE in JavaFx

查看:246
本文介绍了JavaFx中BACKSPACE的键码事件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在JavaFX中使用Java 8和 Gluon场景构建器进行项目。我想检测在 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 方法<+ c $ c>或上的

You should use your keyPressed method in either on key pressed or on key released.

文档,它声明:


没有为不生成的密钥生成密钥类型事件Unicode
字符(例如,操作键,修饰键等)。

No key typed events are generated for keys that don't generate Unicode characters (e.g., action keys, modifier keys, etc.).

Backspace 考虑操作键

这篇关于JavaFx中BACKSPACE的键码事件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆