调用某些方法时如何忽略键盘上的按键?

时间:2019-02-01 11:34:52

标签: java javafx

我正在制作JavaFX应用程序。

我首先使用Initizlize方法在键盘上分配了按键压力。当我按下“ 1”键时,主窗口中会出现一些带有文本的随机卡片。我可以折叠此卡并通过按键盘上的“ R”键来查看其他文本。我还可以通过“ BACKSPACE”将其删除,并通过“ ESCAPE”将其删除。

问题是:如果我不按数字键(例如“ 1”)在主窗口中显示我的卡。按键代码“ BACKSPACE”和“ ESCAPE”仍然有效,如果我按该键,即使主窗口中没有卡,也会删除卡(某些动画)

因此,如果主窗口中没有卡,我决定禁用按键“ ESCAPE”和“ BACKSPACE”,然后在主窗口中出现卡时将其启用。

换句话说,只有在某些在主窗口中显示卡的方法之后,才能启用“ ESCAPE”和“ BACKSPACE”

我该怎么做?

控制器:

@FXML  public void initialize(URL location, ResourceBundle resources) {

//key commands
mainAnchor.setOnKeyPressed(event -> {
  switch (event.getCode()) {


    case DIGIT1:          
      showRandomQuestionCat1();
      showRandomCard();

      break;

    case DIGIT2:          
      showRandomQuestionCat2();
      showRandomCard();
      break;    

    case R:
      flipCardBack();
      break;


    case ESCAPE:
      hideRandomCard();
      break;

    case BACK_SPACE:
      removeCard();
      flipCardBackSpace();
      break;
     }
   });

 }

这是显示卡片的方式:

//show random card in main window
private void showRandomCard(){

if (!animationRunning) {
  animationRunning = true;

  TranslateTransition tt = new TranslateTransition();
  tt.setDuration(Duration.millis(300));
  tt.setNode(randomCard);
  tt.setFromY(950);
  tt.setFromX(-600);
  tt.setToY(0);
  tt.setToX(0);

  TranslateTransition ttRandomBack = new TranslateTransition();
  ttRandomBack.setDuration(Duration.millis(300));
  ttRandomBack.setNode(randomCardBack);
  ttRandomBack.setFromY(950);
  ttRandomBack.setFromX(-600);
  ttRandomBack.setToY(0);
  ttRandomBack.setToX(0);

  RotateTransition rt = new RotateTransition(Duration.millis(300), randomCard);
  rt.setByAngle(360);
  rt.setRate(1);
  rt.setCycleCount(1);
  rt.setOnFinished(evt -> animationRunning = false);

  RotateTransition rtBack = new RotateTransition(Duration.millis(300), randomCardBack);
  rtBack.setByAngle(360);
  rtBack.setRate(1);
  rtBack.setCycleCount(1);
  rtBack.setOnFinished(evt -> animationRunning = false);

  tt.play();
  ttRandomBack.play();
  rt.play();
  rtBack.play();
   }
 }

这是删除带有文字的卡片的方法:

private void removeCard() {

if (!animationRunning) {
  animationRunning = true;

  TranslateTransition tt2 = new TranslateTransition();
  tt2.setDuration(Duration.millis(300));
  tt2.setNode(outOfCards);
  tt2.setFromY(0);
  tt2.setFromX(0);
  tt2.setToY(-950);
  tt2.setToX(600);

  TranslateTransition tt = new TranslateTransition();
  tt.setDuration(Duration.millis(300));
  tt.setNode(randomCard);
  tt.setFromY(0);
  tt.setFromX(0);
  tt.setToY(-950);
  tt.setToX(600);



  TranslateTransition ttRandomBack = new TranslateTransition();
  ttRandomBack.setDuration(Duration.millis(300));
  ttRandomBack.setNode(randomCardBack);
  ttRandomBack.setFromY(0);
  ttRandomBack.setFromX(0);
  ttRandomBack.setToY(-950);
  ttRandomBack.setToX(600);

  RotateTransition rt = new RotateTransition(Duration.millis(300), randomCard);
  rt.setByAngle(360);
  rt.setRate(1);
  rt.setCycleCount(1);
  rt.setOnFinished(evt -> animationRunning = false);

  RotateTransition rt2 = new RotateTransition(Duration.millis(300), outOfCards);
  rt2.setByAngle(360);
  rt2.setRate(1);
  rt2.setCycleCount(1);
  rt2.setOnFinished(evt -> animationRunning = false);

  RotateTransition rtBack = new RotateTransition(Duration.millis(300), randomCardBack);
  rtBack.setByAngle(360);
  rtBack.setRate(1);
  rtBack.setCycleCount(1);
  rtBack.setOnFinished(evt -> animationRunning = false);

  tt2.play();
  rt2.play();
  tt.play();
  ttRandomBack.play();

  rt.play();
  rtBack.play();
    }
  }

0 个答案:

没有答案