Skip to content

Commit

Permalink
Merge pull request #3 from V3ntus/master
Browse files Browse the repository at this point in the history
Added keyboard input support
  • Loading branch information
Alitindrawan24 committed Oct 9, 2021
2 parents 22c9c51 + cdb0cd5 commit 67681e1
Showing 1 changed file with 29 additions and 6 deletions.
35 changes: 29 additions & 6 deletions js/binaryCalculator.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,15 +10,15 @@ function insert(num){

function eql(){
let result = res.innerHTML;

operator = result.replace(/[0-9]/g, '');
result = result.split(new RegExp(/[+\-*/]/));
result = result.split(new RegExp(/[+\-*/]/));

let num1 = result[0];
let num2 = result[1];

num1 = binaryToDecimal(num1);
num2 = binaryToDecimal(num2);
num2 = binaryToDecimal(num2);

switch(operator){
case '+':
Expand Down Expand Up @@ -55,4 +55,27 @@ function binaryToDecimal(binary){
decimal += binary[i] * Math.pow(2, binary.length - i - 1);
}
return decimal;
}
}

// register a keystroke listener
document.addEventListener('keydown', (event) => {
if (event.which == 96) { // if numpad0
insert(0);
} else if (event.which == 97) { // if numpad1
insert(1);
} else if (event.which == 48) { // if digit0
insert(0);
} else if (event.which == 49) { // if digit1
insert(1);
} else if (event.which == 107) { // if numpadAdd
insert('+');
} else if (event.which == 109) { // if numpadSubtract
insert('-');
} else if (event.which == 111) { // if numpadDivide
insert('/');
} else if (event.which == 106) { // if numpadMultiply
insert('*');
} else if (event.which == 13) { // if numpadEnter
eql();
}
});

0 comments on commit 67681e1

Please sign in to comment.