[c/c++ 11 - win32] - how can i detect if the caps lock is activated?
how can i detect if the caps\scrool\num lock is activated?
i have these code that works fine:
1 2 3 4
|
if ((GetKeyState(VK_CAPITAL) & 0x0001)!=0)
a.Text="Caps Lock ON!";
else
a.Text="Caps Lock OFF!";
|
but the WM_KEYUP message only works if the window have the focus. so how can i detect when the window don't have the focus?
finally the code works fine:
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 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53
|
const int AnyKey=257;
const int NoKey=256;
const int ON=2;
void GetKeyBoardKeyState(int *keys)
{
bool NoKey=true;
bool AnyKey =false;
for(int i=0; i<256; i++)
{
keys[i]=0;
if(GetKeyState(i) & 0x8000)
{
//if a key isn't a lock activated(2)
//put it 1
keys[i]=1;
//any key pressed
NoKey=false;
AnyKey=true;
}
else
{
keys[i]=0;
}
}
//testing the lock activation
if(((GetKeyState(VK_CAPITAL) & 0x0001)!=0))
keys[VK_CAPITAL]=2;
if(((GetKeyState(VK_NUMLOCK) & 0x0001)!=0))
keys[VK_NUMLOCK]=2;
if(((GetKeyState(VK_SCROLL) & 0x0001)!=0))
keys[VK_SCROLL]=2;
if(NoKey==false)
{
keys[256]=0;
}
else
{
keys[256]=1;
}
if(AnyKey==true)
{
keys[257]=1;
}
else
{
keys[257]=0;
}
}
|
it needs be optimizated, but works fine.
i use it with a timer with 120ms. even, when the window loses the focus, i can get the keys states ;)
Topic archived. No new replies allowed.