How do I get VB to recognise two buttons are being pressed?
Author |
Message |
Darkmantis
|
Posted: Thu May 11, 2006 6:38 pm Post subject: How do I get VB to recognise two buttons are being pressed? |
|
|
in my side scroller rpg I can't figure out how to get my guy to jump diagonally by pressing 2 buttons it either doesnt work or badly bugs out on me. What do I do? |
|
|
|
|
|
Sponsor Sponsor
|
|
|
Brightguy
|
Posted: Wed May 24, 2006 3:05 pm Post subject: Re: How do I get VB to recognise two buttons are being press |
|
|
You're looking for the GetKeyState WinAPI function (or GetKeyboardState). Declare the function as shown and use like so:
VisualBASIC: | If GetKeyState(vbKeyLeft) And &H8000 Then
LeftPos = LeftPos + 1
End If |
That just checks if the high bit is set (in VB an expression is only false if it evaluates to zero). |
|
|
|
|
|
Darkmantis
|
Posted: Thu May 25, 2006 6:24 am Post subject: (No subject) |
|
|
can you plz explain this code more I dont understand how it works. |
|
|
|
|
|
Brightguy
|
Posted: Sat May 27, 2006 10:14 pm Post subject: Re: How do I get VB to recognise two buttons are being press |
|
|
First of all, when you use a WinAPI function you must declare it in the beginning of the module, for example:
VisualBASIC: | Private Declare Function GetKeyState Lib "user32" Alias "GetKeyState" (ByVal nVirtKey As Long) As Integer |
This particular function takes a single parameter, corresponding to the key you want to determine the state of. (You can use the VB Key Code Constants for this, e.g. vbKeyA, vbKeyB, etc.)
The function returns an Integer which will tell you if the key is pressed or not - if the high bit is set then the key is pressed. (You need to know a little about binary numbers for this.) An Integer is two bytes (thus 16 bits), and the high bit is the left-most of them.
A bitwise AND compares the same-positioned bits of two numbers. To determine if the high bit is set, use a bit mask: compare the Integer with the binary number that has only the high bit set (10000000 00000000, represented in hex by &H8000). So in the example code I posted, LeftPos is only incremented when the left key is pressed. |
|
|
|
|
|
Darkmantis
|
Posted: Sun May 28, 2006 10:08 am Post subject: (No subject) |
|
|
thx, ill try to use this code, ill post again if I get it to work or not. |
|
|
|
|
|
|
|