{"id":22203,"date":"2008-05-23T07:00:00","date_gmt":"2008-05-23T07:00:00","guid":{"rendered":"https:\/\/blogs.msdn.microsoft.com\/oldnewthing\/2008\/05\/23\/what-does-translateaccelerator-do\/"},"modified":"2008-05-23T07:00:00","modified_gmt":"2008-05-23T07:00:00","slug":"what-does-translateaccelerator-do","status":"publish","type":"post","link":"https:\/\/devblogs.microsoft.com\/oldnewthing\/20080523-00\/?p=22203","title":{"rendered":"What does TranslateAccelerator do?"},"content":{"rendered":"<p>\nFor some reason, there appears to be some confusion over\nwhat <code>TranslateAccelerator<\/code> does.\nIt&#8217;s very simple, and\n<a HREF=\"http:\/\/msdn2.microsoft.com\/en-us\/ms646373.aspx\">\nit&#8217;s all spelled out in the documentation<\/a>.\nYou give it a message, and if the message is a keypress that\nmatches an entry in the accelerator table,\nthe corresponding <code>WM_COMMAND<\/code> or <code>WM_SYSCOMMAND<\/code>\nmessage is sent to the window you said you are translating messages for.\n<\/p>\n<p>\nOne point of confusion is which window receives the translated\nmessage.\nIs it the window in the <code>MSG<\/code> structure or the window\npassed as the first parameter to\n<code>TranslateAccelerator<\/code>?\nThis confusion shouldn&#8217;t last long, though, because of the two options,\none of them raises more questions than it resolves.\n<\/p>\n<ul>\n<li>If it went to the window in the <code>MSG<\/code> structure,\n    then there would be no need for a\n    <code>hWnd<\/code> parameter to\n    <code>TranslateAccelerator<\/code>,\n    since it wouldn&#8217;t be used for anything.<\/p>\n<li>If it went to the window in the <code>MSG<\/code> structure,\n    there wouldn&#8217;t be much need to have a <code>TranslateAccelerator<\/code>\n    function anyway, because even without it,\n    the message would have gone to the window in the <code>MSG<\/code>\n    structure anyway.\n<\/ul>\n<p>\nIf the message matches an accelerator,\nthe <code>WM_COMMAND<\/code> or <code>WM_SYSCOMMAND<\/code> message\ngoes to the window you passed as the first parameter to\n<code>TranslateAccelerator<\/code>.\n<\/p>\n<p>\nOnce you understand this, you can answer the next question:\n<\/p>\n<blockquote CLASS=\"q\"><p>\nMy program has two dialogs containing tabs, both running on the\nsame UI thread.\nI want to make sure that the Ctrl+Tab hotkey switches between the\ntabs on the correct dialog.\nI&#8217;ve already created an accelerator table,\nbut how do I decide which window to pass as the first parameter\nto <code>TranslateAccelerator<\/code>?\nRight now, I&#8217;m using <code>GetForegroundWindow()<\/code>.\n<\/p><\/blockquote>\n<p>\nWell, first off, <code>GetForegroundWindow()<\/code> is completely\nwrong, since the foreground window need not be one of your two\ndialogs.\nThe user may be working with Notepad,\nand now you sent a <code>WM_COMMAND<\/code> to Notepad with one\nof your private command codes,\na command code that has a completely different meaning to Notepad.\n<\/p>\n<p>\nOkay, back to the question.\nHow do you know which window to pass as the first parameter\nto <code>TranslateAccelerator<\/code>?\nWell, it&#8217;s the window that you want the <code>WM_COMMAND<\/code>\nmessage to go to:\nYou want it to go to the dialog that contains the window\nthat the user typed Ctrl+Tab into.\nYou can determine this window by looking at the <code>MSG<\/code>\nstructure, since keyboard messages are delivered to the window\nwith keyboard focus.\n<\/p>\n<pre>\nif (IsChild(hwnd1, msg.hwnd))\n    TranslateAccelerator(hwnd1, hAccel, &amp;msg);\nelse if (IsChild(hwnd2, msg.hwnd))\n    TranslateAccelerator(hwnd2, hAccel, &amp;msg);\n<\/pre>\n<p>\nThe window handle in the <code>MSG<\/code> structure tells\nyou which window the user typed the key into;\nif that window is part of the first dialog,\nthen translate the message and send any translated message\nto the first dialog.\nOtherwise, check the same thing with the second dialog.\n<\/p>\n<p>\nEasy as pie.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>For some reason, there appears to be some confusion over what TranslateAccelerator does. It&#8217;s very simple, and it&#8217;s all spelled out in the documentation. You give it a message, and if the message is a keypress that matches an entry in the accelerator table, the corresponding WM_COMMAND or WM_SYSCOMMAND message is sent to the window [&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-22203","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-oldnewthing","tag-code"],"acf":[],"blog_post_summary":"<p>For some reason, there appears to be some confusion over what TranslateAccelerator does. It&#8217;s very simple, and it&#8217;s all spelled out in the documentation. You give it a message, and if the message is a keypress that matches an entry in the accelerator table, the corresponding WM_COMMAND or WM_SYSCOMMAND message is sent to the window [&hellip;]<\/p>\n","_links":{"self":[{"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/posts\/22203","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=22203"}],"version-history":[{"count":0,"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/posts\/22203\/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=22203"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/categories?post=22203"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/tags?post=22203"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}