by Michael S. Kaplan, published on 2008/02/02 10:16 -05:00, original URI: http://blogs.msdn.com/b/michkap/archive/2008/02/02/7385461.aspx
The question that came in:
...problems with the Alt Gr Key on the German keyboard layout under Windows 2000/XP but it works under Windows Vista.
Function calls:
IntPtr vHkl = Win32Api.GetKeyboardLayout(Win32Api.GetWindowThreadProcessId(Win32Api.GetForegroundWindow(), IntPtr.Zero));
Keys virtualKey = Win32Api.MapVirtualKeyEx(0xe038, 3, vHkl);
Details regarding the function calls above:
* Win32Api is customer’s own P/Invoke wrapper class around the Win32 API.
* His current keyboard layout is Swiss German.
* 0xe038 is an extended 2 byte scancode for the Right Alt key (=Alt Gr)
* 3 represents MAPVK_VSC_TO_VK_EX which is supposed to differentiate between left and right keys
* vHkl is the keyboard layout handle to the foreground window.
Problem:
* On Vista MapVirtualKey returns Keys.RMenu which is correct.
* On XP/2000 it returns Keys.None (0) which is wrong.
Is this a known problem? Is there a work around?
My pre-processing thought was that it was not that the left key and the right key are different per se; it was the difference between the left alt and the right alt (which is also ALTGR, which is also CTRL+ALT). So the easiest workaround is to do CTRL+ALT to get what one wants, as this will work in all platforms.
(Notice how the new feature provides a way for a whole generation of developers who have never heard of CTRL+ALT to find a whole new way to get at the longstanding functionality?)
But the generic issue is still true in regard to new Vista functionality either way (even for some keys other than the ALTGR key).
Regular readers might see what is going on here if they remember an older blog (When you think it couldn't get any harder, it gets easier, where I reference how to do all of this the harder, indirect way like before this new functionality was added in Vista, in Getting all you can out of a keyboard layout, Part #9a).
My first thought (and indeed my first response) was that they should have read the documentation more carefully - this is new functionality.
But you can kind of see the problem here -- although the function is documented as being able to distinguish between right and left keys, it cannot do so when the difference between their scan codes is one of those extended codes (which is listed as a >= Vista functionality) -- the very example that the new functionality was added to address.
This led to my second thought -- that I should report it as a documentation bug to get the issue clarified.
But to be perfectly honest, I have been wracking my brain trying to figure out what should be said to clarify all of this that will not confuse it further for the majority of people.
The issue is so uncommon that it seems scary to try to try to cover it in a place where it is almost never needed.
I'll report it anyway, I think -- a note explaining the case where this is important like the ALTGR case (which itself has an easier workaround anyway)....
This post brought to you by ஃ (U+0b83, aka TAMIL SIGN VISARGA [sic])