Advertisement

Would this work?

Started by February 22, 2002 01:32 PM
8 comments, last by Draxis 22 years, 7 months ago
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.
Oooo... Did I get my shifs(<< >>) backwards?
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
"I thought what I'd do was, I'd pretend I was one of those deaf-mutes." - the Laughing Man
Okie dokie, makes sense.
But would my example have even worked?
I don''t know what your using this for... but if its to control movement you would probably want to use ''GetKeyState'' instead.
What does ''GetKeyState'' return?
Advertisement
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.

"I thought what I'd do was, I'd pretend I was one of those deaf-mutes." - the Laughing Man
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.

"I thought what I'd do was, I'd pretend I was one of those deaf-mutes." - the Laughing Man
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
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.

This topic is closed to new replies.

Advertisement