1.

Solve : VB Keypress function?

Answer»

I'm using VB and I wandered how to get a KEYPRESS to carry out a function. For example I run the program, and I enter UP on my keyboard, this carries out a function. So when a key is entered (it can be any key) it calls a function.

====SAMPLE CODE====

sub draw_circle()

'x, y are positions
picture.circle (x, y), 80

End sub

'if statement is carried out when specified key entered, such as A
If [Key is pressed] then
'calls statement to draw circle in picture when key is entered
call draw_circle

End if
==============
You can use KeyPress, KeyUp, KEYDOWN Events for this

Example:

Private Sub Form_KeyPress(KeyAscii As INTEGER)
If KeyAscii=13 Then '13 is Ascii Code for "ENTER" key
'Call FUNCTIONS Here
ElseIf KeyAscii=27 Then '27 is Ascii Code for "ESC" key
'Call Functions Here
End If
End Sub


or

Private Sub Form_KeyDown(KeyCode As Integer, Shift As Integer)
If KeyCode=38 And Shift=0 Then 'UP Arrow Key
'Call Functions Here
ElseIf KeyCode=37 And Shift=0 Then 'Left Arrow Key
'Call Functions Here
ElseIf KeyCode=39 And Shift=0 Then 'Right Arrow Key
'Call Functions Here
ElseIf KeyCode=40 And Shift=0 Then 'Down Arrow Key
'Call Functions Here
End If
End Sub



'This doesn't seem to be working exactly for me. I do as you ask but it does not call the function.

If I do it with a textbox or a LABEL say and select "ENTER" (13 in ascii) then it carries out the function I want, but it isn't working for the whole form.

Anyone?You can try setting your Form.KeyPreview = True at RunTime or DesignTime.



Discussion

No Comment Found