这是我在JavaFX中为ctrl + c组合设置操作的代码。当关注TextField command_line时,它不起作用。为什么呢?
public void setCtrlC() {
command_line.getScene().getAccelerators().put(new KeyCodeCombination(KeyCode.C, KeyCombination.CONTROL_ANY),
new Runnable() {
@Override
public void run() {
LOGGER.debug("CTRL+C pressed");
try {
if (tab_toradex.isSelected()) {
bw.write(3);
bw.flush();
}
if(tab_novatel.isSelected()){
bw2.write(3);
bw2.flush();
}
} catch (IOException e) {
LOGGER.debug("CTRL+C command failed");
}
}
});
}
谢谢!
答案 0 :(得分:0)
好的,用这个解决了:
final KeyCombination keyCombinationShiftC = new KeyCodeCombination(KeyCode.C, KeyCombination.CONTROL_DOWN);
public void setCtrlC() {
command_line.setOnKeyPressed(new EventHandler<KeyEvent>() {
@Override
public void handle(KeyEvent event) {
if (keyCombinationShiftC.match(event)) {
LOGGER.debug("CTRL+C pressed");
try {
if (tab_toradex.isSelected()) {
bw.write(3);
bw.flush();
}
if(tab_novatel.isSelected()){
bw2.write(3);
bw2.flush();
}
} catch (IOException e) {
LOGGER.debug("CTRL+C command failed");
}
}
}
});
}
另一方面,现在它仅在TextField聚焦时起作用...