Aug 18, 2011 at 7:16am UTC
andywestken wrote:Adding an 0x1 == test to the shift version, to shut up the warning,
stops it from working for some reason (have not looked into this yet).
It stops working because, if the key is pressed, the result of GetAsyncKeyState
right shifted by 15 bits is 1111111111111111 instead of 0000000000000001.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25
#include <iostream>
#include <string>
#include <bitset>
using namespace std;
inline string short_to_bits(short n)
{
return bitset<16>(n).to_string();
}
inline short bits_to_short(const string & str)
{
return bitset<16>(str).to_ulong();
}
int main()
{
short n = bits_to_short("1000000000000000" );
cout << short_to_bits(n >>= 15) << endl;
cout << ((short )0x1 == n) << endl;
cout << ((short )0xffff == n) << endl;
}
Last edited on Aug 18, 2011 at 7:31am UTC
Aug 18, 2011 at 9:22pm UTC
@m4ster r0shi
OK - but I still find my version rather clearer. To shut up the warning in the shift version I find I need to use either
1 2 3 4
inline bool IsPressed(int vkey)
{
return 0xFFFFFFFF == (GetAsyncKeyState(vkey) >> 15);
}
or
1 2 3 4
inline bool IsPressed(int vkey)
{
return 0x1 == ((USHORT)GetAsyncKeyState(vkey) >> 15);
}
or with const
1 2 3 4 5
inline bool IsPressed(int vkey)
{
const USHORT topBit = 0x8000;
return topBit == ((USHORT)GetAsyncKeyState(vkey) >> 15);
}
Last edited on Aug 18, 2011 at 9:22pm UTC
Aug 18, 2011 at 9:35pm UTC
Suit yourself. I like warnings :D
Aug 18, 2011 at 9:55pm UTC
So you don't work (have never worked) with a zero-warning policy in place??