Results 1 to 7 of 7

Thread: Which key is pressed

  1. #1

    Thread Starter
    Lively Member
    Join Date
    Jul 2007
    Posts
    67

    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

  2. #2
    Fanatic Member Dungeon Keeper's Avatar
    Join Date
    Mar 2008
    Posts
    590

    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

  3. #3
    PowerPoster CDRIVE's Avatar
    Join Date
    Jul 2007
    Posts
    2,620

    Re: Which key is pressed

    Quote 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??

  4. #4
    PowerPoster CDRIVE's Avatar
    Join Date
    Jul 2007
    Posts
    2,620

    Re: Which key is pressed

    Quote 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??

  5. #5
    Fanatic Member Dungeon Keeper's Avatar
    Join Date
    Mar 2008
    Posts
    590

    Re: Which key is pressed

    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

  6. #6
    PowerPoster CDRIVE's Avatar
    Join Date
    Jul 2007
    Posts
    2,620

    Re: Which key is pressed

    Quote 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??

  7. #7
    PowerPoster CDRIVE's Avatar
    Join Date
    Jul 2007
    Posts
    2,620

    Re: Which key is pressed

    Quote 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
  •  



Click Here to Expand Forum to Full Width