GetAsyncKeyState() Alt key problem

Started by
3 comments, last by ongamex92 9 years, 10 months ago
Hi Guys,

I have a funny issue when using GetAsyncKeyState() in my application.

I am using the following code to switch between full screen and windowed mode.


if(GetAsyncKeyState(VK_ENTER) && GetAsyncKeyState(VK_MENU))
{
    // react on Alt & Enter keypress
}
But, the Alt key acts as though it is being held.

Pressing Alt & Enter similtaneously works fine.

But, pressing Alt (wait for 10 seconds) then Enter also triggers the event. (Not good)

But, pressing Enter (waiting) then Alt doesn't trigger the event (This is good).

It is like when you press Alt to get a menu and then enter to select the item. Although I am not using any menu's and have no menu's tied to the window in my application.

Any ideas how I can stop this behavior?

Thanks in advance smile.png
Advertisement

You better use WM_SYSKEYDOWN to check for Alt-Enter. This behaves properly as it marks the key press as handled (return zero).

Using GetAsyncKeyState just returns the current state but does not do any changes to the system keyboard states.

Fruny: Ftagn! Ia! Ia! std::time_put_byname! Mglui naflftagn std::codecvt eY'ha-nthlei!,char,mbstate_t>

Actually I forgot to mention that I have this in the Callback


case WM_SYSCOMMAND:
{
	if(wParam==SC_KEYMENU && (lParam>>16)<=0)
		return 0;
}
break;
The above code stops the 'Restore,Move,Size,etc..' box from popping up but doesn't stop the behavior as mentioned.

[edit]
I just changed the key polling to...

if(GetAsyncKeyState(VK_MENU)&0x8000)&&(GetAsyncKeyState(VK_ENTER)&0x8000))

and it worked smile.png

I have no idea what the 0x8000 does though.

8 in binary is 1000, means it have the left most bit set, getasynk.. returns a bitmask where each bit means something, check the docs in msdn.

If the left bit is set, the key is down.

GetAsyncKeyState(KEY) & 0x8000 or GetAsyncKeyState(KEY) & 0x80 (just can remember)

This will be true if the key is down. Alt + Enter is a system command as said above better use the message proc

This topic is closed to new replies.

Advertisement