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

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

## 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 on other sites
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 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 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 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.

• 9
• 9
• 13
• 41
• 15