{"id":15237,"date":"2021-06-12T23:51:50","date_gmt":"2021-06-12T18:21:50","guid":{"rendered":"https:\/\/java2blog.com\/?p=15237"},"modified":"2021-06-12T23:51:50","modified_gmt":"2021-06-12T18:21:50","slug":"detect-keypress-python","status":"publish","type":"post","link":"https:\/\/java2blog.com\/detect-keypress-python\/","title":{"rendered":"Detect keypress in Python"},"content":{"rendered":"<p>Python allows us to work with user input in its programs. We can also work with hardware devices in Python.<\/p>\n<p>In this article, we will discuss how to detect keypress in Python.<br \/>\n<div id=\"toc_container\" class=\"toc_light_blue no_bullets\"><p class=\"toc_title\">Table of Contents<\/p><ul class=\"toc_list\"><li><a href=\"#Using_the_keyboard_module_to_detect_keypress_in_Python\">Using the keyboard module to detect keypress in Python<\/a><\/li><li><a href=\"#Using_the_pynput_module_to_detect_keypress_in_Python\">Using the pynput module to detect keypress in Python<\/a><\/li><li><a href=\"#Using_the_msvcrt_module_to_detect_keypress_in_Python\">Using the msvcrt module to detect keypress in Python<\/a><\/li><\/ul><\/div>\n\n<h2><span id=\"Using_the_keyboard_module_to_detect_keypress_in_Python\">Using the <code>keyboard<\/code> module to detect keypress in Python<\/span><\/h2>\n<p>The <code>keyboard<\/code> module is well equipped with different functions to perform operations related to keyboard input, detecting-simulating key presses, and more. This module works normally on Windows but requires the device to be rooted on Linux devices. To detect keypress, we can use a few functions from this module.<\/p>\n<p>The <code>read_key()<\/code> function from this module is used to read the key pressed by the user. We can check whether the pressed key matches our specified key. <\/p>\n<p>See the following example.<\/p>\n<pre code = \"python\">\nimport keyboard\nif keyboard.read_key() == \"a\":\n    print(\"A Key Pressed\")    \n<\/pre>\n<p><strong>Output:<\/strong><\/p>\n<div class=\"content-box-purple\">\nA Key Pressed\n<\/div>\n<p>If you get output as below:<\/p>\n<div class=\"content-box-red\">\nModuleNotFoundError: No module named &#8216;keyboard&#8217;\n<\/div>\n<p>You need to install keyboard module first.<br \/>\nYou can use following command to install keyboard module.<\/p>\n<div style=\"padding: 12px; color: #ffffff; background-color: #000000; line-height: 1.4;\">\npip install keyboard\n<\/div>\n<p>The <code>wait()<\/code> function waits for the user to press some specific key and then resume the program execution.<\/p>\n<p>For example,<\/p>\n<pre code = \"python\">\nimport keyboard\nkeyboard.wait(\"a\")\nprint(\"A Key Pressed\")    \n<\/pre>\n<p><strong>Output:<\/strong><\/p>\n<div class=\"content-box-purple\">\nA Key Pressed\n<\/div>\n<p>We can also use the <code>is_pressed()<\/code> function to check whether a specific key is pressed or not. It will return a True or False value.<\/p>\n<p>For example,<\/p>\n<pre code = \"python\">\nimport keyboard\nwhile True:\n    if keyboard.is_pressed(\"a\"):\n        print(keyboard.is_pressed('a'))\n        print(\"A Key Pressed\")\n        break    \n<\/pre>\n<p><strong>Output:<\/strong><\/p>\n<div class=\"content-box-purple\">\nTrue<br \/>\nA Key Pressed\n<\/div>\n<p>The <code>on_press_key()<\/code> function can execute a function when a specific key is pressed. We have to pass the function and the required key as parameters to this function.<\/p>\n<p>We will use this function in the following code.<\/p>\n<pre code = \"python\">\nimport keyboard\nkeyboard.on_press_key(\"a\", lambda _:print(\"A Key Pressed\"))    \n<\/pre>\n<p><strong>Output:<\/strong><\/p>\n<div class=\"content-box-purple\">\nA Key Pressed\n<\/div>\n<h2><span id=\"Using_the_pynput_module_to_detect_keypress_in_Python\">Using the <code>pynput<\/code> module to detect keypress in Python<\/span><\/h2>\n<p>The <code>pynput<\/code> module allows us to work with different input devices like keyboard and mouse.<\/p>\n<p>We will create two functions that will be collected using the <code>Listener<\/code> function of this module.<\/p>\n<p>See the following code.<\/p>\n<pre code = \"python\">\nfrom pynput.keyboard import Key, Listener\ndef press(key):\n    print(key)\ndef release(key):\n    if key == Key.space:\n        return False #Returns False to stop the listener\n##If spacebar is pressed it will stop\nwith Listener( \n        on_press=press,\n        on_release=release) as listener:\n    listener.join()    \n<\/pre>\n<p><strong>Output:<\/strong><\/p>\n<div class=\"content-box-purple\">\n&#8216;a&#8217;<br \/>\n&#8216;b&#8217;<br \/>\nKey.space\n<\/div>\n<p>There are a lot of things happening in the above example.<\/p>\n<ul>\n<li>The <code>press()<\/code> function will be executed when we press the key and print the key.<\/li>\n<li>Similarly, the <code>on_release()<\/code> function will be executed when the key is released.<\/li>\n<li>These two functions are collected in the <code>Listener()<\/code> function using the <code>on_press<\/code> and <code>on_release<\/code> parameters.<\/li>\n<li>We can also stop the <code>Listener()<\/code> function by pressing some specific key. In our example, if the spacebar is pressed, the <code>release<\/code> function will return False and the <code>Listener()<\/code> function will stop.<\/li>\n<\/ul>\n<h2><span id=\"Using_the_msvcrt_module_to_detect_keypress_in_Python\">Using the <code>msvcrt<\/code> module to detect keypress in Python<\/span><\/h2>\n<p>The <code>msvcrt<\/code> is another Windows-only module that can detect keypresses.<\/p>\n<p>We will detect a keypress and print that key using the following code.<\/p>\n<pre code = \"python\">\nimport msvcrt\nwhile True:\n    if msvcrt.kbhit():\n        k = msvcrt.getch()\n        print(\"Key Pressed\", k)\n        break    \n<\/pre>\n<div class=\"content-box-purple\">\nKey Pressed b&#8217;r&#8217;\n<\/div>\n<p>In the above example,<\/p>\n<ul>\n<li>The <code>kbhit()<\/code> function waits for a key to be pressed and returns True when it is pressed.<\/li>\n<li>The <code>getch()<\/code> function returns the key pressed in a byte string. Notice the <code>b<\/code> in the output.<\/li>\n<li>The <code>break<\/code> statement will come out of this code block. If it is removed, then the code will keep on executing.<br \/>\nThat&#8217;s all about how to detect keypress in Python.<\/li>\n<\/ul>\n","protected":false},"excerpt":{"rendered":"<p>Table of ContentsUsing the keyboard module to detect keypress in PythonUsing the pynput module to detect keypress in PythonUsing the msvcrt module to detect keypress in Python Python allows us to work with user input in its programs. We can also work with hardware devices in Python. In this article, we will discuss how to [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":15249,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"inline_featured_image":false,"_mi_skip_tracking":false},"categories":[145],"tags":[],"_links":{"self":[{"href":"https:\/\/java2blog.com\/wp-json\/wp\/v2\/posts\/15237"}],"collection":[{"href":"https:\/\/java2blog.com\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/java2blog.com\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/java2blog.com\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/java2blog.com\/wp-json\/wp\/v2\/comments?post=15237"}],"version-history":[{"count":0,"href":"https:\/\/java2blog.com\/wp-json\/wp\/v2\/posts\/15237\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/java2blog.com\/wp-json\/wp\/v2\/media\/15249"}],"wp:attachment":[{"href":"https:\/\/java2blog.com\/wp-json\/wp\/v2\/media?parent=15237"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/java2blog.com\/wp-json\/wp\/v2\/categories?post=15237"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/java2blog.com\/wp-json\/wp\/v2\/tags?post=15237"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}