{"id":355,"date":"2020-12-13T12:39:28","date_gmt":"2020-12-13T16:39:28","guid":{"rendered":"http:\/\/blog.labhackers.com\/?p=355"},"modified":"2020-12-21T12:23:59","modified_gmt":"2020-12-21T16:23:59","slug":"generating-keyboard-events-using-millikey-usb-serial-commands","status":"publish","type":"post","link":"https:\/\/blog.labhackers.com\/?p=355","title":{"rendered":"Generating Keyboard Events using MilliKey USB Serial Commands"},"content":{"rendered":"\n<p><sup>First posted February 27, 2019.<\/sup><\/p>\n\n\n\n<p>LabHackers\u2019 MilliKey and USB2TTL8 devices can generate a USB Keyboard event after receiving a USB serial command from the software it is connected to.<\/p>\n\n\n\n<p>This post explains the LabHackers\u2019 KGEN command and illustrates how to use it in a simple Python script.<\/p>\n\n\n\n<figure class=\"wp-block-embed is-type-video is-provider-youtube wp-block-embed-youtube wp-embed-aspect-4-3 wp-has-aspect-ratio\"><div class=\"wp-block-embed__wrapper\">\n<iframe loading=\"lazy\" title=\"Using MilliKey KGEN command to generate keyboard events\" width=\"750\" height=\"563\" src=\"https:\/\/www.youtube.com\/embed\/nnRVUDEXzgQ?feature=oembed\" frameborder=\"0\" allow=\"accelerometer; autoplay; clipboard-write; encrypted-media; gyroscope; picture-in-picture\" allowfullscreen><\/iframe>\n<\/div><\/figure>\n\n\n\n<h2 class=\"wp-block-heading\">KGEN Serial Command<\/h2>\n\n\n\n<p>Both the MilliKey and USB2TTL8 support the KGEN serial command. The KGEN command tells the device to generate a key press event for the specified key and duration. The USB keyboard event is identical an event generated from an actual MilliKey button press \u2013 release.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Format<\/h3>\n\n\n\n<p>KGEN key duration [offset]\\n<\/p>\n\n\n\n<p>where:<\/p>\n\n\n\n<ul class=\"wp-block-list\"><li>key: The key to use for the event. A, B, C, \u2026<\/li><li>duration: Number&nbsp;<strong>milliseconds&nbsp;<\/strong>between when device sends key press and release events.<\/li><li>offset: Optional number of&nbsp;<strong>microseconds&nbsp;<\/strong>device will wait before sending the key press event. 0 = send as soon as KGEN command is received \/ processed. Default is 0 usec.<\/li><\/ul>\n\n\n\n<p>For example, send the following serial message to your LabHackers\u2019 device to have it generate a \u2018z\u2019 key press for 123 milliseconds:<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">KGEN Z 123\\n<\/pre>\n\n\n\n<p>or, to press the Up arrow key for 300 milliseconds, 2.5 milliseconds after receiving the command:<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">KGEN UP 300 2500\\n<\/pre>\n\n\n\n<h2 class=\"wp-block-heading\">Python Example<\/h2>\n\n\n\n<p>To keep this example as short as possible, it uses KGEN to generate keyboard events but does \/not\/ have code to collect them.<\/p>\n\n\n\n<p>To \u2018see\u2019 the keyboard events generated from this example, start the example and immediately switch focus to a text editor window so that you can see the keyboard events in action.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code lang=\"python\" class=\"language-python\">#!\/usr\/bin\/env python\n# -*- coding: utf-8 -*-\nfrom __future__ import division, print_function\n\nimport serial\nimport time\n\n# Serial Port of LabHackers' Device.\nserial_address = 'COM138'\n#serial_address = '\/dev\/cu.usbmodem3775821'\n\n# press duration\npress_duration = 250\n\n# seconds to sleep between key press\niti = 0.5\n\n# List of characters to use in KGEN.\nkey_list = \"these key events are KGEN generated.\"\n\n# Open USB Serial connection to LabHackers' device\ntry:\n    s = serial.Serial(serial_address, baudrate=128000, timeout=0.1)\nexcept serial.SerialException as e:\n    print(\"Check SERIAL_PORT variable is correct for device being tested.\")\n    raise e\n\n# Give user 5 seconds to switch to a text editor program.\ntime.sleep(5.0)\n\nfor key in key_list:\n    # KGEN uses 'SPACE' to indicate ' ' key.\n    if key == ' ':\n        key = 'SPACE'\n\n    # for each character in key_list, tell the LabHackers device to immediately\n    # generate a press event for that key lasting press_duration msec.\n    s.write(\"KGEN {} {} {}\\n\".format(key, press_duration, 0).encode())\n\n    # wait iti seconds before issuing next KGEN command\n    time.sleep(iti)\n\n# close serial connection\ns.close()<\/code><\/pre>\n\n\n\n<p>In an upcoming post we will use the KGEN command to test keyboard event latency and event time stamping accuracy using&nbsp;<a href=\"http:\/\/www.psychopy.org\/\" data-type=\"URL\" data-id=\"http:\/\/www.psychopy.org\/\" target=\"_blank\" rel=\"noreferrer noopener\">PsychoPy<\/a>.<\/p>\n\n\n\n<p>Happy Hacking!<\/p>\n","protected":false},"excerpt":{"rendered":"<p>First posted February 27, 2019. LabHackers\u2019 MilliKey and USB2TTL8 devices can generate a USB Keyboard event after receiving a USB serial command from the software it is connected to. This post explains the LabHackers\u2019 KGEN command and illustrates how to use it in a simple Python script. KGEN Serial Command Both the MilliKey and USB2TTL8&hellip; <a class=\"more-link\" href=\"https:\/\/blog.labhackers.com\/?p=355\">Continue reading <span class=\"screen-reader-text\">Generating Keyboard Events using MilliKey USB Serial Commands<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"spay_email":"","footnotes":""},"categories":[5,4,6],"tags":[16,8,10,9],"class_list":["post-355","post","type-post","status-publish","format-standard","hentry","category-how-to","category-millikey-response-box","category-usb2ttl8-adaptor","tag-keyboard-events","tag-millikey","tag-python","tag-usb2ttl8","entry"],"jetpack_featured_media_url":"","_links":{"self":[{"href":"https:\/\/blog.labhackers.com\/index.php?rest_route=\/wp\/v2\/posts\/355","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/blog.labhackers.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/blog.labhackers.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/blog.labhackers.com\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/blog.labhackers.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=355"}],"version-history":[{"count":6,"href":"https:\/\/blog.labhackers.com\/index.php?rest_route=\/wp\/v2\/posts\/355\/revisions"}],"predecessor-version":[{"id":445,"href":"https:\/\/blog.labhackers.com\/index.php?rest_route=\/wp\/v2\/posts\/355\/revisions\/445"}],"wp:attachment":[{"href":"https:\/\/blog.labhackers.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=355"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/blog.labhackers.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=355"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/blog.labhackers.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=355"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}