|
-
Mar 23rd, 2008, 01:24 PM
#1
Thread Starter
Lively Member
Which key is pressed
Hi,
what is the code for a program like this
IF Up key is pressed then display hello msgbox
Once up key is not pressed down anymore close hello msgbox and display goodbye msgbox
Thanks much appreciated
-
Mar 23rd, 2008, 01:35 PM
#2
Fanatic Member
Re: Which key is pressed
Code:
Private Sub Form_KeyDown(KeyCode As Integer, Shift As Integer)
MsgBox ("hello")
End Sub
Code:
Private Sub Form_KeyUp(KeyAscii As Integer)
MsgBox ("hello")
End Sub
-
Mar 23rd, 2008, 01:37 PM
#3
Re: Which key is pressed
 Originally Posted by airman00
Hi,
what is the code for a program like this
IF Up key is pressed then display hello msgbox
Once up key is not pressed down anymore close hello msgbox and display goodbye msgbox
Thanks much appreciated
Code:
Option Explicit
Private Sub Form_KeyDown(KeyCode As Integer, Shift As Integer)
If KeyCode = 38 Then Print "You pressed the UP key."
End Sub
Private Sub Form_KeyUp(KeyCode As Integer, Shift As Integer)
If KeyCode = 38 Then Print "You released the UP key."
End Sub
Private Sub Form_Load()
Me.Show
Me.KeyPreview = True
End Sub
Last edited by CDRIVE; Mar 23rd, 2008 at 01:49 PM.
<--- Did someone help you? Please rate their post. The little green squares make us feel really smart!
If topic has been resolved, please pull down the Thread Tools & mark it Resolved.
Is VB consuming your life, and is that a bad thing?? 
-
Mar 23rd, 2008, 01:53 PM
#4
Re: Which key is pressed
 Originally Posted by Dungeon Keeper
Code:
Private Sub Form_KeyDown(KeyCode As Integer, Shift As Integer)
MsgBox ("hello")
End Sub
Code:
Private Sub Form_KeyUp(KeyAscii As Integer)
MsgBox ("hello")
End Sub
I considered using a MsgBox, but they are Modal. Because of this your KeyUp code will never execute.
<--- Did someone help you? Please rate their post. The little green squares make us feel really smart!
If topic has been resolved, please pull down the Thread Tools & mark it Resolved.
Is VB consuming your life, and is that a bad thing?? 
-
Mar 23rd, 2008, 01:56 PM
#5
Fanatic Member
-
Mar 23rd, 2008, 02:12 PM
#6
Re: Which key is pressed
 Originally Posted by Dungeon Keeper
This works for me
Code:
Private Sub Form_KeyUp(KeyCode As Integer, Shift As Integer)
MsgBox ("UP")
End Sub
Edit: shi* i messed the code in post a little, my mistake 
I fixed it now 
I didn't mean as a stand alone code block. What you posted first executes the KeyDown event which fires a MsgBox. The MsgBox has to be cleared before any subsequent code will execute. By then the KeyUp event has long past.
You have the same message in both KeyDown & KeyUp events. Change the KeyUp message and you will understand what I'm saying, as it will never fire.
<--- Did someone help you? Please rate their post. The little green squares make us feel really smart!
If topic has been resolved, please pull down the Thread Tools & mark it Resolved.
Is VB consuming your life, and is that a bad thing?? 
-
Mar 23rd, 2008, 02:23 PM
#7
Re: Which key is pressed
 Originally Posted by Dungeon Keeper
This works for me
Code:
Private Sub Form_KeyUp(KeyCode As Integer, Shift As Integer)
MsgBox ("UP")
End Sub
Edit: shi* i messed the code in post a little, my mistake 
I fixed it now 
You are aware that you have the ability to edit your posts and your code? It's a better choice than leaving errors posted.
<--- Did someone help you? Please rate their post. The little green squares make us feel really smart!
If topic has been resolved, please pull down the Thread Tools & mark it Resolved.
Is VB consuming your life, and is that a bad thing?? 
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|