{"id":13197,"date":"2023-10-30T12:23:37","date_gmt":"2023-10-30T12:23:37","guid":{"rendered":"https:\/\/www.qec.tw\/?p=13197"},"modified":"2023-10-30T12:23:40","modified_gmt":"2023-10-30T12:23:40","slug":"keyboard-controller-example","status":"publish","type":"post","link":"https:\/\/www.qec.tw\/zh\/86duino-reference\/usb\/keyboard-controller-example\/","title":{"rendered":"\u9375\u76e4\u63a7\u5236\u5668\u7bc4\u4f8b"},"content":{"rendered":"<div class=\"wp-block-codemirror-blocks-code-block code-block\"><pre>\/*\n Keyboard Controller Example\n \n Shows the output of a USB Keyboard connected to \n the Native USB port on an Arduino Due Board.\n \n created 8 Oct 2012\n by Cristian Maglie\n \n http:\/\/arduino.cc\/en\/Tutorial\/KeyboardController\n \n This sample code is part of the public domain.\n *\/\n\n\n\/\/ Require keyboard control library\n#include &lt;KeyboardController.h&gt;\n\/\/#include &lt;Ethernet.h&gt;\n\n\/\/ Initialize USB Controller\nUSBHost usb;\n\n\/\/ Attach keyboard controller to USB\nKeyboardController keyboard(usb);\n\n\/\/ This function intercepts key press\nvoid keyPressed() {\n  Serial.print(&quot;Pressed:  &quot;);\n  printKey();\n}\n\n\/\/ This function intercepts key release\nvoid keyReleased() {\n  Serial.print(&quot;Released: &quot;);\n  printKey();\n}\n\nvoid printKey() {\n  \/\/ getOemKey() returns the OEM-code associated with the key\n  Serial.print(&quot; key:&quot;);\n  Serial.print(keyboard.getOemKey());\n\n  \/\/ getModifiers() returns a bits field with the modifiers-keys\n  int mod = keyboard.getModifiers();\n  Serial.print(&quot; mod:&quot;);\n  Serial.print(mod);\n\n  Serial.print(&quot; =&gt; &quot;);\n\n  if (mod &amp; LeftCtrl)\n    Serial.print(&quot;L-Ctrl &quot;);\n  if (mod &amp; LeftShift)\n    Serial.print(&quot;L-Shift &quot;);\n  if (mod &amp; Alt)\n    Serial.print(&quot;Alt &quot;);\n  if (mod &amp; LeftCmd)\n    Serial.print(&quot;L-Cmd &quot;);\n  if (mod &amp; RightCtrl)\n    Serial.print(&quot;R-Ctrl &quot;);\n  if (mod &amp; RightShift)\n    Serial.print(&quot;R-Shift &quot;);\n  if (mod &amp; AltGr)\n    Serial.print(&quot;AltGr &quot;);\n  if (mod &amp; RightCmd)\n    Serial.print(&quot;R-Cmd &quot;);\n\n  \/\/ getKey() returns the ASCII translation of OEM key\n  \/\/ combined with modifiers.\n  Serial.write(keyboard.getKey());\n  Serial.println();\n}\n\nvoid setup()\n{\n  Serial.begin(9600);\n  Serial.println(&quot;Program started&quot;);\n  delay(200);\n}\n\nvoid loop()\n{\n  \/\/ Process USB tasks\n  usb.Task();\n}<\/pre><\/div>","protected":false},"excerpt":{"rendered":"","protected":false},"author":1,"featured_media":13198,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"site-sidebar-layout":"default","site-content-layout":"","ast-site-content-layout":"default","site-content-style":"default","site-sidebar-style":"default","ast-global-header-display":"","ast-banner-title-visibility":"","ast-main-header-display":"","ast-hfb-above-header-display":"","ast-hfb-below-header-display":"","ast-hfb-mobile-header-display":"","site-post-title":"","ast-breadcrumbs-content":"","ast-featured-img":"","footer-sml-layout":"","ast-disable-related-posts":"","theme-transparent-header-meta":"","adv-header-id-meta":"","stick-header-meta":"","header-above-stick-meta":"","header-main-stick-meta":"","header-below-stick-meta":"","astra-migrate-meta-layouts":"set","ast-page-background-enabled":"default","ast-page-background-meta":{"desktop":{"background-color":"","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""},"tablet":{"background-color":"","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""},"mobile":{"background-color":"","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""}},"ast-content-background-meta":{"desktop":{"background-color":"var(--ast-global-color-5)","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""},"tablet":{"background-color":"var(--ast-global-color-5)","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""},"mobile":{"background-color":"var(--ast-global-color-5)","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""}},"footnotes":""},"categories":[215],"tags":[162],"class_list":["post-13197","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-usb","tag-86duino"],"_links":{"self":[{"href":"https:\/\/www.qec.tw\/zh\/wp-json\/wp\/v2\/posts\/13197","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.qec.tw\/zh\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.qec.tw\/zh\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.qec.tw\/zh\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.qec.tw\/zh\/wp-json\/wp\/v2\/comments?post=13197"}],"version-history":[{"count":0,"href":"https:\/\/www.qec.tw\/zh\/wp-json\/wp\/v2\/posts\/13197\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.qec.tw\/zh\/wp-json\/wp\/v2\/media\/13198"}],"wp:attachment":[{"href":"https:\/\/www.qec.tw\/zh\/wp-json\/wp\/v2\/media?parent=13197"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.qec.tw\/zh\/wp-json\/wp\/v2\/categories?post=13197"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.qec.tw\/zh\/wp-json\/wp\/v2\/tags?post=13197"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}