Sign in to follow this  
3dcgmodeling

()different from (())

Recommended Posts

not work. if(GetAsyncKeyState( (VK_SPACE) & 0x8000) ) { MessageBox(0,"space pressed",0,0); break; } work as intent ,when press spacebar, MessageBox(0,"space pressed",0,0) run. if(GetAsyncKeyState(VK_SPACE) & 0x8000) { MessageBox(0,"space pressed",0,0); break; }

Share this post


Link to post
Share on other sites
Quote:
Original post by 3dcgmodeling
not work.
if(GetAsyncKeyState( (VK_SPACE) & 0x8000) )
{
MessageBox(0,"space pressed",0,0);
break;
}

work as intent ,when press spacebar, MessageBox(0,"space pressed",0,0) run.

if(GetAsyncKeyState(VK_SPACE) & 0x8000)
{
MessageBox(0,"space pressed",0,0);
break;
}
Among other things, parentheses in C/C++ are used in function calls (to enclose arguments), and to control the order in which terms are evaluated.

Note first of all that the parentheses around VK_SPACE are unnecessary; removing them might make the example more clear.

In your first example, the argument to GetAsyncKeyState() is 'VK_SPACE & 0x8000'. This is incorrect; GetAsyncKeyState() accepts a single integer value specifying a key, so by &'ing VK_SPACE with 0x8000 you're specifying a key other than VK_SPACE (or perhaps just submitting an invalid value).

In the second example, the argument to GetAsyncKeyState() is VK_SPACE (which is correct). The return value of this function call is then &'ed with 0x8000 which (according to the docs) tells you whether the key is currently down. The resulting integer value is then converted implicitly to a Boolean value for use in the 'if' conditional.

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this