{"id":90871,"date":"2015-07-20T07:00:00","date_gmt":"2015-07-20T21:00:00","guid":{"rendered":"https:\/\/blogs.msdn.microsoft.com\/oldnewthing\/20150720-00\/?p=90871\/"},"modified":"2019-03-13T12:17:28","modified_gmt":"2019-03-13T19:17:28","slug":"20150720-00","status":"publish","type":"post","link":"https:\/\/devblogs.microsoft.com\/oldnewthing\/20150720-00\/?p=90871","title":{"rendered":"How can I detect whether a keyboard is attached to the computer?"},"content":{"rendered":"<p>Today&#8217;s Little Program tells you whether a keyboard is attached to the computer. The short answer is &#8220;Enumerate the raw input devices and see if any of them is a keyboard.&#8221; <\/p>\n<p>Remember: Little Programs don&#8217;t worry about silly things like race conditions. <\/p>\n<\/p>\n<pre>\n#include &lt;windows.h&gt;\n#include &lt;iostream&gt;\n#include &lt;vector&gt;\n#include &lt;algorithm&gt;\n\nbool IsKeyboardPresent()\n{\n UINT numDevices = 0;\n  if (GetRawInputDeviceList(nullptr, &amp;numDevices,\n                            sizeof(RAWINPUTDEVICELIST)) != 0) {\n   throw GetLastError();\n }\n\n std::vector&lt;RAWINPUTDEVICELIST&gt; devices(numDevices);\n\n if (GetRawInputDeviceList(&amp;devices[0], &amp;numDevices,\n                           sizeof(RAWINPUTDEVICELIST)) == (UINT)-1) {\n  throw GetLastError();\n }\n\n return std::find_if(devices.begin(), devices.end(),\n    [](RAWINPUTDEVICELIST&amp; device)\n    { return device.dwType == RIM_TYPEKEYBOARD; }) != devices.end();\n}\n\nint __cdecl main(int, char**)\n{\n std::cout &lt;&lt; IsKeyboardPresent() &lt;&lt; std::endl;\n return 0;\n}\n<\/pre>\n<p>There is a race condition in this code if the number of devices changes between the two calls to <code>Get&shy;Raw&shy;Input&shy;Device&shy;List<\/code>. I will leave you to fix it before incorporating this code into your program. <\/p>\n","protected":false},"excerpt":{"rendered":"<p>Look at the available raw input devices.<\/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-90871","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-oldnewthing","tag-code"],"acf":[],"blog_post_summary":"<p>Look at the available raw input devices.<\/p>\n","_links":{"self":[{"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/posts\/90871","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=90871"}],"version-history":[{"count":0,"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/posts\/90871\/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=90871"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/categories?post=90871"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/tags?post=90871"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}