{"id":1029812,"date":"2024-12-31T11:11:37","date_gmt":"2024-12-31T03:11:37","guid":{"rendered":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/1029812.html"},"modified":"2024-12-31T11:11:39","modified_gmt":"2024-12-31T03:11:39","slug":"python%e5%a6%82%e4%bd%95%e6%a3%80%e6%b5%8b%e6%9f%90%e4%b8%aa%e6%8c%89%e9%94%ae%e8%a2%ab%e6%8c%89%e4%b8%8b","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/1029812.html","title":{"rendered":"python\u5982\u4f55\u68c0\u6d4b\u67d0\u4e2a\u6309\u952e\u88ab\u6309\u4e0b"},"content":{"rendered":"<p style=\"text-align:center;\" ><img decoding=\"async\" src=\"https:\/\/cdn-docs.pingcode.com\/wp-content\/uploads\/2024\/12\/93828d26-02a5-4f95-96ca-d572665af169.webp?x-oss-process=image\/auto-orient,1\/format,webp\" alt=\"python\u5982\u4f55\u68c0\u6d4b\u67d0\u4e2a\u6309\u952e\u88ab\u6309\u4e0b\" \/><\/p>\n<p><p> <strong>Python\u68c0\u6d4b\u67d0\u4e2a\u6309\u952e\u88ab\u6309\u4e0b\u7684\u65b9\u6cd5\u6709\u591a\u79cd\uff1a\u4f7f\u7528Pygame\u5e93\u3001\u4f7f\u7528Pynput\u5e93\u3001\u4f7f\u7528Keyboard\u5e93<\/strong>\u3002\u5176\u4e2d\uff0c\u4f7f\u7528Pygame\u5e93\u3001\u4f7f\u7528Pynput\u5e93\u662f\u4e24\u79cd\u5e38\u89c1\u7684\u65b9\u6cd5\u3002\u4e0b\u9762\u5c06\u8be6\u7ec6\u4ecb\u7ecd\u5982\u4f55\u4f7f\u7528\u8fd9\u4e24\u79cd\u65b9\u6cd5\u6765\u68c0\u6d4b\u67d0\u4e2a\u6309\u952e\u88ab\u6309\u4e0b\u3002<\/p>\n<\/p>\n<p><h3>\u4e00\u3001\u4f7f\u7528Pygame\u5e93<\/h3>\n<\/p>\n<p><p>Pygame \u662f\u4e00\u4e2a\u8de8\u5e73\u53f0\u7684Python\u6a21\u5757\uff0c\u4e13\u95e8\u7528\u4e8e\u7f16\u5199\u89c6\u9891\u6e38\u620f\u3002\u5b83\u5305\u62ec\u8ba1\u7b97\u673a\u56fe\u5f62\u548c\u58f0\u97f3\u5e93\u3002\u5c3d\u7ba1\u5b83\u662f\u4e3a\u6e38\u620f\u5f00\u53d1\u8bbe\u8ba1\u7684\uff0c\u4f46\u5b83\u4e5f\u53ef\u4ee5\u7528\u4e8e\u68c0\u6d4b\u6309\u952e\u8f93\u5165\u3002<\/p>\n<\/p>\n<p><h4>1. \u5b89\u88c5Pygame<\/h4>\n<\/p>\n<p><p>\u9996\u5148\uff0c\u60a8\u9700\u8981\u5b89\u88c5Pygame\u5e93\u3002\u53ef\u4ee5\u4f7f\u7528\u4ee5\u4e0b\u547d\u4ee4\u6765\u5b89\u88c5\u5b83\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-bash\">pip install pygame<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>2. \u68c0\u6d4b\u6309\u952e\u88ab\u6309\u4e0b<\/h4>\n<\/p>\n<p><p>\u4f7f\u7528Pygame\u5e93\u68c0\u6d4b\u6309\u952e\u88ab\u6309\u4e0b\u7684\u57fa\u672c\u6b65\u9aa4\u5982\u4e0b\uff1a<\/p>\n<\/p>\n<ol>\n<li>\u521d\u59cb\u5316Pygame\u3002<\/li>\n<li>\u521b\u5efa\u4e00\u4e2a\u4e8b\u4ef6\u5faa\u73af\u6765\u68c0\u67e5\u6309\u952e\u4e8b\u4ef6\u3002<\/li>\n<li>\u4f7f\u7528<code>pygame.key.get_pressed()<\/code>\u65b9\u6cd5\u6765\u68c0\u67e5\u67d0\u4e2a\u6309\u952e\u662f\u5426\u88ab\u6309\u4e0b\u3002<\/li>\n<\/ol>\n<p><p>\u4ee5\u4e0b\u662f\u4e00\u4e2a\u793a\u4f8b\u4ee3\u7801\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import pygame<\/p>\n<p>import sys<\/p>\n<h2><strong>\u521d\u59cb\u5316Pygame<\/strong><\/h2>\n<p>pygame.init()<\/p>\n<h2><strong>\u8bbe\u7f6e\u7a97\u53e3\u5927\u5c0f<\/strong><\/h2>\n<p>screen = pygame.display.set_mode((640, 480))<\/p>\n<h2><strong>\u4e3b\u5faa\u73af<\/strong><\/h2>\n<p>while True:<\/p>\n<p>    for event in pygame.event.get():<\/p>\n<p>        if event.type == pygame.QUIT:<\/p>\n<p>            pygame.quit()<\/p>\n<p>            sys.exit()<\/p>\n<p>    # \u68c0\u6d4b\u6309\u952e<\/p>\n<p>    keys = pygame.key.get_pressed()<\/p>\n<p>    if keys[pygame.K_SPACE]:<\/p>\n<p>        print(&quot;\u7a7a\u683c\u952e\u88ab\u6309\u4e0b&quot;)<\/p>\n<p>    # \u5237\u65b0\u5c4f\u5e55<\/p>\n<p>    pygame.display.update()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u793a\u4f8b\u4e2d\uff0c\u6211\u4eec\u9996\u5148\u521d\u59cb\u5316\u4e86Pygame\u5e76\u8bbe\u7f6e\u4e86\u4e00\u4e2a\u7a97\u53e3\u5927\u5c0f\u3002\u5728\u4e3b\u5faa\u73af\u4e2d\uff0c\u6211\u4eec\u68c0\u67e5\u4e8b\u4ef6\u961f\u5217\u4e2d\u7684\u4e8b\u4ef6\uff0c\u5982\u679c\u68c0\u6d4b\u5230<code>pygame.QUIT<\/code>\u4e8b\u4ef6\uff08\u5373\u7528\u6237\u5173\u95ed\u7a97\u53e3\uff09\uff0c\u5219\u9000\u51fa\u7a0b\u5e8f\u3002\u7136\u540e\uff0c\u6211\u4eec\u4f7f\u7528<code>pygame.key.get_pressed()<\/code>\u65b9\u6cd5\u6765\u83b7\u53d6\u6240\u6709\u6309\u952e\u7684\u72b6\u6001\uff0c\u5e76\u68c0\u67e5\u7a7a\u683c\u952e\u662f\u5426\u88ab\u6309\u4e0b\u3002<\/p>\n<\/p>\n<p><h3>\u4e8c\u3001\u4f7f\u7528Pynput\u5e93<\/h3>\n<\/p>\n<p><p>Pynput \u662f\u4e00\u4e2a\u7528\u4e8e\u63a7\u5236\u548c\u76d1\u89c6\u8f93\u5165\u8bbe\u5907\u7684\u5e93\u3002\u5b83\u53ef\u4ee5\u76d1\u542c\u952e\u76d8\u4e8b\u4ef6\u5e76\u6267\u884c\u76f8\u5e94\u7684\u64cd\u4f5c\u3002<\/p>\n<\/p>\n<p><h4>1. \u5b89\u88c5Pynput<\/h4>\n<\/p>\n<p><p>\u9996\u5148\uff0c\u60a8\u9700\u8981\u5b89\u88c5Pynput\u5e93\u3002\u53ef\u4ee5\u4f7f\u7528\u4ee5\u4e0b\u547d\u4ee4\u6765\u5b89\u88c5\u5b83\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-bash\">pip install pynput<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>2. \u68c0\u6d4b\u6309\u952e\u88ab\u6309\u4e0b<\/h4>\n<\/p>\n<p><p>\u4f7f\u7528Pynput\u5e93\u68c0\u6d4b\u6309\u952e\u88ab\u6309\u4e0b\u7684\u57fa\u672c\u6b65\u9aa4\u5982\u4e0b\uff1a<\/p>\n<\/p>\n<ol>\n<li>\u5bfc\u5165<code>pynput.keyboard<\/code>\u6a21\u5757\u3002<\/li>\n<li>\u521b\u5efa\u4e00\u4e2a\u952e\u76d8\u76d1\u542c\u5668\u6765\u76d1\u542c\u6309\u952e\u4e8b\u4ef6\u3002<\/li>\n<li>\u5b9a\u4e49\u6309\u952e\u6309\u4e0b\u548c\u677e\u5f00\u65f6\u7684\u56de\u8c03\u51fd\u6570\u3002<\/li>\n<\/ol>\n<p><p>\u4ee5\u4e0b\u662f\u4e00\u4e2a\u793a\u4f8b\u4ee3\u7801\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;a&#39;:<\/p>\n<p>            print(&quot;\u6309\u952e &#39;a&#39; \u88ab\u6309\u4e0b&quot;)<\/p>\n<p>    except AttributeError:<\/p>\n<p>        pass<\/p>\n<p>def on_release(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>\u521b\u5efa\u952e\u76d8\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\u793a\u4f8b\u4e2d\uff0c\u6211\u4eec\u5b9a\u4e49\u4e86\u4e24\u4e2a\u56de\u8c03\u51fd\u6570<code>on_press<\/code>\u548c<code>on_release<\/code>\uff0c\u5206\u522b\u5728\u6309\u952e\u6309\u4e0b\u548c\u677e\u5f00\u65f6\u88ab\u8c03\u7528\u3002\u5728<code>on_press<\/code>\u51fd\u6570\u4e2d\uff0c\u6211\u4eec\u68c0\u67e5\u6309\u952e\u662f\u5426\u662f\u5b57\u7b26&#39;a&#39;\uff0c\u5982\u679c\u662f\uff0c\u5219\u6253\u5370\u4e00\u6761\u6d88\u606f\u3002\u5728<code>on_release<\/code>\u51fd\u6570\u4e2d\uff0c\u6211\u4eec\u68c0\u67e5\u6309\u952e\u662f\u5426\u662fEsc\u952e\uff0c\u5982\u679c\u662f\uff0c\u5219\u8fd4\u56deFalse\u4ee5\u505c\u6b62\u76d1\u542c\u3002\u6700\u540e\uff0c\u6211\u4eec\u521b\u5efa\u4e00\u4e2a\u952e\u76d8\u76d1\u542c\u5668\u5e76\u542f\u52a8\u5b83\u3002<\/p>\n<\/p>\n<p><h3>\u4e09\u3001\u4f7f\u7528Keyboard\u5e93<\/h3>\n<\/p>\n<p><p>Keyboard \u662f\u4e00\u4e2a\u7b80\u5355\u4e14\u529f\u80fd\u5f3a\u5927\u7684\u5e93\uff0c\u7528\u4e8e\u8bb0\u5f55\u548c\u6a21\u62df\u952e\u76d8\u4e8b\u4ef6\u3002\u5b83\u4e0d\u4ec5\u53ef\u4ee5\u68c0\u6d4b\u6309\u952e\u88ab\u6309\u4e0b\uff0c\u8fd8\u53ef\u4ee5\u6a21\u62df\u6309\u952e\u3002<\/p>\n<\/p>\n<p><h4>1. \u5b89\u88c5Keyboard<\/h4>\n<\/p>\n<p><p>\u9996\u5148\uff0c\u60a8\u9700\u8981\u5b89\u88c5Keyboard\u5e93\u3002\u53ef\u4ee5\u4f7f\u7528\u4ee5\u4e0b\u547d\u4ee4\u6765\u5b89\u88c5\u5b83\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-bash\">pip install keyboard<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>2. \u68c0\u6d4b\u6309\u952e\u88ab\u6309\u4e0b<\/h4>\n<\/p>\n<p><p>\u4f7f\u7528Keyboard\u5e93\u68c0\u6d4b\u6309\u952e\u88ab\u6309\u4e0b\u7684\u57fa\u672c\u6b65\u9aa4\u5982\u4e0b\uff1a<\/p>\n<\/p>\n<ol>\n<li>\u5bfc\u5165<code>keyboard<\/code>\u6a21\u5757\u3002<\/li>\n<li>\u4f7f\u7528<code>keyboard.is_pressed()<\/code>\u65b9\u6cd5\u6765\u68c0\u67e5\u67d0\u4e2a\u6309\u952e\u662f\u5426\u88ab\u6309\u4e0b\u3002<\/li>\n<\/ol>\n<p><p>\u4ee5\u4e0b\u662f\u4e00\u4e2a\u793a\u4f8b\u4ee3\u7801\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import keyboard<\/p>\n<p>print(&quot;\u6309\u4e0b &#39;q&#39; \u9000\u51fa\u7a0b\u5e8f&quot;)<\/p>\n<p>while True:<\/p>\n<p>    if keyboard.is_pressed(&#39;q&#39;):<\/p>\n<p>        print(&quot;\u6309\u952e &#39;q&#39; \u88ab\u6309\u4e0b&quot;)<\/p>\n<p>        break<\/p>\n<p>    elif keyboard.is_pressed(&#39;a&#39;):<\/p>\n<p>        print(&quot;\u6309\u952e &#39;a&#39; \u88ab\u6309\u4e0b&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u793a\u4f8b\u4e2d\uff0c\u6211\u4eec\u4f7f\u7528<code>keyboard.is_pressed()<\/code>\u65b9\u6cd5\u6765\u68c0\u67e5\u6309\u952e\u662f\u5426\u88ab\u6309\u4e0b\u3002\u5982\u679c\u68c0\u6d4b\u5230\u6309\u952e&#39;q&#39;\u88ab\u6309\u4e0b\uff0c\u5219\u6253\u5370\u4e00\u6761\u6d88\u606f\u5e76\u9000\u51fa\u7a0b\u5e8f\uff1b\u5982\u679c\u68c0\u6d4b\u5230\u6309\u952e&#39;a&#39;\u88ab\u6309\u4e0b\uff0c\u5219\u6253\u5370\u4e00\u6761\u6d88\u606f\u3002<\/p>\n<\/p>\n<p><h3>\u56db\u3001\u603b\u7ed3<\/h3>\n<\/p>\n<p><p>\u68c0\u6d4b\u6309\u952e\u88ab\u6309\u4e0b\u7684\u65b9\u6cd5\u6709\u591a\u79cd\uff0c\u672c\u6587\u4ecb\u7ecd\u4e86\u4f7f\u7528Pygame\u5e93\u3001Pynput\u5e93\u548cKeyboard\u5e93\u7684\u4e09\u79cd\u65b9\u6cd5\u3002<strong>Pygame\u5e93\u9002\u5408\u4e8e\u9700\u8981\u8fdb\u884c\u590d\u6742\u56fe\u5f62\u754c\u9762\u548c\u6e38\u620f\u5f00\u53d1\u7684\u573a\u666f\uff0cPynput\u5e93\u9002\u5408\u4e8e\u9700\u8981\u76d1\u542c\u548c\u63a7\u5236\u8f93\u5165\u8bbe\u5907\u7684\u573a\u666f\uff0cKeyboard\u5e93\u9002\u5408\u4e8e\u9700\u8981\u7b80\u5355\u68c0\u6d4b\u548c\u6a21\u62df\u6309\u952e\u4e8b\u4ef6\u7684\u573a\u666f<\/strong>\u3002\u6839\u636e\u5177\u4f53\u9700\u6c42\u9009\u62e9\u5408\u9002\u7684\u65b9\u6cd5\u53ef\u4ee5\u63d0\u9ad8\u5f00\u53d1\u6548\u7387\u548c\u4ee3\u7801\u8d28\u91cf\u3002<\/p>\n<\/p>\n<h2><strong>\u76f8\u5173\u95ee\u7b54FAQs\uff1a<\/strong><\/h2>\n<p> <strong>\u5982\u4f55\u4f7f\u7528Python\u68c0\u6d4b\u952e\u76d8\u8f93\u5165\uff1f<\/strong><br \/>\u5728Python\u4e2d\uff0c\u53ef\u4ee5\u4f7f\u7528\u5e93\u5982<code>pynput<\/code>\u548c<code>keyboard<\/code>\u6765\u76d1\u6d4b\u952e\u76d8\u8f93\u5165\u3002<code>pynput<\/code>\u63d0\u4f9b\u4e86\u7b80\u5355\u7684API\u6765\u76d1\u542c\u6309\u952e\u4e8b\u4ef6\uff0c\u800c<code>keyboard<\/code>\u5e93\u5219\u5141\u8bb8\u4f60\u66f4\u76f4\u63a5\u5730\u5904\u7406\u6309\u952e\u4e8b\u4ef6\u3002\u4f60\u53ef\u4ee5\u901a\u8fc7\u5b89\u88c5\u8fd9\u4e9b\u5e93\u6765\u5f00\u59cb\uff0c\u4f8b\u5982\u4f7f\u7528<code>pip install pynput<\/code>\u6216<code>pip install keyboard<\/code>\u3002<\/p>\n<p><strong>\u5728Python\u4e2d\u5982\u4f55\u5904\u7406\u591a\u4e2a\u6309\u952e\u7684\u540c\u65f6\u6309\u4e0b\uff1f<\/strong><br \/>\u5982\u679c\u4f60\u60f3\u68c0\u6d4b\u591a\u4e2a\u6309\u952e\u540c\u65f6\u6309\u4e0b\uff0c\u53ef\u4ee5\u4f7f\u7528<code>keyboard<\/code>\u5e93\u4e2d\u7684<code>is_pressed()<\/code>\u51fd\u6570\u3002\u8be5\u51fd\u6570\u53ef\u4ee5\u7528\u6765\u68c0\u67e5\u7279\u5b9a\u7684\u952e\u662f\u5426\u88ab\u6309\u4e0b\u3002\u4f60\u53ef\u4ee5\u5728\u4e00\u4e2a\u5faa\u73af\u4e2d\u7ed3\u5408\u4f7f\u7528\u8fd9\u4e2a\u51fd\u6570\uff0c\u6765\u76d1\u6d4b\u591a\u4e2a\u6309\u952e\u7684\u72b6\u6001\u3002\u4f8b\u5982\uff0c\u4f60\u53ef\u4ee5\u7f16\u5199\u4ee3\u7801\u6765\u68c0\u6d4b\u201cShift\u201d\u548c\u201cZ\u201d\u540c\u65f6\u88ab\u6309\u4e0b\u7684\u60c5\u51b5\u3002<\/p>\n<p><strong>\u5982\u4f55\u5728Python\u7a0b\u5e8f\u4e2d\u6267\u884c\u6309\u952e\u89e6\u53d1\u7684\u64cd\u4f5c\uff1f<\/strong><br \/>\u901a\u8fc7\u4f7f\u7528<code>pynput<\/code>\u5e93\uff0c\u4f60\u53ef\u4ee5\u8f7b\u677e\u5730\u5728\u7279\u5b9a\u6309\u952e\u88ab\u6309\u4e0b\u65f6\u6267\u884c\u67d0\u4e9b\u64cd\u4f5c\u3002\u53ef\u4ee5\u521b\u5efa\u4e00\u4e2a\u952e\u76d8\u76d1\u542c\u5668\uff0c\u5f53\u68c0\u6d4b\u5230\u7279\u5b9a\u6309\u952e\u88ab\u6309\u4e0b\u65f6\uff0c\u8c03\u7528\u6307\u5b9a\u7684\u51fd\u6570\u3002\u4f8b\u5982\uff0c\u6309\u4e0b\u201cEsc\u201d\u952e\u53ef\u4ee5\u7528\u6765\u9000\u51fa\u7a0b\u5e8f\uff0c\u800c\u6309\u4e0b\u201cEnter\u201d\u952e\u53ef\u4ee5\u7528\u6765\u63d0\u4ea4\u6570\u636e\u6216\u6267\u884c\u5176\u4ed6\u64cd\u4f5c\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"Python\u68c0\u6d4b\u67d0\u4e2a\u6309\u952e\u88ab\u6309\u4e0b\u7684\u65b9\u6cd5\u6709\u591a\u79cd\uff1a\u4f7f\u7528Pygame\u5e93\u3001\u4f7f\u7528Pynput\u5e93\u3001\u4f7f\u7528Keyboard\u5e93\u3002\u5176 [&hellip;]","protected":false},"author":3,"featured_media":1029821,"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\/1029812"}],"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=1029812"}],"version-history":[{"count":"1","href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1029812\/revisions"}],"predecessor-version":[{"id":1029825,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1029812\/revisions\/1029825"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/1029821"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=1029812"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=1029812"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=1029812"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}