Capture Enter key with javascript for HTML5 Canvas Animate Application
I'm converting my old Flash applications to HTML5 Canvas applications. In one of them, I want to capture the Enter key when something is typed in a TextInput component and the Enter key is pressed, and if it is pressed, do something. How do I do that with javascript? Here's my Actionscript code. Many thanks
stage.focus = txt_defNumber;
txt_correct.text = gCorrect
txt_incorrect.text = gIncorrect
txt_given.text = gGiven
txt_defNumber.addEventListener(KeyboardEvent.KEY_DOWN, reportKeyDown)
var Term = ""
var Definition = ""
var gNumber = ""
var TermPlural = ""
function reportKeyDown(event:KeyboardEvent):void {
if (event.keyCode == Keyboard.ENTER) {
gNumber = txt_defNumber.text;
var trim:RegExp = /^\s+|\s+$/g;
gNumber = gNumber.replace(trim, "");
if (gNumber == "1") {
Term = "chemistry"
Definition = "The study of the structure and behavior of matter."
gotoAndPlay("input");
} else if (gNumber == "2") {
Term = "value"
TermPlural = "values"
Definition = "A number and unit that together represent the result of a measurement or calculation, e.g. 100 meters."
gotoAndPlay("input");
} else if (gNumber == "3") {
Term = "unit"
TermPlural = "units"
Definition = "A defined quantity based on a standard, e.g. meter."
gotoAndPlay("input");
} else if (gNumber == "4") {
Term = "base unit"
TermPlural = "base units"
Definition = "The seven units from which all other units in the SI system of measurement are derived."
gotoAndPlay("input");
} else if (gNumber == "5") {
Term = "mass"
TermPlural = "masses"
Definition = "The amount of matter in an object. It can also be defined as the property of matter that leads to gravitational attractions between objects and therefore gives rise to weight."
gotoAndPlay("input");
} else if (gNumber == "6") {
Term = "weight"
TermPlural = "weights"
Definition = "A measure of the force of gravitational attraction between an object and a significantly large body, such as the earth or the moon."
gotoAndPlay("input");
} else if (gNumber == "7") {
Term = "matter"
Definition = "Anything that has mass and occupies space."
gotoAndPlay("input");
} else if (gNumber == "8") {
Term = "absolute zero"
Definition = "Zero kelvins (0 K), the lowest possible temperature, equivalent to -273.15 °C. It is the point beyond which motion can no longer be decreased."
gotoAndPlay("input");
} else if (gNumber == "9") {
Term = "precision"
Definition = "The closeness in value of a series of measurements of the same entity."
gotoAndPlay("input");
} else if (gNumber == "10") {
Term = "accuracy"
Definition = "How closely a measured value approaches the true value of a property."
gotoAndPlay("input");
} else {
txt_defNumber.text = "";
txt_incorrect_number.text = "Please type a number from 1 to 10.";
}
}
}
