• Advertisement
Sign in to follow this  

[.net] Distinguish Left/Right Shift/Ctrl/Alt on Windows and Mono?

This topic is 3232 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Is there any cross-platform way to distinguish when left/right modifier keys are pressed? I have seen solutions that involve importing GetKeyState from User32.dll, but is there any "pure .NET" way of doing it? It seems like that information doesn't even come into the WndProc so overriding that doesn't help. If not, does anyone know how to distinguish between left/right modifier keys in Mono?

Share this post


Link to post
Share on other sites
Advertisement
Left and right Ctrl/Alt can be distinguished by checking for an extended key in the WM_KEYDOWN or WM_KEYUP messages.

protected override void WndProc(ref Message m) {
switch (m.Msg) {
case 0x100: // WM_KEYDOWN
case 0x101: // WM_KEYUP
var PressedKey = (Keys)m.WParam;
var IsExtended = ((int)m.LParam & (1 << 24)) != 0;
break;
}
base.WndProc(ref m);
}

This doesn't help the Shift situation, of course, and is still rather Windows-specific. [sad]

Share this post


Link to post
Share on other sites
Hmm well that works without p/invokes on windows but doesn't seem to work on Mono. But I don't have time to more thoroughly test it on Mono right now.

Share this post


Link to post
Share on other sites
Are you using Windows Forms to capture the input? If so, look at the KeyEventArgs passed to the KeyDown event. KeyEventArgs.Modifiers is a bit flag that records whatkeys are pressed at the same time, and there are values for LShiftKey, RShiftKey, LControlKey, and RControlKey.

Share this post


Link to post
Share on other sites
Yeah, I'm using windows forms. Those options are in the Keys enum, but they don't seem to be used. Instead when the ctrl key is pressed, the value reported is either Keys.Control or Keys.ControlKey (I don't remember exactly). With Alt, the value reported is Keys.Menu I think. The LControlKey/etc. values don't seem to be used at all.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement