{"id":968474,"date":"2024-12-27T05:07:27","date_gmt":"2024-12-26T21:07:27","guid":{"rendered":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/968474.html"},"modified":"2024-12-27T05:07:29","modified_gmt":"2024-12-26T21:07:29","slug":"python%e5%a6%82%e4%bd%95%e5%90%8c%e6%97%b6%e6%8d%95%e6%8d%89%e9%94%ae%e7%9b%98","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/968474.html","title":{"rendered":"python\u5982\u4f55\u540c\u65f6\u6355\u6349\u952e\u76d8"},"content":{"rendered":"<p style=\"text-align:center;\" ><img decoding=\"async\" src=\"https:\/\/cdn-kb.worktile.com\/kb\/wp-content\/uploads\/2024\/04\/24183234\/9027e2dd-77d3-4316-944a-1d33e28d1a45.webp\" alt=\"python\u5982\u4f55\u540c\u65f6\u6355\u6349\u952e\u76d8\" \/><\/p>\n<p><p> <strong>\u5728Python\u4e2d\uff0c\u53ef\u4ee5\u901a\u8fc7\u591a\u7ebf\u7a0b\u3001\u591a\u8fdb\u7a0b\u548c\u952e\u76d8\u76d1\u542c\u5e93\u6765\u5b9e\u73b0\u540c\u65f6\u6355\u6349\u952e\u76d8\u4e8b\u4ef6\u3001\u4f7f\u7528\u5e93\u5982<code>pynput<\/code>\u3001<code>keyboard<\/code>\u3002<\/strong>\u4e0b\u9762\u5c06\u8be6\u7ec6\u4ecb\u7ecd\u5982\u4f55\u4f7f\u7528\u8fd9\u4e9b\u65b9\u6cd5\u4ee5\u53ca\u5176\u4f18\u7f3a\u70b9\u3002<\/p>\n<\/p>\n<p><p>\u4e00\u3001\u591a\u7ebf\u7a0b\u4e0e\u591a\u8fdb\u7a0b<\/p>\n<\/p>\n<p><p>\u5728\u9700\u8981\u540c\u65f6\u6355\u6349\u952e\u76d8\u4e8b\u4ef6\u65f6\uff0c\u591a\u7ebf\u7a0b\u548c\u591a\u8fdb\u7a0b\u662f\u4e24\u79cd\u5e38\u89c1\u7684\u9009\u62e9\u3002<\/p>\n<\/p>\n<ol>\n<li><strong>\u591a\u7ebf\u7a0b<\/strong><\/li>\n<\/ol>\n<p><p>\u591a\u7ebf\u7a0b\u5141\u8bb8\u7a0b\u5e8f\u540c\u65f6\u8fd0\u884c\u591a\u4e2a\u64cd\u4f5c\uff0c\u8fd9\u5728\u6355\u6349\u952e\u76d8\u4e8b\u4ef6\u65f6\u975e\u5e38\u6709\u7528\u3002Python\u7684<code>threading<\/code>\u5e93\u63d0\u4f9b\u4e86\u7b80\u5355\u7684\u591a\u7ebf\u7a0b\u652f\u6301\u3002\u4ee5\u4e0b\u662f\u4e00\u4e2a\u7b80\u5355\u7684\u793a\u4f8b\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import threading<\/p>\n<p>import time<\/p>\n<p>def keyboard_listener():<\/p>\n<p>    while True:<\/p>\n<p>        # \u6a21\u62df\u952e\u76d8\u76d1\u542c<\/p>\n<p>        print(&quot;Listening for keyboard events...&quot;)<\/p>\n<p>        time.sleep(1)<\/p>\n<p>def another_task():<\/p>\n<p>    while True:<\/p>\n<p>        # \u6267\u884c\u5176\u4ed6\u4efb\u52a1<\/p>\n<p>        print(&quot;Performing another task...&quot;)<\/p>\n<p>        time.sleep(2)<\/p>\n<h2><strong>\u521b\u5efa\u7ebf\u7a0b<\/strong><\/h2>\n<p>listener_thread = threading.Thread(target=keyboard_listener)<\/p>\n<p>task_thread = threading.Thread(target=another_task)<\/p>\n<h2><strong>\u542f\u52a8\u7ebf\u7a0b<\/strong><\/h2>\n<p>listener_thread.start()<\/p>\n<p>task_thread.start()<\/p>\n<h2><strong>\u7b49\u5f85\u7ebf\u7a0b\u5b8c\u6210<\/strong><\/h2>\n<p>listener_thread.join()<\/p>\n<p>task_thread.join()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0c<code>keyboard_listener<\/code>\u548c<code>another_task<\/code>\u51fd\u6570\u5728\u4e0d\u540c\u7684\u7ebf\u7a0b\u4e2d\u540c\u65f6\u8fd0\u884c\uff0c\u4f7f\u5f97\u7a0b\u5e8f\u80fd\u591f\u540c\u65f6\u76d1\u542c\u952e\u76d8\u4e8b\u4ef6\u548c\u6267\u884c\u5176\u4ed6\u4efb\u52a1\u3002<\/p>\n<\/p>\n<p><p><strong>\u4f18\u70b9\uff1a<\/strong> <\/p>\n<\/p>\n<ul>\n<li>\u5728I\/O\u5bc6\u96c6\u578b\u4efb\u52a1\u4e2d\uff0c\u591a\u7ebf\u7a0b\u53ef\u4ee5\u6709\u6548\u63d0\u9ad8\u7a0b\u5e8f\u6548\u7387\u3002<\/li>\n<li>\u5b9e\u73b0\u7b80\u5355\uff0c\u9002\u5408\u8f7b\u91cf\u7ea7\u4efb\u52a1\u3002<\/li>\n<\/ul>\n<p><p><strong>\u7f3a\u70b9\uff1a<\/strong><\/p>\n<\/p>\n<ul>\n<li>\u5728CPU\u5bc6\u96c6\u578b\u4efb\u52a1\u4e2d\uff0cPython\u7684GIL\uff08\u5168\u5c40\u89e3\u91ca\u5668\u9501\uff09\u9650\u5236\u4e86\u591a\u7ebf\u7a0b\u7684\u6027\u80fd\u63d0\u5347\u3002<\/li>\n<li>\u9700\u8981\u5904\u7406\u7ebf\u7a0b\u5b89\u5168\u95ee\u9898\u3002<\/li>\n<\/ul>\n<ol start=\"2\">\n<li><strong>\u591a\u8fdb\u7a0b<\/strong><\/li>\n<\/ol>\n<p><p>\u591a\u8fdb\u7a0b\u901a\u8fc7\u5728\u4e0d\u540c\u7684\u8fdb\u7a0b\u4e2d\u8fd0\u884c\u4efb\u52a1\u6765\u89c4\u907fGIL\u7684\u9650\u5236\uff0c\u9002\u5408CPU\u5bc6\u96c6\u578b\u4efb\u52a1\u3002Python\u7684<code>multiprocessing<\/code>\u5e93\u53ef\u4ee5\u5b9e\u73b0\u591a\u8fdb\u7a0b\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import multiprocessing<\/p>\n<p>import time<\/p>\n<p>def keyboard_listener():<\/p>\n<p>    while True:<\/p>\n<p>        # \u6a21\u62df\u952e\u76d8\u76d1\u542c<\/p>\n<p>        print(&quot;Listening for keyboard events...&quot;)<\/p>\n<p>        time.sleep(1)<\/p>\n<p>def another_task():<\/p>\n<p>    while True:<\/p>\n<p>        # \u6267\u884c\u5176\u4ed6\u4efb\u52a1<\/p>\n<p>        print(&quot;Performing another task...&quot;)<\/p>\n<p>        time.sleep(2)<\/p>\n<h2><strong>\u521b\u5efa\u8fdb\u7a0b<\/strong><\/h2>\n<p>listener_process = multiprocessing.Process(target=keyboard_listener)<\/p>\n<p>task_process = multiprocessing.Process(target=another_task)<\/p>\n<h2><strong>\u542f\u52a8\u8fdb\u7a0b<\/strong><\/h2>\n<p>listener_process.start()<\/p>\n<p>task_process.start()<\/p>\n<h2><strong>\u7b49\u5f85\u8fdb\u7a0b\u5b8c\u6210<\/strong><\/h2>\n<p>listener_process.join()<\/p>\n<p>task_process.join()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p><strong>\u4f18\u70b9\uff1a<\/strong><\/p>\n<\/p>\n<ul>\n<li>\u53ef\u4ee5\u7ed5\u8fc7GIL\uff0c\u9002\u5408CPU\u5bc6\u96c6\u578b\u4efb\u52a1\u3002<\/li>\n<li>\u8fdb\u7a0b\u4e4b\u95f4\u4e92\u4e0d\u5e72\u6270\uff0c\u63d0\u9ad8\u7a0b\u5e8f\u7a33\u5b9a\u6027\u3002<\/li>\n<\/ul>\n<p><p><strong>\u7f3a\u70b9\uff1a<\/strong><\/p>\n<\/p>\n<ul>\n<li>\u521b\u5efa\u8fdb\u7a0b\u7684\u5f00\u9500\u8f83\u5927\u3002<\/li>\n<li>\u8fdb\u7a0b\u95f4\u901a\u4fe1\u590d\u6742\uff0c\u9700\u8981\u4f7f\u7528\u961f\u5217\u3001\u7ba1\u9053\u7b49\u3002<\/li>\n<\/ul>\n<p><p>\u4e8c\u3001\u952e\u76d8\u76d1\u542c\u5e93<\/p>\n<\/p>\n<p><p>\u4e3a\u4e86\u66f4\u65b9\u4fbf\u5730\u6355\u6349\u952e\u76d8\u4e8b\u4ef6\uff0c\u53ef\u4ee5\u4f7f\u7528\u4e13\u95e8\u7684\u952e\u76d8\u76d1\u542c\u5e93\uff0c\u5982<code>pynput<\/code>\u548c<code>keyboard<\/code>\u3002<\/p>\n<\/p>\n<ol>\n<li><strong>pynput<\/strong><\/li>\n<\/ol>\n<p><p><code>pynput<\/code>\u662f\u4e00\u4e2a\u529f\u80fd\u5f3a\u5927\u7684\u5e93\uff0c\u652f\u6301\u76d1\u542c\u548c\u63a7\u5236\u952e\u76d8\u548c\u9f20\u6807\u3002<\/p>\n<\/p>\n<p><p>\u5b89\u88c5<code>pynput<\/code>\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-bash\">pip install pynput<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u4e0b\u9762\u662f\u4e00\u4e2a\u4f7f\u7528<code>pynput<\/code>\u76d1\u542c\u952e\u76d8\u4e8b\u4ef6\u7684\u793a\u4f8b\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">from pynput import keyboard<\/p>\n<p>def on_press(key):<\/p>\n<p>    try:<\/p>\n<p>        print(&#39;Key {0} pressed.&#39;.format(key.char))<\/p>\n<p>    except AttributeError:<\/p>\n<p>        print(&#39;Special key {0} pressed.&#39;.format(key))<\/p>\n<p>def on_release(key):<\/p>\n<p>    print(&#39;Key {0} released.&#39;.format(key))<\/p>\n<p>    if key == keyboard.Key.esc:<\/p>\n<p>        # \u505c\u6b62\u76d1\u542c<\/p>\n<p>        return False<\/p>\n<h2><strong>\u542f\u52a8\u76d1\u542c\u5668<\/strong><\/h2>\n<p>with keyboard.Listener(on_press=on_press, on_release=on_release) as listener:<\/p>\n<p>    listener.join()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0c\u7a0b\u5e8f\u4f1a\u76d1\u542c\u952e\u76d8\u6309\u952e\u4e8b\u4ef6\uff0c\u5e76\u5728\u6309\u4e0b\u6216\u91ca\u653e\u6309\u952e\u65f6\u8c03\u7528\u56de\u8c03\u51fd\u6570\u3002<\/p>\n<\/p>\n<p><p><strong>\u4f18\u70b9\uff1a<\/strong><\/p>\n<\/p>\n<ul>\n<li>\u4f7f\u7528\u7b80\u5355\uff0c\u529f\u80fd\u5f3a\u5927\u3002<\/li>\n<li>\u652f\u6301\u76d1\u542c\u952e\u76d8\u548c\u9f20\u6807\u4e8b\u4ef6\u3002<\/li>\n<\/ul>\n<p><p><strong>\u7f3a\u70b9\uff1a<\/strong><\/p>\n<\/p>\n<ul>\n<li>\u9700\u8981\u5b89\u88c5\u7b2c\u4e09\u65b9\u5e93\u3002<\/li>\n<\/ul>\n<ol start=\"2\">\n<li><strong>keyboard<\/strong><\/li>\n<\/ol>\n<p><p><code>keyboard<\/code>\u5e93\u662f\u53e6\u4e00\u4e2a\u7528\u4e8e\u6355\u6349\u952e\u76d8\u4e8b\u4ef6\u7684\u5e93\uff0c\u63d0\u4f9b\u4e86\u7b80\u5355\u7684\u63a5\u53e3\u3002<\/p>\n<\/p>\n<p><p>\u5b89\u88c5<code>keyboard<\/code>\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-bash\">pip install keyboard<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u4ee5\u4e0b\u662f\u4e00\u4e2a\u4f7f\u7528<code>keyboard<\/code>\u5e93\u76d1\u542c\u952e\u76d8\u4e8b\u4ef6\u7684\u793a\u4f8b\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import keyboard<\/p>\n<p>def on_key_event(event):<\/p>\n<p>    print(&#39;Event name: {0}, Event scan_code: {1}&#39;.format(event.name, event.scan_code))<\/p>\n<h2><strong>\u6ce8\u518c\u952e\u76d8\u4e8b\u4ef6<\/strong><\/h2>\n<p>keyboard.hook(on_key_event)<\/p>\n<h2><strong>\u963b\u585e\u4e3b\u7ebf\u7a0b\uff0c\u4fdd\u6301\u76d1\u542c<\/strong><\/h2>\n<p>keyboard.w<a href=\"https:\/\/docs.pingcode.com\/blog\/59162.html\" target=\"_blank\">AI<\/a>t(&#39;esc&#39;)  # \u6309\u4e0besc\u952e\u9000\u51fa<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p><strong>\u4f18\u70b9\uff1a<\/strong><\/p>\n<\/p>\n<ul>\n<li>\u63a5\u53e3\u7b80\u5355\u6613\u7528\u3002<\/li>\n<li>\u652f\u6301\u5168\u5c40\u70ed\u952e\u3002<\/li>\n<\/ul>\n<p><p><strong>\u7f3a\u70b9\uff1a<\/strong><\/p>\n<\/p>\n<ul>\n<li>\u9700\u8981\u5b89\u88c5\u7b2c\u4e09\u65b9\u5e93\u3002<\/li>\n<li>\u53ef\u80fd\u5b58\u5728\u517c\u5bb9\u6027\u95ee\u9898\u3002<\/li>\n<\/ul>\n<p><p>\u4e09\u3001\u7efc\u5408\u5e94\u7528<\/p>\n<\/p>\n<p><p>\u5728\u5b9e\u9645\u5e94\u7528\u4e2d\uff0c\u53ef\u80fd\u9700\u8981\u7ed3\u5408\u591a\u7ebf\u7a0b\u3001\u591a\u8fdb\u7a0b\u548c\u952e\u76d8\u76d1\u542c\u5e93\u6765\u5b9e\u73b0\u590d\u6742\u7684\u952e\u76d8\u4e8b\u4ef6\u6355\u6349\u9700\u6c42\u3002\u4f8b\u5982\uff0c\u5728\u4e00\u4e2a\u9700\u8981\u5b9e\u65f6\u54cd\u5e94\u952e\u76d8\u4e8b\u4ef6\u7684\u6e38\u620f\u5e94\u7528\u4e2d\uff0c\u53ef\u4ee5\u4f7f\u7528<code>pynput<\/code>\u76d1\u542c\u952e\u76d8\u4e8b\u4ef6\uff0c\u5e76\u901a\u8fc7\u591a\u7ebf\u7a0b\u5904\u7406\u6e38\u620f\u903b\u8f91\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import threading<\/p>\n<p>from pynput import keyboard<\/p>\n<p>def on_press(key):<\/p>\n<p>    try:<\/p>\n<p>        print(&#39;Key {0} pressed.&#39;.format(key.char))<\/p>\n<p>    except AttributeError:<\/p>\n<p>        print(&#39;Special key {0} pressed.&#39;.format(key))<\/p>\n<p>def game_logic():<\/p>\n<p>    while True:<\/p>\n<p>        # \u6a21\u62df\u6e38\u620f\u903b\u8f91<\/p>\n<p>        print(&quot;Running game logic...&quot;)<\/p>\n<p>        time.sleep(1)<\/p>\n<h2><strong>\u521b\u5efa\u5e76\u542f\u52a8\u6e38\u620f\u903b\u8f91\u7ebf\u7a0b<\/strong><\/h2>\n<p>game_thread = threading.Thread(target=game_logic)<\/p>\n<p>game_thread.start()<\/p>\n<h2><strong>\u542f\u52a8\u952e\u76d8\u76d1\u542c\u5668<\/strong><\/h2>\n<p>with keyboard.Listener(on_press=on_press) as listener:<\/p>\n<p>    listener.join()<\/p>\n<p>game_thread.join()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u901a\u8fc7\u8fd9\u79cd\u65b9\u5f0f\uff0c\u53ef\u4ee5\u5728\u4e0d\u5f71\u54cd\u6e38\u620f\u903b\u8f91\u7684\u60c5\u51b5\u4e0b\u5b9e\u65f6\u6355\u6349\u952e\u76d8\u4e8b\u4ef6\uff0c\u63d0\u9ad8\u7a0b\u5e8f\u7684\u54cd\u5e94\u901f\u5ea6\u548c\u7528\u6237\u4f53\u9a8c\u3002<\/p>\n<\/p>\n<p><p>\u603b\u7ed3\uff1a\u5728Python\u4e2d\uff0c\u53ef\u4ee5\u901a\u8fc7<strong>\u591a\u7ebf\u7a0b\u3001\u591a\u8fdb\u7a0b\u548c\u952e\u76d8\u76d1\u542c\u5e93<\/strong>\u6765\u5b9e\u73b0\u540c\u65f6\u6355\u6349\u952e\u76d8\u4e8b\u4ef6\u3002\u6839\u636e\u5177\u4f53\u9700\u6c42\u9009\u62e9\u5408\u9002\u7684\u65b9\u6cd5\uff0c\u7ed3\u5408\u591a\u79cd\u6280\u672f\u624b\u6bb5\uff0c\u53ef\u4ee5\u5b9e\u73b0\u9ad8\u6548\u3001\u7a33\u5b9a\u7684\u952e\u76d8\u4e8b\u4ef6\u5904\u7406\u7a0b\u5e8f\u3002<\/p>\n<\/p>\n<h2><strong>\u76f8\u5173\u95ee\u7b54FAQs\uff1a<\/strong><\/h2>\n<p> <strong>\u5982\u4f55\u5728Python\u4e2d\u5b9e\u73b0\u952e\u76d8\u8f93\u5165\u76d1\u542c\u529f\u80fd\uff1f<\/strong><br \/>\u5728Python\u4e2d\uff0c\u60a8\u53ef\u4ee5\u4f7f\u7528\u7b2c\u4e09\u65b9\u5e93\u5982<code>pynput<\/code>\u6765\u5b9e\u73b0\u952e\u76d8\u8f93\u5165\u76d1\u542c\u3002\u901a\u8fc7\u5b89\u88c5<code>pynput<\/code>\uff0c\u60a8\u53ef\u4ee5\u521b\u5efa\u4e00\u4e2a\u76d1\u542c\u5668\u6765\u6355\u6349\u952e\u76d8\u4e8b\u4ef6\uff0c\u4ece\u800c\u5b9e\u65f6\u83b7\u53d6\u7528\u6237\u7684\u6309\u952e\u8f93\u5165\u3002\u4ee5\u4e0b\u662f\u4e00\u4e2a\u7b80\u5355\u793a\u4f8b\uff1a<\/p>\n<pre><code class=\"language-python\">from pynput import keyboard\n\ndef on_press(key):\n    print(f&#39;\u6309\u4e0b\u4e86: {key}&#39;)\n\ndef on_release(key):\n    print(f&#39;\u91ca\u653e\u4e86: {key}&#39;)\n    if key == keyboard.Key.esc:\n        # \u505c\u6b62\u76d1\u542c\n        return False\n\nwith keyboard.Listener(on_press=on_press, on_release=on_release) as listener:\n    listener.join()\n<\/code><\/pre>\n<p><strong>\u4f7f\u7528Python\u6355\u6349\u952e\u76d8\u8f93\u5165\u65f6\u9700\u8981\u6ce8\u610f\u54ea\u4e9b\u5b89\u5168\u6027\u95ee\u9898\uff1f<\/strong><br \/>\u5728\u4f7f\u7528Python\u6355\u6349\u952e\u76d8\u8f93\u5165\u65f6\uff0c\u5b89\u5168\u6027\u95ee\u9898\u662f\u4e00\u4e2a\u4e0d\u53ef\u5ffd\u89c6\u7684\u65b9\u9762\u3002\u786e\u4fdd\u60a8\u4ec5\u5728\u53d7\u4fe1\u4efb\u7684\u73af\u5883\u4e2d\u4f7f\u7528\u6b64\u529f\u80fd\uff0c\u5e76\u4e14\u4e0d\u8981\u5728\u4e0d\u77e5\u60c5\u7684\u60c5\u51b5\u4e0b\u8bb0\u5f55\u7528\u6237\u7684\u8f93\u5165\u3002\u6b64\u5916\uff0c\u9075\u5faa\u76f8\u5173\u6cd5\u5f8b\u6cd5\u89c4\uff0c\u786e\u4fdd\u7528\u6237\u7684\u9690\u79c1\u4e0d\u88ab\u4fb5\u72af\u3002<\/p>\n<p><strong>\u5982\u4f55\u5728Python\u4e2d\u5904\u7406\u540c\u65f6\u6309\u4e0b\u591a\u4e2a\u952e\u7684\u60c5\u51b5\uff1f<\/strong><br \/>\u8981\u5904\u7406\u591a\u4e2a\u952e\u540c\u65f6\u6309\u4e0b\u7684\u60c5\u51b5\uff0c\u53ef\u4ee5\u5728<code>on_press<\/code>\u65b9\u6cd5\u4e2d\u8bb0\u5f55\u6bcf\u4e00\u4e2a\u88ab\u6309\u4e0b\u7684\u952e\uff0c\u5e76\u5728<code>on_release<\/code>\u65b9\u6cd5\u4e2d\u8fdb\u884c\u6e05\u7406\u3002\u901a\u8fc7\u7ef4\u62a4\u4e00\u4e2a\u96c6\u5408\u6765\u8ddf\u8e2a\u5f53\u524d\u6309\u4e0b\u7684\u952e\uff0c\u60a8\u53ef\u4ee5\u8f7b\u677e\u8bc6\u522b\u7ec4\u5408\u952e\u64cd\u4f5c\u3002\u4ee5\u4e0b\u662f\u793a\u4f8b\u4ee3\u7801\uff1a<\/p>\n<pre><code class=\"language-python\">from pynput import keyboard\n\ncurrent_keys = set()\n\ndef on_press(key):\n    current_keys.add(key)\n    print(f&#39;\u5f53\u524d\u6309\u4e0b\u7684\u952e: {current_keys}&#39;)\n\ndef on_release(key):\n    current_keys.discard(key)\n    print(f&#39;\u5f53\u524d\u6309\u4e0b\u7684\u952e: {current_keys}&#39;)\n    if key == keyboard.Key.esc:\n        return False\n\nwith keyboard.Listener(on_press=on_press, on_release=on_release) as listener:\n    listener.join()\n<\/code><\/pre>\n<p>\u901a\u8fc7\u4ee5\u4e0a\u65b9\u6cd5\uff0c\u60a8\u53ef\u4ee5\u7075\u6d3b\u5730\u6355\u6349\u548c\u5904\u7406\u952e\u76d8\u8f93\u5165\uff0c\u6ee1\u8db3\u4e0d\u540c\u7684\u9700\u6c42\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"\u5728Python\u4e2d\uff0c\u53ef\u4ee5\u901a\u8fc7\u591a\u7ebf\u7a0b\u3001\u591a\u8fdb\u7a0b\u548c\u952e\u76d8\u76d1\u542c\u5e93\u6765\u5b9e\u73b0\u540c\u65f6\u6355\u6349\u952e\u76d8\u4e8b\u4ef6\u3001\u4f7f\u7528\u5e93\u5982pynput\u3001keyboa [&hellip;]","protected":false},"author":3,"featured_media":968481,"comment_status":"closed","ping_status":"","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[37],"tags":[],"acf":[],"_links":{"self":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/968474"}],"collection":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/users\/3"}],"replies":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/comments?post=968474"}],"version-history":[{"count":"1","href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/968474\/revisions"}],"predecessor-version":[{"id":968485,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/968474\/revisions\/968485"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/968481"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=968474"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=968474"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=968474"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}