如何在 JavaFX 中检测不同语言键盘上的加号键
How to detect PLUS key on different language keyboards in JavaFX
我正在尝试检测何时按下加号键,在我的英国键盘上该键位于 EQUALS 键上,但在我的德语键盘上位于另一个键上。我希望能够在用户不按 SHIFT 的情况下检测到加号键,以防加号通常通过 shift+另一个键到达。
这是正常缩放操作在例如浏览器中的工作方式。我如何在 JavaFX 中执行此操作,而无需列出每个键盘和对应的加号所在的键?
例如,下面的程序(检查 KeyCode.PLUS)在我的英国键盘上不起作用。我正在使用 JDK 1.8.0_40.
import javafx.application.Application;
import javafx.scene.Scene;
import javafx.scene.input.KeyCode;
import javafx.scene.input.KeyEvent;
import javafx.scene.layout.Pane;
import javafx.stage.Stage;
public class Test extends Application {
public static void main(String[] args) {
launch(args);
}
@Override
public void start(Stage stage) throws Exception {
Scene scene = new Scene(new Pane());
scene.addEventFilter(KeyEvent.KEY_PRESSED, e -> {
if (e.getCode() == KeyCode.PLUS) {
System.out.println("YES");
}
});
stage.setScene(scene);
stage.show();
}
}
我测试了你的程序,工作组合是为了测试:
KeyCode.ADD
:对应小键盘上的+
键;
KeyCode.EQUALS
:这对应于 =
键,因为您不希望用户按下 Shift
。
KeyCode.PLUS
:这对应于大写锁定时的=
键。
public class Test extends Application {
public static void main(String[] args) {
launch(args);
}
@Override
public void start(Stage stage) throws Exception {
Scene scene = new Scene(new Pane());
scene.addEventFilter(KeyEvent.KEY_PRESSED, e -> {
if (e.getCode() == KeyCode.ADD || e.getCode() == KeyCode.EQUALS || e.getCode() == KeyCode.PLUS) {
System.out.println("YES");
}
});
stage.setScene(scene);
stage.show();
}
}
如果您希望它适用于所有平台和键盘布局,您需要原始字符和 KeyEvent.KEY_PRESSED 过滤器。来自 KeyEvent 文档:
"Key typed" events are higher-level and generally do not depend on the
platform or keyboard layout. They are generated when a Unicode
character is entered, and are the preferred way to find out about
character input. In the simplest case, a key typed event is produced
by a single key press (e.g., 'a').
但要注意:
Often, however, characters are produced by series of key presses
(e.g., SHIFT + 'a'), and the mapping from key pressed events to key
typed events may be many-to-one or many-to-many.
混合使用它们会得到非常丑陋且难以阅读的代码。
因此您的示例应如下所示:
更新
我已将事件更改为 KEY_PRESSED 并使您的示例 (Ctrl + '+') 尝试工作,即使代码相等或字符相等。在大多数情况下,它适用于代码,但有时 charCombo 适合。
我已经用英语(英国)、法语、德语、西班牙语键盘布局尝试了以下代码并且有效。
import javafx.application.Application;
import javafx.event.EventHandler;
import javafx.scene.Scene;
import javafx.scene.input.KeyCharacterCombination;
import javafx.scene.input.KeyCode;
import javafx.scene.input.KeyCodeCombination;
import javafx.scene.input.KeyCombination;
import javafx.scene.input.KeyEvent;
import javafx.scene.layout.Pane;
import javafx.stage.Stage;
public class Test extends Application {
public static void main(String[] args) {
launch(args);
}
@Override
public void start(Stage stage) throws Exception {
Scene scene = new Scene(new Pane(), 300, 300);
scene.setOnKeyPressed(new EventHandler<KeyEvent>() {
KeyCombination charCombo = new KeyCharacterCombination("+", KeyCombination.CONTROL_DOWN);
KeyCombination codeCombo = new KeyCodeCombination(KeyCode.PLUS, KeyCombination.CONTROL_DOWN);
@Override
public void handle(KeyEvent event) {
if (charCombo.match(event) || codeCombo.match(event)) {
System.out.println("Zoom in");
}
}
});
stage.setScene(scene);
stage.show();
}
}
我正在尝试检测何时按下加号键,在我的英国键盘上该键位于 EQUALS 键上,但在我的德语键盘上位于另一个键上。我希望能够在用户不按 SHIFT 的情况下检测到加号键,以防加号通常通过 shift+另一个键到达。
这是正常缩放操作在例如浏览器中的工作方式。我如何在 JavaFX 中执行此操作,而无需列出每个键盘和对应的加号所在的键?
例如,下面的程序(检查 KeyCode.PLUS)在我的英国键盘上不起作用。我正在使用 JDK 1.8.0_40.
import javafx.application.Application;
import javafx.scene.Scene;
import javafx.scene.input.KeyCode;
import javafx.scene.input.KeyEvent;
import javafx.scene.layout.Pane;
import javafx.stage.Stage;
public class Test extends Application {
public static void main(String[] args) {
launch(args);
}
@Override
public void start(Stage stage) throws Exception {
Scene scene = new Scene(new Pane());
scene.addEventFilter(KeyEvent.KEY_PRESSED, e -> {
if (e.getCode() == KeyCode.PLUS) {
System.out.println("YES");
}
});
stage.setScene(scene);
stage.show();
}
}
我测试了你的程序,工作组合是为了测试:
KeyCode.ADD
:对应小键盘上的+
键;KeyCode.EQUALS
:这对应于=
键,因为您不希望用户按下Shift
。KeyCode.PLUS
:这对应于大写锁定时的=
键。
public class Test extends Application {
public static void main(String[] args) {
launch(args);
}
@Override
public void start(Stage stage) throws Exception {
Scene scene = new Scene(new Pane());
scene.addEventFilter(KeyEvent.KEY_PRESSED, e -> {
if (e.getCode() == KeyCode.ADD || e.getCode() == KeyCode.EQUALS || e.getCode() == KeyCode.PLUS) {
System.out.println("YES");
}
});
stage.setScene(scene);
stage.show();
}
}
如果您希望它适用于所有平台和键盘布局,您需要原始字符和 KeyEvent.KEY_PRESSED 过滤器。来自 KeyEvent 文档:
"Key typed" events are higher-level and generally do not depend on the platform or keyboard layout. They are generated when a Unicode character is entered, and are the preferred way to find out about character input. In the simplest case, a key typed event is produced by a single key press (e.g., 'a').
但要注意:
Often, however, characters are produced by series of key presses (e.g., SHIFT + 'a'), and the mapping from key pressed events to key typed events may be many-to-one or many-to-many.
混合使用它们会得到非常丑陋且难以阅读的代码。
因此您的示例应如下所示:
更新
我已将事件更改为 KEY_PRESSED 并使您的示例 (Ctrl + '+') 尝试工作,即使代码相等或字符相等。在大多数情况下,它适用于代码,但有时 charCombo 适合。
我已经用英语(英国)、法语、德语、西班牙语键盘布局尝试了以下代码并且有效。
import javafx.application.Application;
import javafx.event.EventHandler;
import javafx.scene.Scene;
import javafx.scene.input.KeyCharacterCombination;
import javafx.scene.input.KeyCode;
import javafx.scene.input.KeyCodeCombination;
import javafx.scene.input.KeyCombination;
import javafx.scene.input.KeyEvent;
import javafx.scene.layout.Pane;
import javafx.stage.Stage;
public class Test extends Application {
public static void main(String[] args) {
launch(args);
}
@Override
public void start(Stage stage) throws Exception {
Scene scene = new Scene(new Pane(), 300, 300);
scene.setOnKeyPressed(new EventHandler<KeyEvent>() {
KeyCombination charCombo = new KeyCharacterCombination("+", KeyCombination.CONTROL_DOWN);
KeyCombination codeCombo = new KeyCodeCombination(KeyCode.PLUS, KeyCombination.CONTROL_DOWN);
@Override
public void handle(KeyEvent event) {
if (charCombo.match(event) || codeCombo.match(event)) {
System.out.println("Zoom in");
}
}
});
stage.setScene(scene);
stage.show();
}
}