Sign in to follow this  
Cygon

[.net] Getting VK_LSHIFT & VK_RSHIFT from WM_KEYDOWN

Recommended Posts

Cygon    1219
I'm using an [url="http://msdn.microsoft.com/en-us/library/system.windows.forms.imessagefilter.aspx"]IMessageFilter[/url] in my .NET-based game to capture certain window messages (mostly WM_INPUT, WM_CHAR and WM_KEYUP/DOWN). Pretty simple stuff:

[code]
internal class WindowMessageFilter : IMessageFilter, IDisposable {
public WindowMessageFilter(IntPtr windowHandle) {
Application.AddMessageFilter(this);
}

bool IMessageFilter.PreFilterMessage(ref Message message) {
switch (message.Msg) {
case (int)UnsafeNativeMethods.WindowMessages.WM_KEYDOWN: {
int virtualKeyCode = message.WParam.ToInt32();
return true; // consumed!
}
}
return false;
}
}
[/code]

According to MSDN, the VK_ code is provided in the wParam of a WM_KEYDOWN message. So far so good, but no matter whether I press the left or the right Shift key, the WM_KEYDOWN I receive reports only VK_SHIFT in its wParam, whereas I would prefer to get separate messages for VK_LSHIFT and VK_RSHIFT.

I've seen that bit 24 in lParam specifies whether it's the left or right shift key, so I could translate VK_SHIFT and VK_CONTROL to the left/right-distinguishing equivalents manually, but is that the only way to arrive at the VK_LSHIFT/VK_RSHIFT messages? Or is there some flag or mode I can set to tell Windows to provide me with the latter?

Share this post


Link to post
Share on other sites
a_loho    106
According to [url="http://msdn.microsoft.com/en-us/library/ms646301(VS.85).aspx"]MSDN[/url] the VK_LSHIFT and VK_RSHIFT are only available with some functions :

[quote]These left- and right-distinguishing constants are available to an application only through the GetKeyboardState, SetKeyboardState, GetAsyncKeyState, GetKeyState, and MapVirtualKey functions.[/quote]

You could try to add a check to GetKeyState(VK_LSHIFT) to distinguish between the two.

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