6 Replies - 629 Views - Last Post: 01 March 2013 - 06:42 PM Rate Topic: -----

#1 TheKOAD  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 2
  • Joined: 16-February 13

Using multiple keys in keydown case structure

Posted 16 February 2013 - 04:15 PM

So what I would like to do is in the forms keyDown event, use a case structure to move multiple figures when e.keycode = "A certain key" but the problem I'm encountering is that when I need two keys pressed and held together (ie: key.Right + keys.D), I cannot get more than one figure to move. Here's my code thus far:

Select Case e.KeyCode
            Case Keys.Right
                Me.body1.Location = New Point(body1.Location.X + 5, body1.Location.Y)
                Me.body2.Location = New Point(body2.Location.X + 5, body2.Location.Y)
                Me.body3.Location = New Point(body3.Location.X + 5, body3.Location.Y)
                Me.body4.Location = New Point(body4.Location.X + 5, body4.Location.Y)
            Case Keys.Left
                Me.body1.Location = New Point(body1.Location.X - 5, body1.Location.Y)
                Me.body2.Location = New Point(body2.Location.X - 5, body2.Location.Y)
                Me.body3.Location = New Point(body3.Location.X - 5, body3.Location.Y)
                Me.body4.Location = New Point(body4.Location.X - 5, body4.Location.Y)
            Case Keys.D
                Me.body5.Location = New Point(body5.Location.X + 5, body5.Location.Y)
                Me.body6.Location = New Point(body6.Location.X + 5, body6.Location.Y)
                Me.body7.Location = New Point(body7.Location.X + 5, body7.Location.Y)
                Me.body8.Location = New Point(body8.Location.X + 5, body8.Location.Y)
            Case Keys.A
                Me.body5.Location = New Point(body5.Location.X - 5, body5.Location.Y)
                Me.body6.Location = New Point(body6.Location.X - 5, body6.Location.Y)
                Me.body7.Location = New Point(body7.Location.X - 5, body7.Location.Y)
                Me.body8.Location = New Point(body8.Location.X - 5, body8.Location.Y)
        End Select



Additional cases in replies would be helpful. Thanks in advance

Is This A Good Question/Topic? 0
  • +

Replies To: Using multiple keys in keydown case structure

#2 _HAWK_  Icon User is online

  • Master(Of Foo)
  • member icon

Reputation: 1055
  • View blog
  • Posts: 4,085
  • Joined: 02-July 08

Re: Using multiple keys in keydown case structure

Posted 16 February 2013 - 04:56 PM

Difficult to get that thru normal methods. Workaround:

Use a boolean to toggle whether the any arrow key pressed.
KeyDown if not boolean then boolean(arrow key) = true, next keyDown if boolean and "D" then do something
KeyUp(arrow key) boolean = false

I also used an array of the arrow keys to check against to set the boolean - works!
Was This Post Helpful? 0
  • +
  • -

#3 Jondeate  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 56
  • Joined: 03-February 13

Re: Using multiple keys in keydown case structure

Posted 16 February 2013 - 05:13 PM

Im kind of new to Vb.net, so if any corrections feel free to add.

The problem here is youre using a select case statement.
I think it would work if you used an If instead.

Anyways, in my projects i use this line of code:
Private Declare Function GetKeyPress Lib "user32" Alias "GetAsyncKeyState" (ByVal key As Integer) As Integer

To get key presses.

Then i can put
  If GetKeyPress(Key.(whatever)) Then
Instructions
End if

And that does it.. multiple key presses can be executed at the same time.

good luck
Was This Post Helpful? 0
  • +
  • -

#4 lar3ry  Icon User is offline

  • Coding Geezer
  • member icon

Reputation: 310
  • View blog
  • Posts: 1,290
  • Joined: 12-September 12

Re: Using multiple keys in keydown case structure

Posted 16 February 2013 - 05:39 PM

Just to clarify what Jondeate said, a Select Case statement will only execute one of the Case blocks, then drop out through the End Select. Multiple If statements will execute in turn, and will execute code in each If when the condition is true.
Was This Post Helpful? 1
  • +
  • -

#5 Jondeate  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 56
  • Joined: 03-February 13

Re: Using multiple keys in keydown case structure

Posted 16 February 2013 - 06:15 PM

View Postlar3ry, on 16 February 2013 - 05:39 PM, said:

Just to clarify what Jondeate said, a Select Case statement will only execute one of the Case blocks, then drop out through the End Select. Multiple If statements will execute in turn, and will execute code in each If when the condition is true.


Thank you
Was This Post Helpful? 0
  • +
  • -

#6 TheKOAD  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 2
  • Joined: 16-February 13

Re: Using multiple keys in keydown case structure

Posted 01 March 2013 - 06:30 PM

Sorry for the late response but i haven't had internet access for a while. It looks like what Jondeate said would work, but I was gonna try and be lazy because I hate using unorganized If Then statements. Oh well ce la vie de la programmer.
Thanks
Was This Post Helpful? 0
  • +
  • -

#7 _HAWK_  Icon User is online

  • Master(Of Foo)
  • member icon

Reputation: 1055
  • View blog
  • Posts: 4,085
  • Joined: 02-July 08

Re: Using multiple keys in keydown case structure

Posted 01 March 2013 - 06:42 PM

Did you even try my method? I made a quick solution and tested it and it was working great for what I understood of your requirements.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1