Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Question Concerning Keyascii KeyPress 1

Status
Not open for further replies.

Goodloe

Programmer
Sep 4, 2001
25
US
I created a calculator in VB6. It works ok, except for when I want to enter numbers from the keyboard oppose to clicking with the mouse, I can only enter the numbers once. For example I might want to say 3+2 or 3+3 and so on. What happens is when I keyin 3 plus the number to be calculated; only the number to be calculated can't be entered from the keyboard. The following is a small simple of the code I'm currently working with.

Private Sub cmdDigits_KeyPress(Index As Integer, KeyAscii As Integer)
If KeyAscii = Asc("0") Then
lbldisplay.Caption = lbldisplay.Caption + cmdDigits(0).Caption
cmdDigits(0).SetFocus

I also need help programming the rest of the operator keys ( + - * / Backspace etc...). So far everything works fine longs as I'm clicking with the mouse, I trying get things working from the keyboard as well. THANK YOU SO VERY MUCH.


 

Look into and read up on the forms keypreview event. I believe you will be able to use this to your advantage.

Good Luck

 
Dont do anything in the keypress event, exept..

Private Sub cmdDigits_KeyPress(Index As Integer, KeyAscii As Integer)
If KeyAscii = Asc("0") Then
call cmdDigits_Click(0)
End If
End Sub
All the Best
Praveen Menon
pcmin@rediffmail.com
 
Good morning, I've checked out keypreview Event, and I also tried the following.

Private Sub cmdDigits_KeyPress(Index As Integer, KeyAscii As Integer)
If KeyAscii = Asc("0") Then
call cmdDigits_Click(0)
End If
End Sub

So far I haven't had any luck, In addition to the problem, if I enter a number from the keyboard, and click the clear button to clear it out, and if I try to re-enter the number it won't work when entering it from the keyboard, it works just fine if I do a mouse click. Do you have any other suggestions/examples?
Thanks
 
I think the problem is probably a result of your placement of the KeyPress event. It seems to be attached to a command button. Try using a form level KeyPress event (or see the second to last paragraph).

As for the +-*/ keys, the ASCII codes are 43, 45, 42, and 47 respectively. Backspace has an ASCII value of 8.

You might want to consider using a textbox rather than a label. That will allow the numbers and backspace keys to work naturally.

You might also want to consider programming the non-numeric values of the keys on the numeric keypad. That would allow the calculator to work irregardless of whether or not the NumLock is on. BlackburnKL
 
Yes true.. i didnt notice it was written in the command buttons' key press.. all u have to do is this..


In the forms' keypress event, write the code


Select case keyascii
case Asc("0")
call cmdDigits_Click(0)
case Asc("1")
call cmdDigits_Click(1)
....
....

end select

Also, set the forms' keypreview Property to true.

U can check for all the keys that are about to be pressed, and call their respective commandbuttons' click event to fire the action code. All the Best
Praveen Menon
pcmin@rediffmail.com
 
Mr. Praveen Menon thank you very much, the Select Case and setting the Keypreview Property to true did work.
 
Is this the same calculator you started in September 2001, referred in thread222-131523 ?
________________________________________________________________
If you want to get the best response to a question, please check out FAQ222-2244 first

'People who live in windowed environments shouldn't cast pointers.'
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top