Jump to content
  • Advertisement

Archived

This topic is now archived and is closed to further replies.

Draxis

Would this work?

This topic is 6052 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

I''m brand spanking new at this, so bare with me. Would this macro work for getting keystates?
  
#define KEYDOWN(KeyState) ((GetAsyncKeyState(KeyState)>>((sizeof(SHORT)) - 1)) ? 1 : 0)
  
Since GetAsyncKeyState() returns a SHORT with the actual state being stored in the highest bit, wouldn''t this work? I dunno, bits confuse me.

Share this post


Link to post
Share on other sites
Advertisement
You might find it easier and faster to use a mask instead of a shift sizeof subtract conditional operator

#define KEYDOWN(KeyState) ( GetAsyncKeyState((int)(KeyState)) & 0x8000 )





Edited by - lessbread on February 22, 2002 2:57:14 PM

Share this post


Link to post
Share on other sites
Guest Anonymous Poster
I don''t know what your using this for... but if its to control movement you would probably want to use ''GetKeyState'' instead.

Share this post


Link to post
Share on other sites
Work it out

sizeof(SHORT) == 2

ret >> 1 ? 1 : 0

aka

ret / 2 ? 1 : 0

The trouble is that the other bits of the return value might be set in which case you''ll always return true.

The docs say:
quote:
If the most significant bit is set, the key is down, and if the least significant bit is set, the key was pressed after the previous call to GetAsyncKeyState. However, you should not rely on this last behavior; ... The behavior of the least significant bit of the return value is retained strictly for compatibility with 16-bit Windows applications (which are non-preemptive) and should not be relied upon.


So there''s an uncertainty regarding how the other bits are set. Since & 0x8000 zeroes all of the bits except the msb, it''s also more precise.

If you wanted to shift off all the bits except for the msb, you''d have to shift 15 places to the right

ret >> 15 ? 1 : 0

But since the left most bits are filled with 0''s by the shift, you wouldn''t need the conditional either; ret >> 15 would already be equal to 1 or 0 depending on the situation.

I would still use the mask.

Share this post


Link to post
Share on other sites
quote:
Platform SDK: Windows User Interface
The GetKeyState function retrieves the status of the specified virtual key. The status specifies whether the key is up, down, or toggled (on, off—alternating each time the key is pressed).

SHORT GetKeyState(
int nVirtKey // virtual-key code
);

The key status returned from this function changes as a thread reads key messages from its message queue. The status does not reflect the interrupt-level state associated with the hardware. Use the GetAsyncKeyState function to retrieve that information.

An application calls GetKeyState in response to a keyboard-input message. This function retrieves the state of the key when the input message was generated.


Share this post


Link to post
Share on other sites
You''re right, I see how that''s working now, since
0x8000 = 1000 0000 0000 0000
and
  
1000 0000 0000 0000
& 1001 1100 0101 0110 <-- random return
---------------------
1000 0000 0000 0000

  
while
1000 0000 0000 0000
& 0001 1100 0101 0110 <-- random return
---------------------
0000 0000 0000 0000

Understanding things is fun

Share this post


Link to post
Share on other sites
GetKeyState returns the same thing as GetAsyncKeyState does...
The difference is that GetAsyncKeyState will give you a series of keydowns when you hold a key down. It uses the key ''Repeat delay'' you have set in the keyboard properties. It''ll give you very jerky movement... if thats what your using it for.

Share this post


Link to post
Share on other sites

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!