Help - KeyUp doesn't seem to be responding

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • raiden1985
    New Member
    • Feb 2007
    • 5

    Help - KeyUp doesn't seem to be responding

    Please take a look at my code....

    Code:
        Const WM_KEYDOWN As Integer = &H100
        Const WM_SYSKEYDOWN As Integer = &H104
        Const WM_KEYUP As Integer = &H101
        Const WM_SYSKEYUP As Integer = &H105
    
    
        Protected Overrides Function ProcessCmdKey(ByRef msg As Message, ByVal keyData As Keys) As Boolean
    
    
    
            Dim XLocation As Integer = PictureBox2.Location.X
            Dim Ylocation As Integer = PictureBox2.Location.Y
    
            If ((msg.Msg = WM_KEYDOWN) Or (msg.Msg = WM_SYSKEYDOWN)) Then
    
                Select Case (keyData)
                    Case Keys.W
                        Me.Timer2.Start()
    
                        Me.PictureBox2.SetBounds(XLocation, Ylocation - Me.ComboBox1.Text, 100, 100)
                        If Me.PictureBox2.Top <= Me.PictureBox1.Top Then
                            Ylocation = upbound
                            Me.PictureBox2.SetBounds(XLocation, Me.PictureBox1.Top, 100, 100)
                        End If
    
                    Case Keys.A
                        Me.PictureBox2.SetBounds(XLocation - Me.ComboBox1.Text, Ylocation, 100, 100)
                        If Me.PictureBox2.Left <= Me.PictureBox1.Left Then
                            Me.PictureBox2.SetBounds(Me.PictureBox1.Left, Ylocation, 100, 100)
                        End If
    
                    Case Keys.D
                        Me.PictureBox2.SetBounds(XLocation + Me.ComboBox1.Text, Ylocation, 100, 100)
                        If Me.PictureBox2.Right >= Me.PictureBox1.Right Then
                            Me.PictureBox2.SetBounds(Me.PictureBox1.Right - 100, Ylocation, 100, 100)
                        End If
    
                    Case Keys.S
                        Me.PictureBox2.SetBounds(XLocation, Ylocation + Me.ComboBox1.Text, 100, 100)
                        If Me.PictureBox2.Bottom >= Me.PictureBox1.Bottom Then
                            Me.PictureBox2.SetBounds(XLocation, Me.PictureBox1.Bottom - 100, 100, 100)
                        End If
                End Select
    
            End If
            If ((msg.Msg = WM_KEYUP) Or (msg.Msg = WM_SYSKEYUP)) Then
                If keyData = Keys.P Then
                    MessageBox.Show("hi")
                End If
            End If
    
    
            Return MyBase.ProcessCmdKey(msg, keyData)
        End Function
    Help.....what is the problem with KeyUp? / . \
    It didnt respon when i press & release the P button on keyboard>.<

    P.S. i searched nearly ALL post in here......

    Thanks~! >.<
    Last edited by Killer42; Feb 12 '07, 02:22 AM. Reason: Please use CODE tags around your program code.
  • Killer42
    Recognized Expert Expert
    • Oct 2006
    • 8429

    #2
    What debugging have you tried?

    For example, have you checked what values are present, to see why they don't match what you code expects? Stepping through the code and examining variable values just before the code makes a decision based on them is usually a good way of working out what's going on.

    For that matter, do you know whether control is actually entering this routine?

    Comment

    • raiden1985
      New Member
      • Feb 2007
      • 5

      #3
      I have tried debugging
      i checked the value but there would be a problem
      I need to 'release' the button ( you cannot hold a keyboeard button while you move your mouse to check the code....)
      so....
      i think there is no way to test the WM_KeyUp..i think

      (The value of WM_KeyUP at there while i release the button is 256 , whiich is definetly the value of the WM_KeyDomn if i set the breakpoint at there = =)

      I need to see how people use the WM_KEYUP
      WOuld you kindly show me how a workable function of WM_KEYUP?

      Thanks...

      Comment

      • Killer42
        Recognized Expert Expert
        • Oct 2006
        • 8429

        #4
        I don't think I can help directly, as I work in VB6 and this appears to be structured quite differently. I would have simply coded in the KeyDown and KeyUp events. :)

        What I would recommend trying first is commenting out the entire routine (or creating a test program - this is probably simpler) and inserting a Debug.Print of msg and KeyData. Then run it, press your keys, and see what comes out. That should tell you whether you're testing for the correct values.

        Comment

        • raiden1985
          New Member
          • Feb 2007
          • 5

          #5
          maybe i just turn to use keyup and keydown =.=
          but i just find out that keyup/keydown need an object ( like textbox1 etc)
          to process

          how can i use keyup/keydown like 'Overrides' event?

          (I just want to move a picturebox via keyboard ....like a real game
          i really tried searching the web for game code but nothing help = =
          could u please show me some example?)

          Thanks for your replys.....thou ght not yet really helpful but
          thanks for your advice

          Comment

          • balid
            New Member
            • Feb 2007
            • 18

            #6
            Raiden1985,

            Have you confirmed that that msg.msg value being passed matches your keyUp values or what the value of keyData is?

            That least that's where I would start.

            If you don't mind me asking, is this for an assignment? Because it seems like it would be a lot easier to use the KeyUp and KeyDown events as Killer42 pointed out.

            Comment

            • Killer42
              Recognized Expert Expert
              • Oct 2006
              • 8429

              #7
              Originally posted by raiden1985
              maybe i just turn to use keyup and keydown =.=
              but i just find out that keyup/keydown need an object ( like textbox1 etc)
              to process

              how can i use keyup/keydown like 'Overrides' event?

              (I just want to move a picturebox via keyboard ....like a real game
              i really tried searching the web for game code but nothing help = =
              could u please show me some example?)

              Thanks for your replys.....thou ght not yet really helpful but
              thanks for your advice
              Well, to be honest, I have never before heard of "Overrides" . VB6 and VB.Net really are quite different languages in many ways. So I can't say whether it's better or worse than the way I would have coded it. There may be very good reasons not to use the KeyUp/KeyDown events. Or there might not - I wouldn't know.

              One thing I do know (for VB6, at least) is that if you set the KeyPreview property of a form to True, then it gets first look at keypresses before any of the controls on the form.

              Hm... have you tried posting your question in the .Net forum? Perhaps you'll find a better response there.

              (Also, I'll see if I can get one or two other VB experts to jump in here - their .Net skills may be better than mine.)

              Comment

              • willakawill
                Top Contributor
                • Oct 2006
                • 1646

                #8
                This function traps command keypresses and menu shortcuts. It is not going to trap normal keys like P, according to Microsoft anyway.
                to quote them:
                This method is called during message preprocessing to handle command keys. Command keys are keys that always take precedence over regular input keys. Examples of command keys include accelerators and menu shortcuts. The method must return true to indicate that it has processed the command key, or false to indicate that the key is not a command key. This method is only called when the control is hosted in a Windows Forms application or as an ActiveX control.

                Comment

                • hariharanmca
                  Top Contributor
                  • Dec 2006
                  • 1977

                  #9
                  Originally posted by raiden1985
                  Please take a look at my code....

                  Code:
                      Const WM_KEYDOWN As Integer = &H100
                      Const WM_SYSKEYDOWN As Integer = &H104
                      Const WM_KEYUP As Integer = &H101
                      Const WM_SYSKEYUP As Integer = &H105
                  
                  
                      Protected Overrides Function ProcessCmdKey(ByRef msg As Message, ByVal keyData As Keys) As Boolean
                  
                  
                  
                          Dim XLocation As Integer = PictureBox2.Location.X
                          Dim Ylocation As Integer = PictureBox2.Location.Y
                  
                          If ((msg.Msg = WM_KEYDOWN) Or (msg.Msg = WM_SYSKEYDOWN)) Then
                  
                              Select Case (keyData)
                                  Case Keys.W
                                      Me.Timer2.Start()
                  
                                      Me.PictureBox2.SetBounds(XLocation, Ylocation - Me.ComboBox1.Text, 100, 100)
                                      If Me.PictureBox2.Top <= Me.PictureBox1.Top Then
                                          Ylocation = upbound
                                          Me.PictureBox2.SetBounds(XLocation, Me.PictureBox1.Top, 100, 100)
                                      End If
                  
                                  Case Keys.A
                                      Me.PictureBox2.SetBounds(XLocation - Me.ComboBox1.Text, Ylocation, 100, 100)
                                      If Me.PictureBox2.Left <= Me.PictureBox1.Left Then
                                          Me.PictureBox2.SetBounds(Me.PictureBox1.Left, Ylocation, 100, 100)
                                      End If
                  
                                  Case Keys.D
                                      Me.PictureBox2.SetBounds(XLocation + Me.ComboBox1.Text, Ylocation, 100, 100)
                                      If Me.PictureBox2.Right >= Me.PictureBox1.Right Then
                                          Me.PictureBox2.SetBounds(Me.PictureBox1.Right - 100, Ylocation, 100, 100)
                                      End If
                  
                                  Case Keys.S
                                      Me.PictureBox2.SetBounds(XLocation, Ylocation + Me.ComboBox1.Text, 100, 100)
                                      If Me.PictureBox2.Bottom >= Me.PictureBox1.Bottom Then
                                          Me.PictureBox2.SetBounds(XLocation, Me.PictureBox1.Bottom - 100, 100, 100)
                                      End If
                              End Select
                  
                          End If
                          If ((msg.Msg = WM_KEYUP) Or (msg.Msg = WM_SYSKEYUP)) Then
                              If keyData = Keys.P Then
                                  MessageBox.Show("hi")
                              End If
                          End If
                  
                  
                          Return MyBase.ProcessCmdKey(msg, keyData)
                      End Function
                  Help.....what is the problem with KeyUp? / . \
                  It didnt respon when i press & release the P button on keyboard>.<

                  P.S. i searched nearly ALL post in here......

                  Thanks~! >.<

                  are you sure that KeyUp, KeyDown, and KeyPress are not working properly
                  OR
                  any one of this is not working

                  Comment

                  • Killer42
                    Recognized Expert Expert
                    • Oct 2006
                    • 8429

                    #10
                    Originally posted by willakawill
                    This function traps command keypresses and menu shortcuts. It is not going to trap normal keys like P, according to Microsoft anyway.
                    to quote them: ...
                    I don't know, Will. That wording is a bit vague. It could indicate that whatever you decide to process here becomes, by definition, a "command key".

                    Raiden, I still think basic debugging skills will produce your answer in this case; step through code, use breakpoints/watches/whatever, and determine exactly what is happening, and check against what was intended.

                    Comment

                    Working...