{"id":1164924,"date":"2025-01-15T15:13:54","date_gmt":"2025-01-15T07:13:54","guid":{"rendered":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/1164924.html"},"modified":"2025-01-15T15:13:57","modified_gmt":"2025-01-15T07:13:57","slug":"python%e5%a6%82%e4%bd%95%e5%81%9a%e5%88%b0%e8%99%9a%e6%8b%9f%e6%8c%89%e9%94%ae","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/1164924.html","title":{"rendered":"Python\u5982\u4f55\u505a\u5230\u865a\u62df\u6309\u952e"},"content":{"rendered":"<p style=\"text-align:center;\" ><img decoding=\"async\" src=\"https:\/\/cdn-kb.worktile.com\/kb\/wp-content\/uploads\/2024\/04\/25205408\/9a129726-8afe-4063-8e55-10df98e0c584.webp\" alt=\"Python\u5982\u4f55\u505a\u5230\u865a\u62df\u6309\u952e\" \/><\/p>\n<p><p> Python\u53ef\u4ee5\u901a\u8fc7\u591a\u79cd\u65b9\u5f0f\u5b9e\u73b0\u865a\u62df\u6309\u952e\u64cd\u4f5c\uff0c\u4f8b\u5982<strong>\u4f7f\u7528PyAutoGUI\u5e93\u3001Pynput\u5e93<\/strong>\u7b49\u3002<strong>PyAutoGUI\u5e93<\/strong>\u662f\u4e00\u4e2a\u81ea\u52a8\u5316\u5e93\uff0c\u53ef\u4ee5\u7528\u4e8e\u6a21\u62df\u9f20\u6807\u548c\u952e\u76d8\u64cd\u4f5c\u3002<strong>Pynput\u5e93<\/strong>\u4e5f\u63d0\u4f9b\u4e86\u952e\u76d8\u548c\u9f20\u6807\u7684\u63a7\u5236\u529f\u80fd\u3002\u4e0b\u9762\u5c06\u8be6\u7ec6\u4ecb\u7ecd\u5982\u4f55\u4f7f\u7528PyAutoGUI\u5e93\u5b9e\u73b0\u865a\u62df\u6309\u952e\u64cd\u4f5c\u3002<\/p>\n<\/p>\n<p><p><strong>\u4f7f\u7528PyAutoGUI\u5e93\u5b9e\u73b0\u865a\u62df\u6309\u952e\u64cd\u4f5c\u7684\u6b65\u9aa4\u5982\u4e0b\uff1a<\/strong><\/p>\n<\/p>\n<ol>\n<li><strong>\u5b89\u88c5PyAutoGUI\u5e93<\/strong>\uff1a\u53ef\u4ee5\u901a\u8fc7pip\u547d\u4ee4\u5b89\u88c5PyAutoGUI\u5e93\u3002<\/li>\n<li><strong>\u5bfc\u5165\u5e93<\/strong>\uff1a\u5728Python\u811a\u672c\u4e2d\u5bfc\u5165PyAutoGUI\u5e93\u3002<\/li>\n<li><strong>\u6a21\u62df\u6309\u952e\u64cd\u4f5c<\/strong>\uff1a\u4f7f\u7528PyAutoGUI\u5e93\u7684<code>press<\/code>\u65b9\u6cd5\u6a21\u62df\u6309\u952e\u6309\u4e0b\u64cd\u4f5c\u3002<\/li>\n<\/ol>\n<p><p>\u5177\u4f53\u6b65\u9aa4\u5982\u4e0b\uff1a<\/p>\n<\/p>\n<p><h3>\u4e00\u3001\u5b89\u88c5PyAutoGUI\u5e93<\/h3>\n<\/p>\n<p><p>\u5728\u7ec8\u7aef\u6216\u547d\u4ee4\u63d0\u793a\u7b26\u4e2d\u8fd0\u884c\u4ee5\u4e0b\u547d\u4ee4\u4ee5\u5b89\u88c5PyAutoGUI\u5e93\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-bash\">pip install pyautogui<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u4e8c\u3001\u5bfc\u5165PyAutoGUI\u5e93<\/h3>\n<\/p>\n<p><p>\u5728Python\u811a\u672c\u4e2d\u5bfc\u5165PyAutoGUI\u5e93\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import pyautogui<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u4e09\u3001\u6a21\u62df\u6309\u952e\u64cd\u4f5c<\/h3>\n<\/p>\n<p><p>PyAutoGUI\u5e93\u63d0\u4f9b\u4e86\u591a\u79cd\u65b9\u6cd5\u6765\u6a21\u62df\u6309\u952e\u64cd\u4f5c\uff1a<\/p>\n<\/p>\n<ol>\n<li><strong>\u5355\u952e\u6309\u4e0b<\/strong>\uff1a<\/li>\n<\/ol>\n<p><pre><code class=\"language-python\">pyautogui.press(&#39;a&#39;)  # \u6a21\u62df\u6309\u4e0b\u5b57\u6bcd&#39;a&#39;\u952e<\/p>\n<p><\/code><\/pre>\n<\/p>\n<ol start=\"2\">\n<li><strong>\u7ec4\u5408\u952e\u6309\u4e0b<\/strong>\uff1a<\/li>\n<\/ol>\n<p><pre><code class=\"language-python\">pyautogui.hotkey(&#39;ctrl&#39;, &#39;c&#39;)  # \u6a21\u62df\u6309\u4e0bCtrl+C\u7ec4\u5408\u952e<\/p>\n<p><\/code><\/pre>\n<\/p>\n<ol start=\"3\">\n<li><strong>\u952e\u76d8\u6309\u952e\u5e8f\u5217<\/strong>\uff1a<\/li>\n<\/ol>\n<p><pre><code class=\"language-python\">pyautogui.typewrite(&#39;Hello, World!&#39;)  # \u6a21\u62df\u952e\u5165\u4e00\u4e32\u5b57\u7b26<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u56db\u3001Pynput\u5e93<\/h3>\n<\/p>\n<p><p>Pynput\u5e93\u4e5f\u662f\u4e00\u4e2a\u5f3a\u5927\u7684\u5e93\uff0c\u53ef\u4ee5\u7528\u4e8e\u63a7\u5236\u548c\u76d1\u63a7\u952e\u76d8\u548c\u9f20\u6807\u3002\u4ee5\u4e0b\u662f\u4f7f\u7528Pynput\u5e93\u5b9e\u73b0\u865a\u62df\u6309\u952e\u64cd\u4f5c\u7684\u6b65\u9aa4\u3002<\/p>\n<\/p>\n<ol>\n<li><strong>\u5b89\u88c5Pynput\u5e93<\/strong>\uff1a<\/li>\n<\/ol>\n<p><pre><code class=\"language-bash\">pip install pynput<\/p>\n<p><\/code><\/pre>\n<\/p>\n<ol start=\"2\">\n<li><strong>\u5bfc\u5165\u5e93<\/strong>\uff1a<\/li>\n<\/ol>\n<p><pre><code class=\"language-python\">from pynput.keyboard import Key, Controller<\/p>\n<p><\/code><\/pre>\n<\/p>\n<ol start=\"3\">\n<li><strong>\u6a21\u62df\u6309\u952e\u64cd\u4f5c<\/strong>\uff1a<\/li>\n<\/ol>\n<p><pre><code class=\"language-python\">keyboard = Controller()<\/p>\n<p>keyboard.press(&#39;a&#39;)  # \u6a21\u62df\u6309\u4e0b\u5b57\u6bcd&#39;a&#39;\u952e<\/p>\n<p>keyboard.release(&#39;a&#39;)  # \u91ca\u653e\u5b57\u6bcd&#39;a&#39;\u952e<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u4e94\u3001\u7efc\u5408\u793a\u4f8b<\/h3>\n<\/p>\n<p><p>\u4ee5\u4e0b\u662f\u4e00\u4e2a\u7efc\u5408\u793a\u4f8b\uff0c\u5c55\u793a\u5982\u4f55\u4f7f\u7528PyAutoGUI\u548cPynput\u5e93\u6765\u5b9e\u73b0\u66f4\u590d\u6742\u7684\u865a\u62df\u6309\u952e\u64cd\u4f5c\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import pyautogui<\/p>\n<p>from pynput.keyboard import Key, Controller<\/p>\n<h2><strong>\u4f7f\u7528PyAutoGUI\u6a21\u62df\u7ec4\u5408\u952e\u6309\u4e0b<\/strong><\/h2>\n<p>pyautogui.hotkey(&#39;ctrl&#39;, &#39;alt&#39;, &#39;del&#39;)<\/p>\n<h2><strong>\u4f7f\u7528Pynput\u6a21\u62df\u952e\u76d8\u8f93\u5165<\/strong><\/h2>\n<p>keyboard = Controller()<\/p>\n<p>keyboard.press(Key.shift)<\/p>\n<p>keyboard.press(&#39;a&#39;)<\/p>\n<p>keyboard.release(&#39;a&#39;)<\/p>\n<p>keyboard.release(Key.shift)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u516d\u3001\u5e94\u7528\u573a\u666f<\/h3>\n<\/p>\n<ol>\n<li><strong>\u81ea\u52a8\u5316\u6d4b\u8bd5<\/strong>\uff1a\u865a\u62df\u6309\u952e\u64cd\u4f5c\u5e38\u7528\u4e8e\u81ea\u52a8\u5316\u6d4b\u8bd5\uff0c\u6a21\u62df\u7528\u6237\u8f93\u5165\u3002<\/li>\n<li><strong>\u6e38\u620f\u811a\u672c<\/strong>\uff1a\u5728\u6e38\u620f\u4e2d\u6a21\u62df\u6309\u952e\u64cd\u4f5c\uff0c\u5b9e\u73b0\u81ea\u52a8\u5316\u811a\u672c\u3002<\/li>\n<li><strong>\u65e5\u5e38\u4efb\u52a1\u81ea\u52a8\u5316<\/strong>\uff1a\u5982\u81ea\u52a8\u586b\u5199\u8868\u5355\u3001\u81ea\u52a8\u53d1\u9001\u90ae\u4ef6\u7b49\u3002<\/li>\n<\/ol>\n<p><h3>\u4e03\u3001\u5b89\u5168\u548c\u4f26\u7406\u8003\u8651<\/h3>\n<\/p>\n<p><p>\u5728\u4f7f\u7528\u865a\u62df\u6309\u952e\u64cd\u4f5c\u65f6\uff0c\u9700\u8981\u6ce8\u610f\u4ee5\u4e0b\u51e0\u70b9\uff1a<\/p>\n<\/p>\n<ol>\n<li><strong>\u907f\u514d\u6ee5\u7528<\/strong>\uff1a\u4e0d\u8981\u5c06\u865a\u62df\u6309\u952e\u64cd\u4f5c\u7528\u4e8e\u975e\u6cd5\u7528\u9014\uff0c\u5982\u672a\u7ecf\u6388\u6743\u7684\u8bbf\u95ee\u3002<\/li>\n<li><strong>\u7528\u6237\u9690\u79c1<\/strong>\uff1a\u786e\u4fdd\u4e0d\u4fb5\u72af\u7528\u6237\u9690\u79c1\uff0c\u4e0d\u8bb0\u5f55\u6216\u53d1\u9001\u654f\u611f\u4fe1\u606f\u3002<\/li>\n<li><strong>\u5408\u6cd5\u5408\u89c4<\/strong>\uff1a\u9075\u5b88\u76f8\u5173\u6cd5\u5f8b\u6cd5\u89c4\uff0c\u786e\u4fdd\u64cd\u4f5c\u5408\u6cd5\u5408\u89c4\u3002<\/li>\n<\/ol>\n<p><h3>\u516b\u3001\u5e38\u89c1\u95ee\u9898\u548c\u89e3\u51b3\u65b9\u6cd5<\/h3>\n<\/p>\n<ol>\n<li><strong>\u6309\u952e\u65e0\u6cd5\u8bc6\u522b<\/strong>\uff1a\u68c0\u67e5\u6309\u952e\u540d\u79f0\u662f\u5426\u6b63\u786e\uff0c\u53c2\u8003PyAutoGUI\u6216Pynput\u7684\u6587\u6863\u3002<\/li>\n<li><strong>\u5ef6\u8fdf\u548c\u54cd\u5e94\u95ee\u9898<\/strong>\uff1a\u5728\u6a21\u62df\u590d\u6742\u64cd\u4f5c\u65f6\uff0c\u53ef\u4ee5\u52a0\u5165\u9002\u5f53\u7684\u5ef6\u8fdf\uff0c\u786e\u4fdd\u64cd\u4f5c\u987a\u5229\u8fdb\u884c\u3002<\/li>\n<\/ol>\n<p><pre><code class=\"language-python\">import time<\/p>\n<p>pyautogui.press(&#39;a&#39;)<\/p>\n<p>time.sleep(0.5)<\/p>\n<p>pyautogui.press(&#39;b&#39;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<ol start=\"3\">\n<li><strong>\u6743\u9650\u95ee\u9898<\/strong>\uff1a\u5728\u67d0\u4e9b\u64cd\u4f5c\u7cfb\u7edf\u4e2d\uff0c\u53ef\u80fd\u9700\u8981\u63d0\u5347\u6743\u9650\u624d\u80fd\u6a21\u62df\u6309\u952e\u64cd\u4f5c\u3002<\/li>\n<\/ol>\n<p><p>\u901a\u8fc7\u4e0a\u8ff0\u65b9\u6cd5\uff0c\u6211\u4eec\u53ef\u4ee5\u5728Python\u4e2d\u5b9e\u73b0\u865a\u62df\u6309\u952e\u64cd\u4f5c\uff0c\u65b9\u4fbf\u5730\u8fdb\u884c\u5404\u79cd\u81ea\u52a8\u5316\u4efb\u52a1\u3002PyAutoGUI\u548cPynput\u5e93\u90fd\u63d0\u4f9b\u4e86\u5f3a\u5927\u7684\u529f\u80fd\uff0c\u53ef\u4ee5\u6ee1\u8db3\u4e0d\u540c\u573a\u666f\u7684\u9700\u6c42\u3002\u5e0c\u671b\u672c\u6587\u5bf9\u4f60\u7406\u89e3\u548c\u4f7f\u7528\u865a\u62df\u6309\u952e\u64cd\u4f5c\u6709\u6240\u5e2e\u52a9\u3002<\/p>\n<\/p>\n<h2><strong>\u76f8\u5173\u95ee\u7b54FAQs\uff1a<\/strong><\/h2>\n<p> <strong>1. \u5982\u4f55\u4f7f\u7528Python\u5b9e\u73b0\u865a\u62df\u6309\u952e\u7684\u6a21\u62df\uff1f<\/strong><br \/>Python\u53ef\u4ee5\u901a\u8fc7\u4e00\u4e9b\u7b2c\u4e09\u65b9\u5e93\u6765\u6a21\u62df\u865a\u62df\u6309\u952e\u64cd\u4f5c\u3002\u5e38\u7528\u7684\u5e93\u5305\u62ec<code>pyautogui<\/code>\u548c<code>pynput<\/code>\u3002\u4f7f\u7528<code>pyautogui<\/code>\uff0c\u4f60\u53ef\u4ee5\u901a\u8fc7\u7b80\u5355\u7684\u51fd\u6570\u8c03\u7528\u6765\u5b9e\u73b0\u6309\u952e\u64cd\u4f5c\uff0c\u5982<code>pyautogui.press(&#39;a&#39;)<\/code>\u53ef\u4ee5\u6a21\u62df\u6309\u4e0b\u5b57\u6bcd&#39;A&#39;\u3002\u800c<code>pynput<\/code>\u5219\u66f4\u9002\u5408\u5904\u7406\u952e\u76d8\u4e8b\u4ef6\uff0c\u5141\u8bb8\u4f60\u76d1\u542c\u548c\u63a7\u5236\u952e\u76d8\u8f93\u5165\u3002<\/p>\n<p><strong>2. \u5728Python\u4e2d\u5982\u4f55\u5904\u7406\u7ec4\u5408\u952e\u7684\u865a\u62df\u6309\u952e\u64cd\u4f5c\uff1f<\/strong><br \/>\u7ec4\u5408\u952e\u7684\u6a21\u62df\u53ef\u4ee5\u901a\u8fc7<code>pyautogui<\/code>\u548c<code>pynput<\/code>\u5e93\u5b9e\u73b0\u3002\u5728<code>pyautogui<\/code>\u4e2d\uff0c\u53ef\u4ee5\u4f7f\u7528<code>pyautogui.hotkey(&#39;ctrl&#39;, &#39;c&#39;)<\/code>\u6765\u6a21\u62df\u6309\u4e0bCtrl+C\u7684\u64cd\u4f5c\u3002\u800c\u5728<code>pynput<\/code>\u4e2d\uff0c\u53ef\u4ee5\u4f7f\u7528<code>Controller<\/code>\u7c7b\u6765\u6309\u4e0b\u548c\u91ca\u653e\u7ec4\u5408\u952e\uff0c\u793a\u4f8b\u4ee3\u7801\u5982\u4e0b\uff1a  <\/p>\n<pre><code class=\"language-python\">from pynput.keyboard import Key, Controller\n\nkeyboard = Controller()\nkeyboard.press(Key.ctrl)\nkeyboard.press(&#39;c&#39;)\nkeyboard.release(&#39;c&#39;)\nkeyboard.release(Key.ctrl)\n<\/code><\/pre>\n<p><strong>3. \u4f7f\u7528Python\u6a21\u62df\u6309\u952e\u65f6\uff0c\u5982\u4f55\u786e\u4fdd\u6309\u952e\u64cd\u4f5c\u7684\u53ef\u9760\u6027\uff1f<\/strong><br \/>\u786e\u4fdd\u865a\u62df\u6309\u952e\u64cd\u4f5c\u7684\u53ef\u9760\u6027\uff0c\u53ef\u4ee5\u8003\u8651\u6dfb\u52a0\u9002\u5f53\u7684\u5ef6\u65f6\u3002\u4f7f\u7528<code>time.sleep()<\/code>\u51fd\u6570\u53ef\u4ee5\u5728\u6309\u952e\u64cd\u4f5c\u4e4b\u95f4\u6dfb\u52a0\u95f4\u9694\uff0c\u907f\u514d\u7531\u4e8e\u64cd\u4f5c\u8fc7\u5feb\u5bfc\u81f4\u7684\u9519\u8bef\u3002\u540c\u65f6\uff0c\u786e\u4fdd\u5728\u6267\u884c\u865a\u62df\u6309\u952e\u64cd\u4f5c\u524d\uff0c\u76ee\u6807\u5e94\u7528\u7a0b\u5e8f\u5904\u4e8e\u6d3b\u52a8\u72b6\u6001\uff0c\u4ee5\u4fbf\u63a5\u6536\u8f93\u5165\u3002\u5728\u8fdb\u884c\u590d\u6742\u7684\u64cd\u4f5c\u65f6\uff0c\u5efa\u8bae\u9010\u6b65\u8c03\u8bd5\uff0c\u89c2\u5bdf\u6bcf\u4e00\u6b65\u7684\u6267\u884c\u6548\u679c\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"Python\u53ef\u4ee5\u901a\u8fc7\u591a\u79cd\u65b9\u5f0f\u5b9e\u73b0\u865a\u62df\u6309\u952e\u64cd\u4f5c\uff0c\u4f8b\u5982\u4f7f\u7528PyAutoGUI\u5e93\u3001Pynput\u5e93\u7b49\u3002PyAutoGU [&hellip;]","protected":false},"author":3,"featured_media":1164927,"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\/1164924"}],"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=1164924"}],"version-history":[{"count":"1","href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1164924\/revisions"}],"predecessor-version":[{"id":1164930,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1164924\/revisions\/1164930"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/1164927"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=1164924"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=1164924"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=1164924"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}