{"id":43963,"date":"2014-09-29T07:00:00","date_gmt":"2014-09-29T07:00:00","guid":{"rendered":"https:\/\/blogs.msdn.microsoft.com\/oldnewthing\/2014\/09\/29\/simulating-media-controller-buttons-like-play-and-pause\/"},"modified":"2014-09-29T07:00:00","modified_gmt":"2014-09-29T07:00:00","slug":"simulating-media-controller-buttons-like-play-and-pause","status":"publish","type":"post","link":"https:\/\/devblogs.microsoft.com\/oldnewthing\/20140929-00\/?p=43963","title":{"rendered":"Simulating media controller buttons like Play and Pause"},"content":{"rendered":"<p>\nToday&#8217;s Little Program simulates pressing the Play\/Pause\nbutton on your fancy keyboard.\nThis might be useful if you want to write a program that converts\nsome other input (say, gesture detection) into media controller events.\n<\/p>\n<p>\nOne way of doing this is to take advantage of the\n<code>Def&shy;Window&shy;Proc<\/code> function,\nsince the default behavior for the\n<code>WM_APP&shy;COMMAND<\/code> message is to pass\nthe message up the parent chain,\nand if it still can&#8217;t find a handler,\nit hands the message to the shell for global processing.\n<\/p>\n<p>\nRemember, don&#8217;t\n<a HREF=\"http:\/\/blogs.msdn.com\/b\/oldnewthing\/archive\/2006\/06\/13\/629451.aspx\">\nfumble around<\/a>.\nIf you want to send a message to a window,\nthen send a message to a window.\nDon&#8217;t broadcast a message to every window in the system\n(resulting in mass chaos).\n<\/p>\n<p>\nTake the scratch program and make this simple addition:\n<\/p>\n<pre>\nvoid OnChar(HWND hwnd, TCHAR ch, int cRepeat)\n{\n if (ch == ' ') {\n  SendMessage(hwnd, WM_APPCOMMAND, (WPARAM)hwnd,\n      MAKELONG(0, FAPPCOMMAND_KEY | APPCOMMAND_MEDIA_PLAY_PAUSE));\n }\n}\n HANDLE_MSG(hwnd, WM_CHAR, OnChar);\n<\/pre>\n<p>\nWhen you press the space bar in the scratch application,\nit pretends that you instead pressed the <i>Play\/Pause<\/i>\nbutton on your fancy keyboard with no shift modifiers.\n<\/p>\n<p>\nThe scratch program doesn&#8217;t do anything with the key,\nso it ends up falling through to\n<code>Def&shy;Window&shy;Proc<\/code>,\nwhich eventually hands the key to the shell and any other\nregistered shell hooks.\nIf you have a program like Windows Media Player\nwhich registers for shell events,\nit will see the notification and pause\/resume playback.\n<\/p>\n<p>\nOf course, this assumes that the program you want to talk to\nlistens globally for the keypress.\nIf you want to make the current foreground program respond\nas if you had pressed the <i>Play\/Pause<\/i>,\nyou can just inject the keypress.\n<\/p>\n<pre>\nint __cdecl main(int, char**)\n{\n INPUT inputs[2] = {};\n inputs[0].type = INPUT_KEYBOARD;\n inputs[0].ki.wVk = VK_MEDIA_PLAY_PAUSE;\n inputs[0].ki.wScan = <a HREF=\"http:\/\/msdn.microsoft.com\/en-us\/windows\/hardware\/gg463446#END\">0x22<\/a>;\n inputs[0].ki.dwFlags = KEYEVENTF_EXTENDEDKEY;\n inputs[1].type = INPUT_KEYBOARD;\n inputs[1].ki.wVk = VK_MEDIA_PLAY_PAUSE;\n inputs[1].ki.wScan = 0x22;\n inputs[0].ki.dwFlags = KEYEVENTF_EXTENDEDKEY | KEYEVENTF_KEYUP;\n SendInput(2, inputs, sizeof(INPUT));\n return 0;\n}\n<\/pre>\n<p>\nNote, however, that since we didn&#8217;t do anything about the\nstate of modifier keys,\nif the user happens to have the shift key down at the time\nyou injected the message,\nthe application is going to be told,\n&#8220;Hey, do your play\/pause thing, and if you change behavior\nwhen the shift key is down, here&#8217;s your chance.&#8221;\n<\/p>\n<p>\nBut what did you expect from a Little Program?<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Today&#8217;s Little Program simulates pressing the Play\/Pause button on your fancy keyboard. This might be useful if you want to write a program that converts some other input (say, gesture detection) into media controller events. One way of doing this is to take advantage of the Def&shy;Window&shy;Proc function, since the default behavior for the WM_APP&shy;COMMAND [&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-43963","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-oldnewthing","tag-code"],"acf":[],"blog_post_summary":"<p>Today&#8217;s Little Program simulates pressing the Play\/Pause button on your fancy keyboard. This might be useful if you want to write a program that converts some other input (say, gesture detection) into media controller events. One way of doing this is to take advantage of the Def&shy;Window&shy;Proc function, since the default behavior for the WM_APP&shy;COMMAND [&hellip;]<\/p>\n","_links":{"self":[{"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/posts\/43963","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=43963"}],"version-history":[{"count":0,"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/posts\/43963\/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=43963"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/categories?post=43963"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/tags?post=43963"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}