{"id":1158120,"date":"2025-01-13T18:36:48","date_gmt":"2025-01-13T10:36:48","guid":{"rendered":""},"modified":"2025-01-13T18:36:52","modified_gmt":"2025-01-13T10:36:52","slug":"python%e5%a6%82%e4%bd%95%e6%8c%89%e7%a9%ba%e6%a0%bc%e9%80%80%e5%87%ba","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/1158120.html","title":{"rendered":"python\u5982\u4f55\u6309\u7a7a\u683c\u9000\u51fa"},"content":{"rendered":"<p style=\"text-align:center;\" ><img decoding=\"async\" src=\"https:\/\/cdn-kb.worktile.com\/kb\/wp-content\/uploads\/2024\/04\/25200145\/130e4a90-7cab-4f26-aac3-62aef326d98d.webp\" alt=\"python\u5982\u4f55\u6309\u7a7a\u683c\u9000\u51fa\" \/><\/p>\n<p><p> \u5728Python\u4e2d\uff0c\u6709\u51e0\u79cd\u65b9\u6cd5\u53ef\u4ee5\u5b9e\u73b0\u6309\u7a7a\u683c\u9000\u51fa\u7a0b\u5e8f\u3002<strong>\u5e38\u7528\u7684\u65b9\u6cd5\u6709\uff1a\u4f7f\u7528\u952e\u76d8\u76d1\u542c\u5e93\u5982<code>keyboard<\/code>\u3001<code>pynput<\/code>\uff0c\u4f7f\u7528\u63a7\u5236\u53f0\u8f93\u5165\u51fd\u6570\u5982<code>input<\/code>\u3002\u6211\u4eec\u5c06\u8be6\u7ec6\u63a2\u8ba8\u4f7f\u7528<code>keyboard<\/code>\u5e93\u7684\u65b9\u6cd5\u3002<\/strong><\/p>\n<\/p>\n<p><p><code>keyboard<\/code>\u5e93\u53ef\u4ee5\u8f7b\u677e\u5730\u76d1\u542c\u6309\u952e\u4e8b\u4ef6\uff0c\u5e76\u4f5c\u51fa\u76f8\u5e94\u7684\u5904\u7406\u3002\u4ee5\u4e0b\u662f\u5982\u4f55\u4f7f\u7528<code>keyboard<\/code>\u5e93\u6765\u5b9e\u73b0\u6309\u7a7a\u683c\u952e\u9000\u51fa\u7a0b\u5e8f\u7684\u8be6\u7ec6\u6b65\u9aa4\uff1a<\/p>\n<\/p>\n<p><h3>\u5b89\u88c5<code>keyboard<\/code>\u5e93<\/h3>\n<\/p>\n<p><p>\u9996\u5148\uff0c\u4f60\u9700\u8981\u5b89\u88c5<code>keyboard<\/code>\u5e93\u3002\u53ef\u4ee5\u4f7f\u7528\u4ee5\u4e0b\u547d\u4ee4\u6765\u5b89\u88c5\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-bash\">pip install keyboard<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u4ee3\u7801\u793a\u4f8b<\/h3>\n<\/p>\n<p><p>\u4e0b\u9762\u662f\u4e00\u4e2a\u4f7f\u7528<code>keyboard<\/code>\u5e93\u6765\u76d1\u542c\u7a7a\u683c\u952e\u5e76\u9000\u51fa\u7a0b\u5e8f\u7684\u7b80\u5355\u793a\u4f8b\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import keyboard<\/p>\n<p>print(&quot;Press space to exit...&quot;)<\/p>\n<h2><strong>\u4f7f\u7528 keyboard.w<a href=\"https:\/\/docs.pingcode.com\/blog\/59162.html\" target=\"_blank\">AI<\/a>t() \u65b9\u6cd5\u6765\u7b49\u5f85\u7a7a\u683c\u952e<\/strong><\/h2>\n<p>keyboard.wait(&#39;space&#39;)<\/p>\n<p>print(&quot;Exiting the program.&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u793a\u4f8b\u4e2d\uff0c\u7a0b\u5e8f\u4f1a\u7b49\u5f85\u7528\u6237\u6309\u4e0b\u7a7a\u683c\u952e\uff0c\u4e00\u65e6\u68c0\u6d4b\u5230\u7a7a\u683c\u952e\u88ab\u6309\u4e0b\uff0c\u7a0b\u5e8f\u5c06\u7ee7\u7eed\u6267\u884c\u5e76\u9000\u51fa\u3002<\/p>\n<\/p>\n<p><h3>\u8be6\u7ec6\u89e3\u91ca<\/h3>\n<\/p>\n<ol>\n<li><strong>\u5bfc\u5165\u5e93<\/strong>\uff1a\u9996\u5148\uff0c\u6211\u4eec\u5bfc\u5165\u4e86<code>keyboard<\/code>\u5e93\u3002<\/li>\n<li><strong>\u6253\u5370\u63d0\u793a<\/strong>\uff1a\u6211\u4eec\u6253\u5370\u4e86\u4e00\u6761\u6d88\u606f\uff0c\u63d0\u793a\u7528\u6237\u6309\u7a7a\u683c\u952e\u9000\u51fa\u7a0b\u5e8f\u3002<\/li>\n<li><strong>\u7b49\u5f85\u6309\u952e<\/strong>\uff1a<code>keyboard.wait(&#39;space&#39;)<\/code>\u4f1a\u963b\u585e\u7a0b\u5e8f\u6267\u884c\uff0c\u76f4\u5230\u68c0\u6d4b\u5230\u7a7a\u683c\u952e\u88ab\u6309\u4e0b\u3002<\/li>\n<li><strong>\u7ee7\u7eed\u6267\u884c<\/strong>\uff1a\u4e00\u65e6\u7a7a\u683c\u952e\u88ab\u6309\u4e0b\uff0c\u7a0b\u5e8f\u4f1a\u7ee7\u7eed\u6267\u884c\uff0c\u5e76\u6253\u5370\u9000\u51fa\u6d88\u606f\u3002<\/li>\n<\/ol>\n<p><h3>\u952e\u76d8\u76d1\u542c\u7684\u5176\u4ed6\u65b9\u6cd5<\/h3>\n<\/p>\n<p><p>\u9664\u4e86<code>keyboard<\/code>\u5e93\uff0c\u8fd8\u6709\u5176\u4ed6\u65b9\u6cd5\u53ef\u4ee5\u5b9e\u73b0\u7c7b\u4f3c\u7684\u529f\u80fd\u3002\u4e0b\u9762\u7b80\u8981\u4ecb\u7ecd\u51e0\u79cd\uff1a<\/p>\n<\/p>\n<p><h4>1. \u4f7f\u7528<code>pynput<\/code>\u5e93<\/h4>\n<\/p>\n<p><p><code>pynput<\/code>\u5e93\u4e5f\u53ef\u4ee5\u7528\u4e8e\u76d1\u542c\u952e\u76d8\u4e8b\u4ef6\u3002\u4ee5\u4e0b\u662f\u4e00\u4e2a\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>        if key.char == &#39; &#39;:<\/p>\n<p>            print(&quot;Space pressed. Exiting...&quot;)<\/p>\n<p>            return False<\/p>\n<p>    except AttributeError:<\/p>\n<p>        pass<\/p>\n<p>with keyboard.Listener(on_press=on_press) as listener:<\/p>\n<p>    listener.join()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u793a\u4f8b\u4e2d\uff0c\u7a0b\u5e8f\u4f1a\u76d1\u542c\u952e\u76d8\u4e8b\u4ef6\uff0c\u4e00\u65e6\u68c0\u6d4b\u5230\u7a7a\u683c\u952e\u88ab\u6309\u4e0b\uff0c\u7a0b\u5e8f\u4f1a\u6253\u5370\u6d88\u606f\u5e76\u9000\u51fa\u3002<\/p>\n<\/p>\n<p><h4>2. \u4f7f\u7528\u63a7\u5236\u53f0\u8f93\u5165\u51fd\u6570<\/h4>\n<\/p>\n<p><p>\u4f60\u4e5f\u53ef\u4ee5\u4f7f\u7528<code>input()<\/code>\u51fd\u6570\u6765\u5b9e\u73b0\u6309\u7a7a\u683c\u952e\u9000\u51fa\u7a0b\u5e8f\uff0c\u867d\u7136\u8fd9\u79cd\u65b9\u6cd5\u4e0d\u5982\u952e\u76d8\u76d1\u542c\u5e93\u7075\u6d3b\uff0c\u4f46\u5728\u67d0\u4e9b\u7b80\u5355\u7684\u573a\u666f\u4e0b\u540c\u6837\u6709\u6548\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">input(&quot;Press space to exit... (Press Enter after typing space)&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u793a\u4f8b\u4e2d\uff0c\u7528\u6237\u9700\u8981\u5728\u6309\u4e0b\u7a7a\u683c\u952e\u540e\u6309\u56de\u8f66\u952e\uff0c\u7a0b\u5e8f\u624d\u4f1a\u7ee7\u7eed\u6267\u884c\u5e76\u9000\u51fa\u3002<\/p>\n<\/p>\n<p><h3>\u603b\u7ed3<\/h3>\n<\/p>\n<p><p><strong>Python\u4e2d\u53ef\u4ee5\u901a\u8fc7\u4f7f\u7528<code>keyboard<\/code>\u5e93\u3001<code>pynput<\/code>\u5e93\u6216\u8005\u63a7\u5236\u53f0\u8f93\u5165\u51fd\u6570\u6765\u5b9e\u73b0\u6309\u7a7a\u683c\u952e\u9000\u51fa\u7a0b\u5e8f<\/strong>\u3002<strong>\u5176\u4e2d<code>keyboard<\/code>\u5e93\u662f\u4e00\u79cd\u975e\u5e38\u4fbf\u6377\u4e14\u5f3a\u5927\u7684\u9009\u62e9<\/strong>\u3002\u901a\u8fc7\u8fd9\u4e9b\u65b9\u6cd5\uff0c\u4f60\u53ef\u4ee5\u6839\u636e\u5177\u4f53\u9700\u6c42\u9009\u62e9\u5408\u9002\u7684\u65b9\u6848\u6765\u5b9e\u73b0\u6309\u952e\u4e8b\u4ef6\u7684\u76d1\u542c\u548c\u5904\u7406\u3002<\/p>\n<\/p>\n<p><p>\u63a5\u4e0b\u6765\uff0c\u6211\u4eec\u5c06\u8be6\u7ec6\u63a2\u8ba8\u6bcf\u79cd\u65b9\u6cd5\u7684\u4f18\u52bf\u548c\u52a3\u52bf\uff0c\u4ee5\u53ca\u5728\u5b9e\u9645\u5e94\u7528\u4e2d\u7684\u6700\u4f73\u5b9e\u8df5\u3002<\/p>\n<\/p>\n<p><h2>\u4e00\u3001\u4f7f\u7528<code>keyboard<\/code>\u5e93<\/h2>\n<\/p>\n<p><h3>\u4f18\u52bf<\/h3>\n<\/p>\n<ol>\n<li><strong>\u7b80\u5355\u6613\u7528<\/strong>\uff1a<code>keyboard<\/code>\u5e93\u7684API\u8bbe\u8ba1\u975e\u5e38\u7b80\u6d01\uff0c\u51e0\u884c\u4ee3\u7801\u5c31\u53ef\u4ee5\u5b9e\u73b0\u590d\u6742\u7684\u952e\u76d8\u76d1\u542c\u529f\u80fd\u3002<\/li>\n<li><strong>\u8de8\u5e73\u53f0\u652f\u6301<\/strong>\uff1a<code>keyboard<\/code>\u5e93\u53ef\u4ee5\u5728Windows\u3001Linux\u548cmacOS\u4e0a\u8fd0\u884c\uff0c\u5177\u6709\u5f88\u597d\u7684\u8de8\u5e73\u53f0\u517c\u5bb9\u6027\u3002<\/li>\n<li><strong>\u9ad8\u6548<\/strong>\uff1a<code>keyboard<\/code>\u5e93\u7684\u6027\u80fd\u8f83\u9ad8\uff0c\u9002\u7528\u4e8e\u5b9e\u65f6\u6027\u8981\u6c42\u8f83\u9ad8\u7684\u573a\u666f\u3002<\/li>\n<\/ol>\n<p><h3>\u52a3\u52bf<\/h3>\n<\/p>\n<ol>\n<li><strong>\u9700\u8981\u5b89\u88c5\u7b2c\u4e09\u65b9\u5e93<\/strong>\uff1a<code>keyboard<\/code>\u5e93\u4e0d\u662fPython\u6807\u51c6\u5e93\u7684\u4e00\u90e8\u5206\uff0c\u9700\u8981\u989d\u5916\u5b89\u88c5\u3002<\/li>\n<li><strong>\u6743\u9650\u95ee\u9898<\/strong>\uff1a\u5728\u67d0\u4e9b\u64cd\u4f5c\u7cfb\u7edf\u4e0a\uff08\u5982macOS\uff09\uff0c\u9700\u8981\u6388\u4e88\u7a0b\u5e8f\u7279\u5b9a\u7684\u6743\u9650\u624d\u80fd\u76d1\u542c\u952e\u76d8\u4e8b\u4ef6\u3002<\/li>\n<\/ol>\n<p><h3>\u8be6\u7ec6\u793a\u4f8b<\/h3>\n<\/p>\n<p><pre><code class=\"language-python\">import keyboard<\/p>\n<p>def main():<\/p>\n<p>    print(&quot;Press space to exit...&quot;)<\/p>\n<p>    # \u4f7f\u7528 keyboard.wait() \u65b9\u6cd5\u6765\u7b49\u5f85\u7a7a\u683c\u952e<\/p>\n<p>    keyboard.wait(&#39;space&#39;)<\/p>\n<p>    print(&quot;Exiting the program.&quot;)<\/p>\n<p>if __name__ == &quot;__main__&quot;:<\/p>\n<p>    main()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u793a\u4f8b\u4e2d\uff0c<code>keyboard.wait(&#39;space&#39;)<\/code>\u4f1a\u963b\u585e\u7a0b\u5e8f\u6267\u884c\uff0c\u76f4\u5230\u68c0\u6d4b\u5230\u7a7a\u683c\u952e\u88ab\u6309\u4e0b\u3002<\/p>\n<\/p>\n<p><h3>\u9ad8\u7ea7\u7528\u6cd5<\/h3>\n<\/p>\n<p><p>\u4f60\u8fd8\u53ef\u4ee5\u4f7f\u7528<code>keyboard.add_hotkey<\/code>\u6765\u5b9e\u73b0\u66f4\u590d\u6742\u7684\u952e\u76d8\u4e8b\u4ef6\u5904\u7406\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import keyboard<\/p>\n<p>def on_space():<\/p>\n<p>    print(&quot;Space pressed. Exiting...&quot;)<\/p>\n<p>    keyboard.unhook_all_hotkeys()<\/p>\n<p>    exit()<\/p>\n<p>keyboard.add_hotkey(&#39;space&#39;, on_space)<\/p>\n<p>print(&quot;Press space to exit...&quot;)<\/p>\n<p>keyboard.wait(&#39;esc&#39;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u793a\u4f8b\u4e2d\uff0c\u6211\u4eec\u4f7f\u7528<code>keyboard.add_hotkey<\/code>\u6ce8\u518c\u4e86\u4e00\u4e2a\u7a7a\u683c\u952e\u7684\u70ed\u952e\uff0c\u5f53\u7a7a\u683c\u952e\u88ab\u6309\u4e0b\u65f6\uff0c\u8c03\u7528<code>on_space<\/code>\u51fd\u6570\u5e76\u9000\u51fa\u7a0b\u5e8f\u3002<\/p>\n<\/p>\n<p><h2>\u4e8c\u3001\u4f7f\u7528<code>pynput<\/code>\u5e93<\/h2>\n<\/p>\n<p><h3>\u4f18\u52bf<\/h3>\n<\/p>\n<ol>\n<li><strong>\u529f\u80fd\u5f3a\u5927<\/strong>\uff1a<code>pynput<\/code>\u5e93\u4e0d\u4ec5\u53ef\u4ee5\u76d1\u542c\u952e\u76d8\u4e8b\u4ef6\uff0c\u8fd8\u53ef\u4ee5\u63a7\u5236\u952e\u76d8\u548c\u9f20\u6807\uff0c\u9002\u7528\u4e8e\u66f4\u590d\u6742\u7684\u4ea4\u4e92\u573a\u666f\u3002<\/li>\n<li><strong>\u8de8\u5e73\u53f0\u652f\u6301<\/strong>\uff1a<code>pynput<\/code>\u5e93\u540c\u6837\u652f\u6301Windows\u3001Linux\u548cmacOS\u3002<\/li>\n<\/ol>\n<p><h3>\u52a3\u52bf<\/h3>\n<\/p>\n<ol>\n<li><strong>\u590d\u6742\u5ea6\u8f83\u9ad8<\/strong>\uff1a<code>pynput<\/code>\u5e93\u7684\u4f7f\u7528\u76f8\u5bf9<code>keyboard<\/code>\u5e93\u8981\u590d\u6742\u4e00\u4e9b\uff0c\u9700\u8981\u66f4\u591a\u7684\u4ee3\u7801\u548c\u914d\u7f6e\u3002<\/li>\n<li><strong>\u9700\u8981\u5b89\u88c5\u7b2c\u4e09\u65b9\u5e93<\/strong>\uff1a<code>pynput<\/code>\u5e93\u4e5f\u9700\u8981\u989d\u5916\u5b89\u88c5\u3002<\/li>\n<\/ol>\n<p><h3>\u8be6\u7ec6\u793a\u4f8b<\/h3>\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>        if key.char == &#39; &#39;:<\/p>\n<p>            print(&quot;Space pressed. Exiting...&quot;)<\/p>\n<p>            return False<\/p>\n<p>    except AttributeError:<\/p>\n<p>        pass<\/p>\n<p>with keyboard.Listener(on_press=on_press) as listener:<\/p>\n<p>    listener.join()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u793a\u4f8b\u4e2d\uff0c\u6211\u4eec\u521b\u5efa\u4e86\u4e00\u4e2a\u952e\u76d8\u76d1\u542c\u5668\uff0c\u5e76\u5728\u68c0\u6d4b\u5230\u7a7a\u683c\u952e\u88ab\u6309\u4e0b\u65f6\u9000\u51fa\u7a0b\u5e8f\u3002<\/p>\n<\/p>\n<p><h3>\u9ad8\u7ea7\u7528\u6cd5<\/h3>\n<\/p>\n<p><p>\u4f60\u53ef\u4ee5\u4f7f\u7528<code>pynput<\/code>\u5e93\u7684\u66f4\u591a\u529f\u80fd\u6765\u5b9e\u73b0\u66f4\u590d\u6742\u7684\u952e\u76d8\u4e8b\u4ef6\u5904\u7406\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">from pynput import keyboard<\/p>\n<p>class KeyHandler:<\/p>\n<p>    def __init__(self):<\/p>\n<p>        self.listener = keyboard.Listener(on_press=self.on_press)<\/p>\n<p>        self.listener.start()<\/p>\n<p>    def on_press(self, key):<\/p>\n<p>        try:<\/p>\n<p>            if key.char == &#39; &#39;:<\/p>\n<p>                print(&quot;Space pressed. Exiting...&quot;)<\/p>\n<p>                self.listener.stop()<\/p>\n<p>        except AttributeError:<\/p>\n<p>            pass<\/p>\n<p>if __name__ == &quot;__main__&quot;:<\/p>\n<p>    handler = KeyHandler()<\/p>\n<p>    handler.listener.join()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u793a\u4f8b\u4e2d\uff0c\u6211\u4eec\u5c06\u952e\u76d8\u4e8b\u4ef6\u5904\u7406\u5c01\u88c5\u5230\u4e00\u4e2a\u7c7b\u4e2d\uff0c\u4fbf\u4e8e\u6269\u5c55\u548c\u7ba1\u7406\u3002<\/p>\n<\/p>\n<p><h2>\u4e09\u3001\u4f7f\u7528\u63a7\u5236\u53f0\u8f93\u5165\u51fd\u6570<\/h2>\n<\/p>\n<p><h3>\u4f18\u52bf<\/h3>\n<\/p>\n<ol>\n<li><strong>\u65e0\u9700\u5b89\u88c5\u7b2c\u4e09\u65b9\u5e93<\/strong>\uff1a\u8fd9\u79cd\u65b9\u6cd5\u4e0d\u9700\u8981\u4efb\u4f55\u7b2c\u4e09\u65b9\u5e93\uff0c\u4f7f\u7528Python\u81ea\u5e26\u7684\u529f\u80fd\u5373\u53ef\u5b9e\u73b0\u3002<\/li>\n<li><strong>\u7b80\u5355\u76f4\u89c2<\/strong>\uff1a\u4ee3\u7801\u7b80\u5355\u6613\u61c2\uff0c\u9002\u7528\u4e8e\u975e\u5e38\u57fa\u672c\u7684\u573a\u666f\u3002<\/li>\n<\/ol>\n<p><h3>\u52a3\u52bf<\/h3>\n<\/p>\n<ol>\n<li><strong>\u4e0d\u7075\u6d3b<\/strong>\uff1a\u7528\u6237\u9700\u8981\u6309\u4e0b\u56de\u8f66\u952e\u624d\u80fd\u89e6\u53d1\u4e8b\u4ef6\uff0c\u4e0d\u9002\u7528\u4e8e\u9700\u8981\u5b9e\u65f6\u54cd\u5e94\u7684\u573a\u666f\u3002<\/li>\n<li><strong>\u4ec5\u9002\u7528\u4e8e\u63a7\u5236\u53f0<\/strong>\uff1a\u8fd9\u79cd\u65b9\u6cd5\u53ea\u80fd\u5728\u63a7\u5236\u53f0\u5e94\u7528\u4e2d\u4f7f\u7528\uff0c\u4e0d\u9002\u7528\u4e8e\u56fe\u5f62\u754c\u9762\u5e94\u7528\u3002<\/li>\n<\/ol>\n<p><h3>\u8be6\u7ec6\u793a\u4f8b<\/h3>\n<\/p>\n<p><pre><code class=\"language-python\">input(&quot;Press space to exit... (Press Enter after typing space)&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u793a\u4f8b\u4e2d\uff0c\u7528\u6237\u9700\u8981\u5728\u6309\u4e0b\u7a7a\u683c\u952e\u540e\u6309\u56de\u8f66\u952e\uff0c\u7a0b\u5e8f\u624d\u4f1a\u7ee7\u7eed\u6267\u884c\u5e76\u9000\u51fa\u3002<\/p>\n<\/p>\n<p><h3>\u9ad8\u7ea7\u7528\u6cd5<\/h3>\n<\/p>\n<p><p>\u4f60\u53ef\u4ee5\u4f7f\u7528\u5faa\u73af\u548c\u6761\u4ef6\u5224\u65ad\u6765\u5b9e\u73b0\u7a0d\u5fae\u590d\u6742\u4e00\u4e9b\u7684\u903b\u8f91\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">while True:<\/p>\n<p>    user_input = input(&quot;Press space to exit... (Press Enter after typing space)&quot;)<\/p>\n<p>    if user_input == &#39; &#39;:<\/p>\n<p>        print(&quot;Space pressed. Exiting...&quot;)<\/p>\n<p>        break<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u793a\u4f8b\u4e2d\uff0c\u7a0b\u5e8f\u4f1a\u4e0d\u65ad\u63d0\u793a\u7528\u6237\u8f93\u5165\uff0c\u76f4\u5230\u7528\u6237\u8f93\u5165\u7a7a\u683c\u952e\u5e76\u6309\u4e0b\u56de\u8f66\u952e\u3002<\/p>\n<\/p>\n<p><h2>\u56db\u3001\u5b9e\u9645\u5e94\u7528\u4e2d\u7684\u6700\u4f73\u5b9e\u8df5<\/h2>\n<\/p>\n<p><h3>\u9009\u62e9\u5408\u9002\u7684\u5e93<\/h3>\n<\/p>\n<p><p>\u6839\u636e\u5b9e\u9645\u9700\u6c42\u9009\u62e9\u5408\u9002\u7684\u5e93\u3002\u5982\u679c\u4f60\u7684\u5e94\u7528\u9700\u8981\u5b9e\u65f6\u76d1\u542c\u952e\u76d8\u4e8b\u4ef6\uff0c\u5efa\u8bae\u4f7f\u7528<code>keyboard<\/code>\u6216<code>pynput<\/code>\u5e93\uff1b\u5982\u679c\u53ea\u662f\u7b80\u5355\u7684\u63a7\u5236\u53f0\u8f93\u5165\uff0c\u53ef\u4ee5\u4f7f\u7528<code>input()<\/code>\u51fd\u6570\u3002<\/p>\n<\/p>\n<p><h3>\u9519\u8bef\u5904\u7406<\/h3>\n<\/p>\n<p><p>\u5728\u76d1\u542c\u952e\u76d8\u4e8b\u4ef6\u65f6\uff0c\u6ce8\u610f\u5904\u7406\u53ef\u80fd\u51fa\u73b0\u7684\u5f02\u5e38\u3002\u4f8b\u5982\uff0c\u67d0\u4e9b\u952e\u53ef\u80fd\u6ca1\u6709<code>char<\/code>\u5c5e\u6027\uff0c\u8bbf\u95ee\u65f6\u4f1a\u5f15\u53d1<code>AttributeError<\/code>\u3002<\/p>\n<\/p>\n<p><h3>\u8de8\u5e73\u53f0\u517c\u5bb9\u6027<\/h3>\n<\/p>\n<p><p>\u786e\u4fdd\u4f60\u7684\u4ee3\u7801\u5728\u4e0d\u540c\u64cd\u4f5c\u7cfb\u7edf\u4e0a\u90fd\u80fd\u6b63\u5e38\u8fd0\u884c\u3002<code>keyboard<\/code>\u548c<code>pynput<\/code>\u5e93\u90fd\u652f\u6301\u8de8\u5e73\u53f0\uff0c\u4f46\u5728\u67d0\u4e9b\u64cd\u4f5c\u7cfb\u7edf\u4e0a\u53ef\u80fd\u9700\u8981\u989d\u5916\u7684\u914d\u7f6e\u6216\u6743\u9650\u3002<\/p>\n<\/p>\n<p><h3>\u6027\u80fd\u4f18\u5316<\/h3>\n<\/p>\n<p><p>\u5728\u9ad8\u6027\u80fd\u8981\u6c42\u7684\u5e94\u7528\u4e2d\uff0c\u5c3d\u91cf\u51cf\u5c11\u963b\u585e\u64cd\u4f5c\u3002\u4f7f\u7528\u975e\u963b\u585e\u7684\u952e\u76d8\u76d1\u542c\u65b9\u6cd5\uff0c\u5e76\u5728\u68c0\u6d4b\u5230\u76ee\u6807\u6309\u952e\u65f6\u5feb\u901f\u5904\u7406\u3002<\/p>\n<\/p>\n<p><h3>\u5b89\u5168\u6027<\/h3>\n<\/p>\n<p><p>\u5728\u9700\u8981\u76d1\u542c\u5168\u5c40\u952e\u76d8\u4e8b\u4ef6\u7684\u5e94\u7528\u4e2d\uff0c\u6ce8\u610f\u7528\u6237\u6743\u9650\u548c\u7cfb\u7edf\u5b89\u5168\uff0c\u907f\u514d\u76d1\u542c\u654f\u611f\u4fe1\u606f\u6216\u5f15\u53d1\u5b89\u5168\u6f0f\u6d1e\u3002<\/p>\n<\/p>\n<p><p>\u901a\u8fc7\u4ee5\u4e0a\u65b9\u6cd5\u548c\u6700\u4f73\u5b9e\u8df5\uff0c\u4f60\u53ef\u4ee5\u5728Python\u7a0b\u5e8f\u4e2d\u5b9e\u73b0\u6309\u7a7a\u683c\u952e\u9000\u51fa\uff0c\u5e76\u6839\u636e\u5177\u4f53\u9700\u6c42\u9009\u62e9\u6700\u5408\u9002\u7684\u5b9e\u73b0\u65b9\u6848\u3002\u5e0c\u671b\u672c\u6587\u5bf9\u4f60\u6709\u6240\u5e2e\u52a9\uff01<\/p>\n<\/p>\n<h2><strong>\u76f8\u5173\u95ee\u7b54FAQs\uff1a<\/strong><\/h2>\n<p> <strong>\u5982\u4f55\u5728Python\u4e2d\u5b9e\u73b0\u6309\u7a7a\u683c\u952e\u9000\u51fa\u7a0b\u5e8f\u7684\u529f\u80fd\uff1f<\/strong><br \/>\u5728Python\u4e2d\uff0c\u53ef\u4ee5\u4f7f\u7528<code>keyboard<\/code>\u5e93\u6765\u5b9e\u73b0\u6309\u7a7a\u683c\u952e\u9000\u51fa\u7a0b\u5e8f\u3002\u9996\u5148\uff0c\u9700\u8981\u5b89\u88c5<code>keyboard<\/code>\u5e93\uff0c\u901a\u8fc7\u547d\u4ee4<code>pip install keyboard<\/code>\u8fdb\u884c\u5b89\u88c5\u3002\u63a5\u4e0b\u6765\uff0c\u53ef\u4ee5\u7f16\u5199\u4e00\u4e2a\u7b80\u5355\u7684\u5faa\u73af\uff0c\u76d1\u542c\u7a7a\u683c\u952e\u7684\u6309\u4e0b\u4e8b\u4ef6\u3002\u4e00\u65e6\u68c0\u6d4b\u5230\u7a7a\u683c\u952e\u88ab\u6309\u4e0b\uff0c\u5c31\u53ef\u4ee5\u4f7f\u7528<code>break<\/code>\u8bed\u53e5\u9000\u51fa\u5faa\u73af\uff0c\u4ece\u800c\u7ed3\u675f\u7a0b\u5e8f\u3002<\/p>\n<p><strong>\u5728Python\u4e2d\u68c0\u6d4b\u7a7a\u683c\u952e\u662f\u5426\u88ab\u6309\u4e0b\u6709\u4ec0\u4e48\u5e38\u7528\u7684\u65b9\u6cd5\uff1f<\/strong><br \/>\u9664\u4e86\u4f7f\u7528<code>keyboard<\/code>\u5e93\uff0cPython\u8fd8\u6709\u5176\u4ed6\u65b9\u6cd5\u53ef\u4ee5\u68c0\u6d4b\u6309\u952e\u4e8b\u4ef6\uff0c\u6bd4\u5982\u4f7f\u7528<code>pygame<\/code>\u5e93\u3002\u901a\u8fc7\u8bbe\u7f6e\u4e8b\u4ef6\u76d1\u542c\uff0c\u7a0b\u5e8f\u53ef\u4ee5\u68c0\u6d4b\u5230\u7a7a\u683c\u952e\u7684\u6309\u4e0b\u3002\u9700\u8981\u5148\u5b89\u88c5<code>pygame<\/code>\u5e93\uff0c\u5e76\u4f7f\u7528<code>pygame.event.get()<\/code>\u65b9\u6cd5\u6765\u83b7\u53d6\u4e8b\u4ef6\u5217\u8868\uff0c\u5224\u65ad\u5176\u4e2d\u662f\u5426\u5305\u542b\u7a7a\u683c\u952e\u7684\u6309\u4e0b\u4e8b\u4ef6\u3002<\/p>\n<p><strong>\u5982\u679c\u4e0d\u60f3\u4f7f\u7528\u5916\u90e8\u5e93\uff0c\u5982\u4f55\u5728Python\u4e2d\u5b9e\u73b0\u7b80\u5355\u7684\u952e\u76d8\u8f93\u5165\u9000\u51fa\u7a0b\u5e8f\uff1f<\/strong><br \/>\u53ef\u4ee5\u4f7f\u7528\u5185\u7f6e\u7684<code>input()<\/code>\u51fd\u6570\u6765\u5b9e\u73b0\u7b80\u5355\u7684\u9000\u51fa\u529f\u80fd\u3002\u901a\u8fc7\u63d0\u793a\u7528\u6237\u8f93\u5165\u5185\u5bb9\uff0c\u7528\u6237\u53ef\u4ee5\u8f93\u5165\u7279\u5b9a\u7684\u5b57\u7b26\u4e32\uff08\u4f8b\u5982\u201cexit\u201d\uff09\u6765\u9000\u51fa\u7a0b\u5e8f\u3002\u867d\u7136\u8fd9\u79cd\u65b9\u6cd5\u4e0d\u76f4\u63a5\u76d1\u542c\u7a7a\u683c\u952e\uff0c\u4f46\u53ef\u4ee5\u901a\u8fc7\u8f93\u5165\u7279\u5b9a\u5b57\u7b26\u6765\u8fbe\u5230\u9000\u51fa\u7684\u6548\u679c\u3002\u793a\u4f8b\u4ee3\u7801\u5982\u4e0b\uff1a<\/p>\n<pre><code class=\"language-python\">while True:\n    user_input = input(&quot;\u8f93\u5165 &#39;exit&#39; \u9000\u51fa\u7a0b\u5e8f\uff1a&quot;)\n    if user_input == &quot;exit&quot;:\n        break\n<\/code><\/pre>\n<p>\u8fd9\u79cd\u65b9\u6cd5\u7b80\u5355\u6613\u7528\uff0c\u9002\u5408\u4e0d\u60f3\u5f15\u5165\u5916\u90e8\u5e93\u7684\u60c5\u51b5\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"\u5728Python\u4e2d\uff0c\u6709\u51e0\u79cd\u65b9\u6cd5\u53ef\u4ee5\u5b9e\u73b0\u6309\u7a7a\u683c\u9000\u51fa\u7a0b\u5e8f\u3002\u5e38\u7528\u7684\u65b9\u6cd5\u6709\uff1a\u4f7f\u7528\u952e\u76d8\u76d1\u542c\u5e93\u5982keyboard\u3001pynpu [&hellip;]","protected":false},"author":3,"featured_media":1158132,"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\/1158120"}],"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=1158120"}],"version-history":[{"count":"1","href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1158120\/revisions"}],"predecessor-version":[{"id":1158134,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1158120\/revisions\/1158134"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/1158132"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=1158120"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=1158120"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=1158120"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}