| NAME: |
|
Key trapping |
| AUTHOR: |
|
punkstar |
| DATE: |
|
9th Jan, 2006 - 08:02:53 AM |
| DESCRIPTION: |
|
The answer to the grand old question, "How can you tell what keys your users are pressing?" |
Need some help with that coding?
This is extremely easy. Firstly open a new form, or the form that you would like to catch the key presses on.
We can then declare a new procedure:
| Code | Private Sub Form_KeyDown(KeyCode As Integer, Shift As Integer)
'space for your code
End Sub |
Now this is the only tricky bit, (if you have no understanding of computers what-so-ever). When you press the letter "d" on the keyboard, you are not sending "d" to the comptuer, you are sending a number. Every key on your keyboard has it own, unique, number.
So if I wanted to find out what the key of anything is, I made a form that would tell me. Whenever I pressed a key, it would set the .text property of a disabled input field to the keycode value.
| Code | Private Sub Form_KeyDown(KeyCode As Integer, Shift As Integer)
Select Case KeyCode
Case Else
txtKey.Text = KeyCode
End Select
End Sub |
As you can see from the above example, I have used a SELECT structure. This is going to add easy expandibility to the code. If, for example, I wanted to run a procedure when the user pressed the Up Arrow of the keyboard, after running the first program, I know that the up arrow keycode is 38. So I can add
| Code | case 38
call myUpArrowProc |
To the SELECT structure, and it would have been intergrated as easily as that!
This technique is used to make games in all kinds of computer languages. I have taken the source code from ocstudios for a small rally car sim. Its pretty good. I have added movement, and it should be put up in the downloads area.
Enjoy!
|