Programming C, C++, Java, PHP, Ruby, Turing, VB
Computer Science Canada 
Programming C, C++, Java, PHP, Ruby, Turing, VB  

Username:   Password: 
 RegisterRegister   
 How do I get VB to recognise two buttons are being pressed?
Index -> Programming, Visual Basic and Other Basics -> Visual Basic Help
View previous topic Printable versionDownload TopicSubscribe to this topicPrivate MessagesRefresh page View next topic
Author Message
Darkmantis




PostPosted: 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
Sponsor
sponsor
Brightguy




PostPosted: 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




PostPosted: 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




PostPosted: 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




PostPosted: 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.
Display posts from previous:   
   Index -> Programming, Visual Basic and Other Basics -> Visual Basic Help
View previous topic Tell A FriendPrintable versionDownload TopicSubscribe to this topicPrivate MessagesRefresh page View next topic

Page 1 of 1  [ 5 Posts ]
Jump to:   


Style:  
Search: