to get information about what key was pressed, I now use following code:
function AddEventListeners() {
document.getElementById('txtHangman').addEventListener('keypress', TextHangman.bind(this), false);
}
And then the eventhandler function:
function TextHangman(_key) {
var _keypressed = _key.which || _key.key;
}
The code works and gives me the information I want but I don't understand what the || operator does when initiliazing the var _keypressed. Some explanation would be great.
Thanks!
G
It means the same as it does everywhere else. It does nothing special when used near a var
statement.
If the left hand side evaluates as true (i.e. not 0
, undefined
, etc), it evaluates as the left hand side.
Otherwise, it evaluates as the right hand side.
Precedence rules means it gets resolved before the assignment.
Essentially the code is the same as:
if (_key.which) {
var _keypressed = _key.which;
} else {
var _keypressed = _key.key;
}