Fancy Calculator with canvas

I’m doing a Fancy calculator old styled drawing buttons and display with canvas
one canvas per button.

So, I use MouseDown event of the canvas to print the number as follows:
Resultado = Resultado + “0”

CvResultado is the Canvas display, that reads Resultado that store the value.

It works doing click on the button.

But I wanna use the keyboard

So I added a Timer for this purpose and in Action event I do This:

If Keyboard.AsyncKeyDown(&h1D) or Keyboard.AsyncKeyDown(&h52) Then
me.mode = me.ModeOff
Resultado = Resultado + “0”
me.mode = me.ModeMultiple

And Instead of print one “0”, supposing that you’re pressing “zero” key, It prints Zero four times.

What Am I doing wrong?

The timer Period may be too short? Regardless, try using the window’s KeyDown event instead.

Using AsyncKeyDown in that manner, it’s difficult for the user to hold the key down just the right amount of time.
What I have done in similar situation is to create a textfield to receive the keystrokes and place it completely off screen. You can capture what is entered there and react to it appropriately for your app.

create a method “AcceptKey(s as string)”
for your mouse clicks… call that method with the “key” that mouse click represents
for the WINDOW keydown event, call that SAME method with the key that was pressed

The “AcceptKey” method can reject keys from the keyboard that are not appropriate.
This way you have ONE “port of entry” and have two input methods

Heck you could even script it to stream characters from a file thru the same method and automate (or record) keystrokes

What is wrong with the KeyDown event of the canvas ?

If might be the Canvas doesn’t have focus all the time, which is why I suggested Window Keydown instead

I’ve tried this, but the issue is that as Dave said, The canvas doesn’t have the focus All time.

By the way, Dave, I can’t find the Keydown event on Window.

Cuz of this, I’m using a Timer

Ok, I’ve found that I have two Xojo installations. One that I wrote the calculator, is corrupt, so It doesn’t have several events
and also corrupt my project, so now I can’t open it, only I see App logo, without window and methods and properties.

So, well I will write it all again :smiley: :smiley: