Categories: MSDN / DotNet / Java / Scripts / Linux / PHP Ask - La ask - La Answer

need to exit app when the space bar is pressed

this code whon't work with any keyPrivate Sub Form_KeyDown(KeyCode As Integer, Shift As Integer)
If KeyCode = "189" And Shift = "1" Then End
End Sub
[166 byte] By [Code_Writer] at [2007-11-11 7:00:08]
# 1 Re: need to exit app when the space bar is pressed
Because the parameters are Integers, not Strings. Try this:

Private Sub Form_KeyDown(KeyCode As Integer, Shift As Integer)
' If Shift key pressed...
If Shift And vbShiftMask Then
' ...and Spacebar pressed
If KeyCode = vbKeySpace Then
' End app
Unload Me
End If
End If
End Sub
Phil Weber at 2007-11-11 17:27:33 >
# 2 Re: need to exit app when the space bar is pressed
dosn't work
need 1 that will work even if th curser is in a text box
Code_Writer at 2007-11-11 17:28:33 >
# 3 Re: need to exit app when the space bar is pressed
Set the Form's KeyPreview property to True.
Phil Weber at 2007-11-11 17:29:44 >
# 4 Re: need to exit app when the space bar is pressed
still no i have lots of text boxes and stuff on the form the courser starts in a text box
Code_Writer at 2007-11-11 17:30:39 >
# 5 Re: need to exit app when the space bar is pressed
plz help i need this asap
Code_Writer at 2007-11-11 17:31:38 >
# 6 Re: need to exit app when the space bar is pressed
Plz help even if i have to use a module
Code_Writer at 2007-11-11 17:32:47 >
# 7 Re: need to exit app when the space bar is pressed
i found it

here's the code

Private Declare Function GetAsyncKeyState Lib "user32" (ByVal vKey As Long) As Integer

Private Sub Timer1_Timer()
Dim l As Long
l = GetAsyncKeyState(32) 'ascii code for space
If l <> 0 Then
SendKeys "CAT"
End If
End Sub

you can set the interval of the timer to the time u wish to check for keystrokes
Code_Writer at 2007-11-11 17:33:40 >
# 8 Re: need to exit app when the space bar is pressed
Have you tried this?

Private Sub Form_KeyDown(KeyCode As Integer, Shift As Integer)
If KeyCode = 32 Then
Unload Me
End If
End Sub
dee-u at 2007-11-11 17:34:40 >