{"id":6253,"date":"2012-10-25T07:00:00","date_gmt":"2012-10-25T07:00:00","guid":{"rendered":"https:\/\/blogs.msdn.microsoft.com\/oldnewthing\/2012\/10\/25\/keyboard-layouts-arent-like-beetlejuice-they-wont-appear-just-because-you-say-their-name\/"},"modified":"2012-10-25T07:00:00","modified_gmt":"2012-10-25T07:00:00","slug":"keyboard-layouts-arent-like-beetlejuice-they-wont-appear-just-because-you-say-their-name","status":"publish","type":"post","link":"https:\/\/devblogs.microsoft.com\/oldnewthing\/20121025-00\/?p=6253","title":{"rendered":"Keyboard layouts aren&#039;t like Beetlejuice &#8211; they won&#039;t appear just because you say their name"},"content":{"rendered":"<p>\nA customer reported a bug in Windows Vista Home Editions:\n<\/p>\n<blockquote CLASS=\"q\">\n<p>\nWe are handling a <kbd>Ctrl<\/kbd>+<kbd>V<\/kbd>\nkeyboard event and want to interpret it in the\ncontext of a US-English keyboard.\n<\/p>\n<pre>\n\/\/ This keyState represents no keys pressed except for Ctrl\nBYTE keyState[256] = {0};\nkeyState[VK_CONTROL] = 0x80;\n\/\/ This is the handle for the US-English keyboard\nHKL hkl = (HKL) 0x04090409;\n\/\/ output variables\nwchar_t outChar[2];\nWORD outWord;\nToUnicodeEx('V', 47, keyState, outChar, 2, 0, hkl);\nToAsciiEx('V', 47, keyState, &amp;outWord, 0, hkl);\nVkKeyScanEx('V', hkl);\n<\/pre>\n<p>\nOn Windows&nbsp;XP and versions of Windows Vista other than Home editions,\nthe three calls all succeed, whereas on Windows Vista Home Editions,\nthe calls fail.\nOn the other hand, if instead of using the US-English keyboard,\nwe use the current keyboard layout:\n<\/p>\n<pre>\nHKL hkl = GetKeyboardLayout(GetCurrentThreadId());\n<\/pre>\n<p>\nthen Windows Vista Home Editions behave the same as\nWindows&nbsp;XP and non-Home editions of Vista.\n<\/p>\n<p>\nThis suggests that the Home Editions of Vista supports keyboard\nqueries only for the currently active keyboard layout,\nwhich renders useless the last parameter to those three functions.\n<\/p>\n<\/blockquote>\n<p>\nNotice how the customer&#8217;s sample code just synthesizes a keyboard\nlayout handle from thin air.\nWhile it is true that\n<a HREF=\"http:\/\/msdn.microsoft.com\/en-us\/library\/ms646296.aspx\">\nthe format keyboard layout handles is documented<\/a>,\nthat doesn&#8217;t mean that you can just make one up\nand start using it.\n<\/p>\n<p>\nIt&#8217;s like saying,\n&#8220;I know that Contoso uses the email address format\nFirstname.Lastname@contoso.com, but I just tried to send email to\nBob.Smith@contoso.com, and it bounced.&#8221;\n<\/p>\n<p>\n&mdash; <i>Does Bob work at Contoso?<\/i>\n<\/p>\n<p>\n&#8220;No. Does that matter?&#8221;\n<\/p>\n<p>\nThe customer&#8217;s code blindly assumes that the US-English keyboard layout\nis loaded rather than calling\n<code>Load&shy;Keyboard&shy;Layout<\/code> to actually load it.\nAs a result, if the keyboard layout is not loaded,\nthe call will fail because you passed an invalid keyboard layout handle.\n<\/p>\n<p>\nThe customer liaison asked,\n&#8220;Is this documented somewhere that the HKL has to be created\nfrom only from the functions and cannot be assigned a value?&#8221;\n<\/p>\n<p>\nUm, yeah, it&#8217;s right there\n<a HREF=\"http:\/\/msdn.microsoft.com\/en-us\/library\/ms646322.aspx\">\nin the documentation<\/a>\nof the <code>hkl<\/code> parameter to the\n<code>To&shy;Unicode&shy;Ex<\/code>\nfunction.\n(Emphasis mine.)\n<\/p>\n<blockquote CLASS=\"m\">\n<p>\n<i>dwhkl<\/i> [in, optional]\n<\/p>\n<p>\nType: <b>HKL<\/b>\n<\/p>\n<p>\nThe input locale identifier used to translate the specified code.\nThis parameter can be any input locale identifier\n<u>previously returned by the <b>Load&shy;Keyboard&shy;Layout<\/b> function<\/u>.\n<\/p>\n<\/blockquote>\n<p>\nIdentical text appears in the documentation of the\n<code>hkl<\/code> parameter to the\n<code>To&shy;Ascii&shy;Ex<\/code> and\n<code>Vk&shy;Key&shy;Scan&shy;Ex<\/code> functions as well.\n<\/p>\n<p>\nThe difference observed on Windows Vista Home Editions, then,\nis that on those systems, in the configurations the customer happens\nto be using, US-English is not a preloaded keyboard layout.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>A customer reported a bug in Windows Vista Home Editions: We are handling a Ctrl+V keyboard event and want to interpret it in the context of a US-English keyboard. \/\/ This keyState represents no keys pressed except for Ctrl BYTE keyState[256] = {0}; keyState[VK_CONTROL] = 0x80; \/\/ This is the handle for the US-English keyboard [&hellip;]<\/p>\n","protected":false},"author":1069,"featured_media":111744,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[1],"tags":[25],"class_list":["post-6253","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-oldnewthing","tag-code"],"acf":[],"blog_post_summary":"<p>A customer reported a bug in Windows Vista Home Editions: We are handling a Ctrl+V keyboard event and want to interpret it in the context of a US-English keyboard. \/\/ This keyState represents no keys pressed except for Ctrl BYTE keyState[256] = {0}; keyState[VK_CONTROL] = 0x80; \/\/ This is the handle for the US-English keyboard [&hellip;]<\/p>\n","_links":{"self":[{"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/posts\/6253","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/users\/1069"}],"replies":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/comments?post=6253"}],"version-history":[{"count":0,"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/posts\/6253\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/media\/111744"}],"wp:attachment":[{"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/media?parent=6253"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/categories?post=6253"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/tags?post=6253"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}