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

Started by
3 comments, last by vermilion_wizard 15 years ago
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?
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]

[Website] [+++ Divide By Cucumber Error. Please Reinstall Universe And Reboot +++]

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.
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.
Turring Machines are better than C++ any day ^_~
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.

This topic is closed to new replies.

Advertisement