Calculator project : conflict with mouse and keyboard support


#1

Hello everyone,

I was at the end of the calculator exercise when I found a weird bug/conflict between my mouse and my keyboard support. You can reproduce the bug by clicking on, for example, 9 with your mouse, then + and 1 with the keyboard and finally you will see the problem when clicking “enter” on your keyboard.

I don’t understand how the last click (here 9) is saved in the “enter” keydown eventListener and I don’t even know if the problem come from there or if it come from another part of my code…

Other than that, my calculator works fine if I only use the mouse or the keyboard.

The calculator : https://dxshura.github.io/calculator/

The code : https://github.com/DxShura/calculator

Thank you in advance for your help,
Sacha


#3

Hey @DxShura, Sorry I’m late to the party, but I’m assuming you found the bug because I can’t replicate any errors using the instruction’s you’ve supplied.


#4

Hi @inuhoo, thanks for the reply, but no the bug is still here… It’s strange that you can’t replicate the bug because when I click a number (9) with my mouse and then an operator and a number and equals with my keyboard (+1) I have something like that “9+19 = 10”, the nine is added after. And if a use equals with my keyboard again and without adding anything else, I will have “9+199 = 28”.


#5

Sorry, I mustn’t have followed your directions correctly!

Bizarre error though, I can’t work out what’s causing the push to your calArray at all.
What I can tell you though is that the click event is being fired AFTER the operate() call found in the enter keydown event is completed.

EDIT: sorry I can’t be of anymore use!


#6

FOUND IT!

the focus is sticking to the click, I’m not sure the technical reason as to why this causes the issue maybe someone more experienced than myself could explain what is going on?

however adding;

keyPress.focus();

to your keydown function around line 84 seems to solve the issue


#7

Thank you very much for your help :smile:


#8

You’re welcome! happy coding.
:grin: