Jump to content
Xtreme .Net Talk

Recommended Posts

Posted

If i want 2 guys on the screen that walk around by keys, how can i make both of them walk at the same time? It only capures 1 key at a time, I've tried getasynckeystate but it wont work???

 

 

thanks

Posted
Do you mean that you want the same key press to affect both guys' movement; or that every alternate key affects each of them in turn?
Posted

they move with different keys, like one with the arrow and the other with w, s, d, and a, but if I press over on one and on the other at the same time, onlu one will move.

 

I need to capure all the key presses

Posted

Well, AFAIK the system can only process one keypress at a time, but the delay time is very, very small so effectively it seems to the user that they are simultaneously pressing and their instructions are being processed.

To test this theory (maybe to explain it better :-{ ) try this test. In Notepad (or even in a Reply Box in this forum) try pressing any two alphabetical keys simultaneously. You should see that both letters pressed will be displayed one after the other on the page/in the box. The order being decided by which was minimally pressed before the other.

 

I know that Control and Shift keys are dealt with slightly differently, but I didn't think the arrow keys were. Presumably, you are using something like KeyPreview in a form and analyzing the keydown value?

Posted
Using the KeyDown and KeyUp events you can determine what keys are being held down at any particular time.

TT

(*_*)

 

There are 10 types of people in this world;

those that understand binary and those that don't.

  • Leaders
Posted
I can't see how GetAsyncKeyState "wont work"... unless you did it incorrectly. You may also be able to have some boolean variables in your form declaration and let them be True when you have detected the KeyDown, and False when the KeyUp is detected. In a timer or a loop, you can do the movements.

Iceplug, USN

One of my coworkers thinks that I believe that drawing bullets is the most efficient way of drawing bullets. Whatever!!! :-(

Posted
Hello? What's wrong with KeyDown and KeyUp? I've tested it and it works. You can press as many keys simultaneously as you like and keep track of which ones are down and which are not.

TT

(*_*)

 

There are 10 types of people in this world;

those that understand binary and those that don't.

Posted
Hello? What's wrong with KeyDown and KeyUp? I've tested it and it works. You can press as many keys simultaneously as you like and keep track of which ones are down and which are not.

 

I must be missing something. Take this code for example:

 

   Private Sub Form1_KeyDown(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles MyBase.KeyDown
       Debug.Write(e.KeyData.ToString)
   End Sub

 

Now lets say I press and hold 'Q'. The debug window fills in with 'Q"s. Now let's say that, while still holding down 'Q', I press and hold down 'W'. Our debug window starts writing 'W''s instead of 'Q''s. The event arguments for the KeyDown event only tell us about the 'W'. And now when I release the 'W' key, the event stops firing altogether, even though I'm still holding down 'Q'.

 

Cool avatar, btw.

"It may be roundly asserted that human ingenuity cannot concoct a cipher which human ingenuity cannot resolve."

 

- Edgar Allan Poe, 1841

 

I long to accomplish great and noble tasks, but it is my chief duty to accomplish humble tasks as though they were great and noble. The world is moved along, not only by the mighty shoves of its heroes, but also by the aggregate of the tiny pushes of each honest worker.

 

- Helen Keller

Posted

You have to use both KeyDown and KeyUp. Lets say "W" is a key you want to monitor.

 

Dim wKeyState as Boolean

Private Sub Form1_KeyDown(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles MyBase.KeyDown
  If e.KeyCode = Keys.W Then wKeyDown = True
End Sub

Private Sub Form1_KeyUp(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles MyBase.KeyUp
  If e.KeyCode = Keys.W Then wKeyDown = False
End Sub

 

Now, you can test the value of wKeyDown to see whether the "W" key is currently being held down.

TT

(*_*)

 

There are 10 types of people in this world;

those that understand binary and those that don't.

Posted

Cool. And it works for multiple keys, like you said:

 


   Dim WKeydown As Boolean
   Dim QKeyDown As Boolean

   Private Sub Form1_KeyDown(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles MyBase.KeyDown
       If e.KeyData = Keys.W Then WKeydown = True
       If e.KeyData = Keys.Q Then QKeyDown = True
       RefreshLabels()

   End Sub

   Private Sub Form1_KeyUp(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles MyBase.KeyUp
       If e.KeyData = Keys.W Then WKeydown = False
       If e.KeyData = Keys.Q Then QKeyDown = False
       RefreshLabels()
   End Sub

   Sub RefreshLabels()
       If WKeydown Then lblW.Text = "W" Else lblW.Text = ""
       If QKeyDown Then lblQ.Text = "Q" Else lblQ.Text = ""
   End Sub

 

Thanks for the trick.

"It may be roundly asserted that human ingenuity cannot concoct a cipher which human ingenuity cannot resolve."

 

- Edgar Allan Poe, 1841

 

I long to accomplish great and noble tasks, but it is my chief duty to accomplish humble tasks as though they were great and noble. The world is moved along, not only by the mighty shoves of its heroes, but also by the aggregate of the tiny pushes of each honest worker.

 

- Helen Keller

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...