Discussion:
space bar key detection in the program
(too old to reply)
thepeacemaker via VBMonster.com
2008-06-24 11:06:58 UTC
Permalink
Hi All,

I am having difficulty with space bar detection in my program.

I need help with the code to implement a simple IF statement. Any help would
be greatly appreciated.

The Problem = if the spacebar key has been pressed in a textbox (text1) by
the user, then [do something]

for example purposes

if the spacebar key has been pressed in a textbox (text1) by the user, then
exit program.

Thanks and God Bless
--
Message posted via VBMonster.com
http://www.vbmonster.com/Uwe/Forums.aspx/vbasic-controls/200806/1
Norm Cook
2008-06-24 12:01:16 UTC
Permalink
Post by thepeacemaker via VBMonster.com
Hi All,
I am having difficulty with space bar detection in my program.
I need help with the code to implement a simple IF statement. Any help would
be greatly appreciated.
The Problem = if the spacebar key has been pressed in a textbox (text1) by
the user, then [do something]
for example purposes
if the spacebar key has been pressed in a textbox (text1) by the user, then
exit program.
Thanks and God Bless
--
Message posted via VBMonster.com
http://www.vbmonster.com/Uwe/Forums.aspx/vbasic-controls/200806/1
Assuming VB classic,

Private Sub Text1_KeyUp(KeyCode As Integer, Shift As Integer)
If KeyCode = Asc(" ") Then
Unload Me 'or whatever
End If
End Sub
Jan Hyde (VB MVP)
2008-06-24 13:39:13 UTC
Permalink
"Norm Cook" <***@cableone.net>'s wild thoughts were
released on Tue, 24 Jun 2008 07:01:16 -0500 bearing the
Post by Norm Cook
Post by thepeacemaker via VBMonster.com
Hi All,
I am having difficulty with space bar detection in my program.
I need help with the code to implement a simple IF statement. Any help would
be greatly appreciated.
The Problem = if the spacebar key has been pressed in a textbox (text1) by
the user, then [do something]
for example purposes
if the spacebar key has been pressed in a textbox (text1) by the user, then
exit program.
Thanks and God Bless
--
Message posted via VBMonster.com
http://www.vbmonster.com/Uwe/Forums.aspx/vbasic-controls/200806/1
Assuming VB classic,
Private Sub Text1_KeyUp(KeyCode As Integer, Shift As Integer)
If KeyCode = Asc(" ") Then
Unload Me 'or whatever
End If
End Sub
Or

If KeyCode = vbKeySpace Then

Which I like a little better.


--
Jan Hyde

https://mvp.support.microsoft.com/profile/Jan.Hyde
MikeD
2008-06-24 13:40:09 UTC
Permalink
Post by Norm Cook
Post by thepeacemaker via VBMonster.com
Hi All,
I am having difficulty with space bar detection in my program.
I need help with the code to implement a simple IF statement. Any help would
be greatly appreciated.
The Problem = if the spacebar key has been pressed in a textbox (text1) by
the user, then [do something]
for example purposes
if the spacebar key has been pressed in a textbox (text1) by the user, then
exit program.
Assuming VB classic,
Private Sub Text1_KeyUp(KeyCode As Integer, Shift As Integer)
If KeyCode = Asc(" ") Then
Unload Me 'or whatever
End If
End Sub
Why would you use the Asc function for this? And why use the KeyUp event instead of the KeyPress event? Granted, I guess there's no
harm in using KeyUp, but KeyPress seems more appropriate. IMO, this is better:

Private Sub Text1_KeyPress(KeyAscii As Integer)

If KeyAscii = vbKeySpace Then
Unload Me
End If

End Sub

With that said, IMO, this is a VERY bad way to close an app (if indeed that is the "do something" mentioned).
--
Mike
Microsoft Visual Basic MVP
Jan Hyde (VB MVP)
2008-06-24 14:29:16 UTC
Permalink
"MikeD" <***@nowhere.edu>'s wild thoughts were released
on Tue, 24 Jun 2008 09:40:09 -0400 bearing the following
Post by MikeD
Post by Norm Cook
Post by thepeacemaker via VBMonster.com
Hi All,
I am having difficulty with space bar detection in my program.
I need help with the code to implement a simple IF statement. Any help would
be greatly appreciated.
The Problem = if the spacebar key has been pressed in a textbox (text1) by
the user, then [do something]
for example purposes
if the spacebar key has been pressed in a textbox (text1) by the user, then
exit program.
Assuming VB classic,
Private Sub Text1_KeyUp(KeyCode As Integer, Shift As Integer)
If KeyCode = Asc(" ") Then
Unload Me 'or whatever
End If
End Sub
Why would you use the Asc function for this? And why use the KeyUp event instead of the KeyPress event? Granted, I guess there's no
Private Sub Text1_KeyPress(KeyAscii As Integer)
If KeyAscii = vbKeySpace Then
Unload Me
End If
End Sub
With that said, IMO, this is a VERY bad way to close an app (if indeed that is the "do something" mentioned).
Maybe the OP has a lucrative contract to fix bugs.....
--
Jan Hyde

https://mvp.support.microsoft.com/profile/Jan.Hyde
Kamiru Mwangi
2011-01-24 01:36:50 UTC
Permalink
On the KeyPress even you can use this ...

If e.KeyChar = ControlChars.Cr Then
'Do it here....
application.Exit
End If

Hope this helps.
Post by thepeacemaker via VBMonster.com
Hi All,
I am having difficulty with space bar detection in my program.
I need help with the code to implement a simple IF statement. Any help would
be greatly appreciated.
The Problem = if the spacebar key has been pressed in a textbox (text1) by
the user, then [do something]
for example purposes
if the spacebar key has been pressed in a textbox (text1) by the user, then
exit program.
Thanks and God Bless
--
Message posted via VBMonster.com
http://www.vbmonster.com/Uwe/Forums.aspx/vbasic-controls/200806/1
Post by Norm Cook
Assuming VB classic,
Private Sub Text1_KeyUp(KeyCode As Integer, Shift As Integer)
If KeyCode = Asc(" ") Then
Unload Me 'or whatever
End If
End Sub
Post by Jan Hyde (VB MVP)
released on Tue, 24 Jun 2008 07:01:16 -0500 bearing the
Or
If KeyCode = vbKeySpace Then
Which I like a little better.
--
Jan Hyde
https://mvp.support.microsoft.com/profile/Jan.Hyde
Post by MikeD
Why would you use the Asc function for this? And why use the KeyUp event instead of the KeyPress event? Granted, I guess there's no
Private Sub Text1_KeyPress(KeyAscii As Integer)
If KeyAscii = vbKeySpace Then
Unload Me
End If
End Sub
With that said, IMO, this is a VERY bad way to close an app (if indeed that is the "do something" mentioned).
--
Mike
Microsoft Visual Basic MVP
Post by Jan Hyde (VB MVP)
on Tue, 24 Jun 2008 09:40:09 -0400 bearing the following
Maybe the OP has a lucrative contract to fix bugs.....
--
Jan Hyde
https://mvp.support.microsoft.com/profile/Jan.Hyde
Submitted via EggHeadCafe
SharePoint Tip / Thought of the Day WebPart
http://www.eggheadcafe.com/tutorials/aspnet/14280ff8-3c9f-46bd-8214-9267e613c8ec/sharepoint-tip--thought-of-the-day-webpart.aspx
Loading...