Skip to main content
GameDev.net gamedev.net
🔒 Locked

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

Started by vermilion_wizard Apr 17, 2009 at 1:39 PM 3 replies 2.2k views
Original Post
vermilion_wizard
vermilion_wizard
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?
benryves
benryves
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 +++]
vermilion_wizard
vermilion_wizard
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.
intrest86
intrest86
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 ^_~
vermilion_wizard
vermilion_wizard
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.

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.