KeyDown is firing twice

I added some code to a KeyDown event on the main window of an app. The problem is that when the “backspace” key is detected the KeyDown event runs two times even though the key is pressed once. The effect is that the backspace gets executed twice. Other keys work as expected. Below is the code.

Dim n As Integer

Select Case Key
Case “1”
CalcLabel.text = CalcLabel.text + Key // Add a “1” to the Label
Case “2”
CalcLabel.text = CalcLabel.text + Key // Add a “2” to the Label…
Case “3”
CalcLabel.text = CalcLabel.text + Key
Case “4”
CalcLabel.text = CalcLabel.text + Key
Case “5”
CalcLabel.text = CalcLabel.text + Key
Case “6”
CalcLabel.text = CalcLabel.text + Key
Case “7”
CalcLabel.text = CalcLabel.text + Key
Case “8”
CalcLabel.text = CalcLabel.text + Key
Case “9”
CalcLabel.text = CalcLabel.text + Key
Case “0”
CalcLabel.text = CalcLabel.text + Key
Else
if Key = Chr(8) then //Backspace key press
n=Len(CalcLabel.text)
CalcLabel.text =CalcLabel.text .Left(n-1) //Shorten the label by one character
end if
End Select

you need to RETURN TRUE if you have dealt with the key…

So you are dealing with the BS key, and since you did not return true, the OS is dealing with it as well.

reduce your code

[code]
Select Case Key
Case “0” to “9”
CalcLabel.text = CalcLabel.text + Key // Add a “1” to the Label

[code]

or better yet…

  If key=ChrB(8) Then Return False
  If key<"0" Or key>"9"  Then Return True
  Return False

[quote=33142:@Dave S]or better yet…

If key=ChrB(8) Then Return False If key<"0" Or key>"9" Then Return True Return False [/quote]
If you wanted to be truly concise:

  Return key<"0" and not key=chr(8) Or key>"9"

However, that code doesn’t do what the OP asked for. He has the keydown handler in the Window, not in the Label he’s changing, so both your code examples do not entirely solve his issue.

The OP main issue is he is not returning TRUE causing the key to be processed twice

Doug, did returning True solve the issue?

Yes. Sorry I did not notice the required Boolean value on the event before posting this. Thanks for the help.

I did take the advice and shortened the code also.
The project is a feet-inches-sixteenths calculator that I am rewriting from an old VB6 program that I wrote years ago. I figured I would resurrect it and use it as a learning tool for Xojo.