Keyup

Status
Not open for further replies.

rwp2Lehigh

Solid State Member
Messages
15
Hey all,

I'm a couple of days into teaching myself VB as I have a little C++ background, so don't laugh if I ask a pretty basic question...

I'm making a calculator for the sake of making something, but I'm having a problem getting it to respond to keystrokes. I tried using Keyup, Keydown, and Keypress, but nothing is working. I put the code into the form module as the following:

Private Sub frmCalc_Keyup(Keycode As Integer, Shift As Integer)
Select Case Keycode
Case Asc("0")
Call btn0_Click
Case Asc("1")
Call btn1_Click
Case Asc("2")
Call btn2_Click
End Select
End Sub

And of course I'll add the rest of the keys later once it works. I tried just putting a msgbox(keycode) in the body of the subroutine, but it never gets activated. Any Ideas?
 
Try:

Private Sub Form_KeyUp(KeyCode As Integer, Shift As Integer)
Select Case KeyCode
Case Asc("0")
MsgBox KeyCode
Case Asc("1")
MsgBox KeyCode
Case Asc("2")
MsgBox KeyCode
End Select
End Sub
 
Hmm, no that didn't work either. Like I said, I even tried just a msgBox as the body for the whole subroutine, but it never even gets to it. Here, like this:

Private Sub Form_KeyUp(KeyCode As Integer, Shift As Integer)
msgbox(keycode)
end sub

and no luck. Thanks for the suggestionthough. Any other ideas?
 
There must be something else going on in your application. I built a quick vb app with only the code I posted and it worked as expected.

It may be due to what has focus when you are pressing the keys. If another control on the form has focus then the form will not respond to the keypress events.
 
what is the control that is recieving the input? i mean any textbox which displays the keys pressed? ot what is the form (calculator) look like?
 
Its one form with a bunch of command buttons and a display box that displays strings. I want to call a click event for a command button when the corresponding key has been pressed because the click events are already defined and work appropriately. But, I've tried even just calling a msgbox on a keypress, keyup, and even keydown, but nothing shows up. For some reason the control is never activated. I'm new so its prolly something simple...
 
The issue is that the command button does not have focus. Therefore the keydown event never fires.

To do what you are referencing, I would use a single image for the buttons and set that as the default object. This way you can minimize the objects on the form and can redirect all the keydown events to a global key handling routine.
 
I dont agree, even when the command button does not have focus we can program to fire its click event upon pressing any specified key. but when there are controls on the form all the key presses aand key downs and ups are by default meant for the controls , but to pass them on to the form first instead of the controls , u have to set the keypreview of the form to true.
try this, it must work.
 
skbandi, you are correct. The key is to set the "KeyPreview" of the form to true. This forces the kbd events to be processed by the form before the form's objects.
 
Status
Not open for further replies.
Back
Top Bottom