P5.js:如何知道是否不再按下某个键?

时间:2018-10-06 13:50:38

标签: processing p5.js

我如何知道是否释放了钥匙? C#编码器已经回答了这个问题,但是我不使用C#。

-- MySQL Workbench Forward Engineering

SET @OLD_UNIQUE_CHECKS=@@UNIQUE_CHECKS, UNIQUE_CHECKS=0;
SET @OLD_FOREIGN_KEY_CHECKS=@@FOREIGN_KEY_CHECKS, FOREIGN_KEY_CHECKS=0;
SET @OLD_SQL_MODE=@@SQL_MODE, SQL_MODE='ONLY_FULL_GROUP_BY,STRICT_TRANS_TABLES,NO_ZERO_IN_DATE,NO_ZERO_DATE,ERROR_FOR_DIVISION_BY_ZERO,NO_ENGINE_SUBSTITUTION';

-- -----------------------------------------------------
-- Schema DialogueFlowDB
-- -----------------------------------------------------

-- -----------------------------------------------------
-- Schema DialogueFlowDB
-- -----------------------------------------------------
CREATE SCHEMA IF NOT EXISTS `DialogueFlowDB` DEFAULT CHARACTER SET utf8 ;
USE `DialogueFlowDB` ;

-- -----------------------------------------------------
-- Table `DialogueFlowDB`.`Status`
-- -----------------------------------------------------
CREATE TABLE IF NOT EXISTS `DialogueFlowDB`.`Status` (
  `ID` INT UNSIGNED NOT NULL AUTO_INCREMENT,
  `Status` CHAR(255) NOT NULL,
  PRIMARY KEY (`ID`))
ENGINE = InnoDB;


-- -----------------------------------------------------
-- Table `DialogueFlowDB`.`MultiStep_Dialogues`
-- -----------------------------------------------------
CREATE TABLE IF NOT EXISTS `DialogueFlowDB`.`MultiStep_Dialogues` (
  `ID` INT UNSIGNED NOT NULL AUTO_INCREMENT,
  `MultiStep_Dialogue` CHAR(255) NOT NULL,
  `Data_Items` VARCHAR(1000) NULL,
  `Nodes` VARCHAR(1000) NULL,
  `Node_Links` VARCHAR(1000) NULL,
  `Comments` VARCHAR(1000) NULL,
  `Status` INT UNSIGNED NOT NULL,
  `Issue` INT NOT NULL,
  `LO_Name` CHAR(255) NULL,
  `LO_Description` CHAR(255) NULL,
  `LO_Condition` VARCHAR(1000) NULL,
  `LO_Variables` VARCHAR(1000) NULL,
  `LO_List` INT NULL,
  PRIMARY KEY (`ID`),
  INDEX `fk_MultiStep_Dialogues_Status1_idx` (`Status` ASC) VISIBLE,
  CONSTRAINT `fk_MultiStep_Dialogues_Status1`
    FOREIGN KEY (`Status`)
    REFERENCES `DialogueFlowDB`.`Status` (`ID`)
    ON DELETE NO ACTION
    ON UPDATE NO ACTION)
ENGINE = InnoDB;


-- -----------------------------------------------------
-- Table `DialogueFlowDB`.`Node_Types`
-- -----------------------------------------------------
CREATE TABLE IF NOT EXISTS `DialogueFlowDB`.`Node_Types` (
  `ID` INT UNSIGNED NOT NULL AUTO_INCREMENT,
  `Node_Type` CHAR(255) NOT NULL,
  PRIMARY KEY (`ID`))
ENGINE = InnoDB;


-- -----------------------------------------------------
-- Table `DialogueFlowDB`.`Variables`
-- -----------------------------------------------------
CREATE TABLE IF NOT EXISTS `DialogueFlowDB`.`Variables` (
  `ID` INT UNSIGNED NOT NULL AUTO_INCREMENT,
  `Variable` CHAR(255) NOT NULL,
  `Variable_ID` CHAR(255) NOT NULL,
  `System_Variable` CHAR(255) NULL,
  `Values_per_Item` ENUM('Single', 'Multiple') NOT NULL DEFAULT 'Single',
  `MultiStep_Dialogue` INT UNSIGNED NOT NULL,
  `Input_Values` VARCHAR(1000) NULL,
  `Comments` VARCHAR(1000) NULL,
  PRIMARY KEY (`ID`),
  INDEX `fk_Variables_MultiStep_Dialogues1_idx` (`MultiStep_Dialogue` ASC) VISIBLE,
  CONSTRAINT `fk_Variables_MultiStep_Dialogues1`
    FOREIGN KEY (`MultiStep_Dialogue`)
    REFERENCES `DialogueFlowDB`.`MultiStep_Dialogues` (`ID`)
    ON DELETE NO ACTION
    ON UPDATE NO ACTION)
ENGINE = InnoDB;


-- -----------------------------------------------------
-- Table `DialogueFlowDB`.`Nodes`
-- -----------------------------------------------------
CREATE TABLE IF NOT EXISTS `DialogueFlowDB`.`Nodes` (
  `ID` INT UNSIGNED NOT NULL AUTO_INCREMENT,
  `Node` CHAR(255) NOT NULL,
  `Node_ID` CHAR(255) NOT NULL,
  `Node_Sequence` ENUM('End', 'Middle', 'Start') NOT NULL,
  `Node_Type` INT UNSIGNED NOT NULL,
  `XPosition` INT NOT NULL DEFAULT 0,
  `YPosition` INT NOT NULL DEFAULT 0,
  `Answer` VARCHAR(1000) NOT NULL,
  `Data_Item` INT UNSIGNED NULL,
  `Status` INT UNSIGNED NOT NULL DEFAULT 'Created',
  `MultiStep_Dialogue` INT UNSIGNED NOT NULL,
  `Comments` VARCHAR(1000) NULL,
  `Previous_Node_Links` VARCHAR(1000) NULL,
  `Next_Node_Link` VARCHAR(100) NULL,
  PRIMARY KEY (`ID`),
  INDEX `fk_Nodes_MultiStep_Dialogues_idx` (`MultiStep_Dialogue` ASC) VISIBLE,
  INDEX `fk_Nodes_Node_Types1_idx` (`Node_Type` ASC) VISIBLE,
  INDEX `fk_Nodes_Variables1_idx` (`Data_Item` ASC) VISIBLE,
  INDEX `fk_Nodes_Status1_idx` (`Status` ASC) VISIBLE,
  CONSTRAINT `fk_Nodes_MultiStep_Dialogues`
    FOREIGN KEY (`MultiStep_Dialogue`)
    REFERENCES `DialogueFlowDB`.`MultiStep_Dialogues` (`ID`)
    ON DELETE NO ACTION
    ON UPDATE NO ACTION,
  CONSTRAINT `fk_Nodes_Node_Types1`
    FOREIGN KEY (`Node_Type`)
    REFERENCES `DialogueFlowDB`.`Node_Types` (`ID`)
    ON DELETE NO ACTION
    ON UPDATE NO ACTION,
  CONSTRAINT `fk_Nodes_Status1`
    FOREIGN KEY (`Status`)
    REFERENCES `DialogueFlowDB`.`Status` (`ID`)
    ON DELETE NO ACTION
    ON UPDATE NO ACTION,
  CONSTRAINT `fk_Nodes_Variables1`
    FOREIGN KEY (`Data_Item`)
    REFERENCES `DialogueFlowDB`.`Variables` (`ID`)
    ON DELETE NO ACTION
    ON UPDATE NO ACTION)
ENGINE = InnoDB;


-- -----------------------------------------------------
-- Table `DialogueFlowDB`.`Node_Links`
-- -----------------------------------------------------
CREATE TABLE IF NOT EXISTS `DialogueFlowDB`.`Node_Links` (
  `ID` INT UNSIGNED NOT NULL AUTO_INCREMENT,
  `Node_Link` CHAR(255) NOT NULL,
  `MultiStep_Dialogue` INT UNSIGNED NOT NULL,
  `From_Node` INT UNSIGNED NOT NULL,
  `To_Node` INT UNSIGNED NOT NULL,
  `Status` INT UNSIGNED NOT NULL,
  `Comments` VARCHAR(1000) NULL,
  PRIMARY KEY (`ID`),
  INDEX `fk_Node_Links_Nodes1_idx` (`From_Node` ASC) VISIBLE,
  INDEX `fk_Node_Links_MultiStep_Dialogues1_idx` (`MultiStep_Dialogue` ASC) VISIBLE,
  INDEX `fk_Node_Links_Nodes2_idx` (`To_Node` ASC) VISIBLE,
  INDEX `fk_Node_Links_Status1_idx` (`Status` ASC) VISIBLE,
  CONSTRAINT `fk_Node_Links_Nodes1`
    FOREIGN KEY (`From_Node`)
    REFERENCES `DialogueFlowDB`.`Nodes` (`ID`)
    ON DELETE NO ACTION
    ON UPDATE NO ACTION,
  CONSTRAINT `fk_Node_Links_MultiStep_Dialogues1`
    FOREIGN KEY (`MultiStep_Dialogue`)
    REFERENCES `DialogueFlowDB`.`MultiStep_Dialogues` (`ID`)
    ON DELETE NO ACTION
    ON UPDATE NO ACTION,
  CONSTRAINT `fk_Node_Links_Nodes2`
    FOREIGN KEY (`To_Node`)
    REFERENCES `DialogueFlowDB`.`Nodes` (`ID`)
    ON DELETE NO ACTION
    ON UPDATE NO ACTION,
  CONSTRAINT `fk_Node_Links_Status1`
    FOREIGN KEY (`Status`)
    REFERENCES `DialogueFlowDB`.`Status` (`ID`)
    ON DELETE NO ACTION
    ON UPDATE NO ACTION)
ENGINE = InnoDB;


SET SQL_MODE=@OLD_SQL_MODE;
SET FOREIGN_KEY_CHECKS=@OLD_FOREIGN_KEY_CHECKS;
SET UNIQUE_CHECKS=@OLD_UNIQUE_CHECKS;

此代码似乎无效。 (不要介意我使用的整数。)

2 个答案:

答案 0 :(得分:3)

按下键时,keyPressed事件被调用一次。 释放键后,keyReleased事件将被调用一次。 变量keyCode告诉您按下或释放了哪个键。 keyIsDown功能可用于检查特定的按键当前是否处于按下状态,即是否按下。

请注意,按住键时不会连续调用keyPressedkeyReleased

function draw() {}

function keyPressed(){
    if (keyCode === ENTER){
      console.log("Enter key pressed");
    }
    return false; // prevent any default behavior
}

function keyReleased(){
    if (keyCode === ENTER){
      console.log("Enter key released");
    }
    return false; // prevent any default behavior
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/p5.js/0.7.3/p5.min.js"></script>

如果要连续检查和处理键的状态,则可以通过检查drawkeyIsDown函数中进行此操作:

function draw() {

    .....

    if ( keyIsDown(ENTER) ) {
        this.velocity -= this.gravity*15;
    } else {
        this.velocity += this.gravity*20000;
    }

    .....

}

如果按住ENTER键(当然焦点必须在画布上),此简单示例将更改画布的颜色:

function draw() {
    if ( keyIsDown(ENTER) )
        background(255,0,0)
    else
        background(0,0,255)
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/p5.js/0.7.3/p5.min.js"></script>

答案 1 :(得分:-1)

您可以使用以下代码:

$(document).keyup(function(){
console.log('key up');
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
记得添加jquery!