{"id":1096722,"date":"2025-01-08T15:04:17","date_gmt":"2025-01-08T07:04:17","guid":{"rendered":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/1096722.html"},"modified":"2025-01-08T15:04:20","modified_gmt":"2025-01-08T07:04:20","slug":"python%e4%b8%adctrl%e5%bf%ab%e6%8d%b7%e9%94%ae%e5%a6%82%e4%bd%95%e8%a1%a8%e7%a4%ba-2","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/1096722.html","title":{"rendered":"python\u4e2dctrl\u5feb\u6377\u952e\u5982\u4f55\u8868\u793a"},"content":{"rendered":"<p style=\"text-align:center;\" ><img decoding=\"async\" src=\"https:\/\/cdn-kb.worktile.com\/kb\/wp-content\/uploads\/2024\/04\/24211822\/bcff5e09-51c0-4b41-9aad-555ec610d051.webp\" alt=\"python\u4e2dctrl\u5feb\u6377\u952e\u5982\u4f55\u8868\u793a\" \/><\/p>\n<p><p> <strong>\u5728Python\u4e2d\uff0c\u8868\u793aCtrl\u5feb\u6377\u952e\u7684\u65b9\u5f0f\u4e3b\u8981\u6709\u4ee5\u4e0b\u51e0\u79cd\uff1a\u4f7f\u7528<code>keyboard<\/code>\u6a21\u5757\u3001\u4f7f\u7528<code>pyautogui<\/code>\u6a21\u5757\u3001\u4f7f\u7528<code>pynput<\/code>\u6a21\u5757<\/strong>\u3002\u5176\u4e2d\uff0c\u4f7f\u7528<code>keyboard<\/code>\u6a21\u5757\u662f\u6700\u5e38\u89c1\u548c\u7b80\u4fbf\u7684\u65b9\u6cd5\u3002\u63a5\u4e0b\u6765\uff0c\u6211\u4eec\u5c06\u8be6\u7ec6\u8ba8\u8bba\u8fd9\u51e0\u79cd\u65b9\u6cd5\uff0c\u5e76\u63d0\u4f9b\u4ee3\u7801\u793a\u4f8b\u3002<\/p>\n<\/p>\n<p><p>\u4e00\u3001\u4f7f\u7528<code>keyboard<\/code>\u6a21\u5757<\/p>\n<\/p>\n<p><p><code>keyboard<\/code>\u6a21\u5757\u662f\u4e00\u4e2a\u7528\u4e8e\u952e\u76d8\u63a7\u5236\u7684Python\u5e93\uff0c\u5b83\u53ef\u4ee5\u6a21\u62df\u952e\u76d8\u6309\u952e\u3001\u8bb0\u5f55\u6309\u952e\u4e8b\u4ef6\u7b49\u3002\u4f7f\u7528<code>keyboard<\/code>\u6a21\u5757\uff0c\u6211\u4eec\u53ef\u4ee5\u5f88\u5bb9\u6613\u5730\u5b9e\u73b0Ctrl\u5feb\u6377\u952e\u7684\u8868\u793a\u548c\u64cd\u4f5c\u3002<\/p>\n<\/p>\n<p><h3>\u5b89\u88c5\u548c\u5bfc\u5165\u6a21\u5757<\/h3>\n<\/p>\n<p><p>\u9996\u5148\uff0c\u9700\u8981\u5b89\u88c5<code>keyboard<\/code>\u6a21\u5757\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-bash\">pip install keyboard<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u7136\u540e\u5728Python\u811a\u672c\u4e2d\u5bfc\u5165\u8be5\u6a21\u5757\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import keyboard<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u4f7f\u7528Ctrl\u5feb\u6377\u952e<\/h3>\n<\/p>\n<p><p>\u5728<code>keyboard<\/code>\u6a21\u5757\u4e2d\uff0c\u53ef\u4ee5\u4f7f\u7528\u7ec4\u5408\u952e\u6765\u8868\u793aCtrl\u5feb\u6377\u952e\uff0c\u4f8b\u5982<code>Ctrl+C<\/code>\u3001<code>Ctrl+V<\/code>\u7b49\u3002\u4ee5\u4e0b\u662f\u4e00\u4e9b\u793a\u4f8b\u4ee3\u7801\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\"># \u6309\u4e0bCtrl+C\u7ec4\u5408\u952e<\/p>\n<p>keyboard.press_and_release(&#39;ctrl+c&#39;)<\/p>\n<h2><strong>\u76d1\u542cCtrl+S\u7ec4\u5408\u952e<\/strong><\/h2>\n<p>keyboard.add_hotkey(&#39;ctrl+s&#39;, lambda: print(&#39;Ctrl+S was pressed!&#39;))<\/p>\n<h2><strong>\u8fd0\u884c\u4e00\u4e2a\u65e0\u9650\u5faa\u73af\uff0c\u4fdd\u6301\u7a0b\u5e8f\u8fd0\u884c<\/strong><\/h2>\n<p>keyboard.w<a href=\"https:\/\/docs.pingcode.com\/blog\/59162.html\" target=\"_blank\">AI<\/a>t(&#39;esc&#39;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u4e0a\u9762\u7684\u4ee3\u7801\u4e2d\uff0c<code>press_and_release<\/code>\u65b9\u6cd5\u7528\u4e8e\u6a21\u62df\u6309\u4e0b\u548c\u91ca\u653e\u7ec4\u5408\u952e\uff0c\u800c<code>add_hotkey<\/code>\u65b9\u6cd5\u7528\u4e8e\u76d1\u542c\u7279\u5b9a\u7684\u7ec4\u5408\u952e\u5e76\u6267\u884c\u76f8\u5e94\u7684\u56de\u8c03\u51fd\u6570\u3002<\/p>\n<\/p>\n<p><p>\u4e8c\u3001\u4f7f\u7528<code>pyautogui<\/code>\u6a21\u5757<\/p>\n<\/p>\n<p><p><code>pyautogui<\/code>\u6a21\u5757\u662f\u4e00\u4e2a\u7528\u4e8e\u81ea\u52a8\u5316\u63a7\u5236\u9f20\u6807\u548c\u952e\u76d8\u7684Python\u5e93\u3002\u5b83\u4e5f\u53ef\u4ee5\u7528\u4e8e\u8868\u793a\u548c\u6a21\u62dfCtrl\u5feb\u6377\u952e\u3002<\/p>\n<\/p>\n<p><h3>\u5b89\u88c5\u548c\u5bfc\u5165\u6a21\u5757<\/h3>\n<\/p>\n<p><p>\u9996\u5148\uff0c\u9700\u8981\u5b89\u88c5<code>pyautogui<\/code>\u6a21\u5757\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-bash\">pip install pyautogui<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u7136\u540e\u5728Python\u811a\u672c\u4e2d\u5bfc\u5165\u8be5\u6a21\u5757\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import pyautogui<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u4f7f\u7528Ctrl\u5feb\u6377\u952e<\/h3>\n<\/p>\n<p><p>\u5728<code>pyautogui<\/code>\u6a21\u5757\u4e2d\uff0c\u53ef\u4ee5\u4f7f\u7528\u7ec4\u5408\u952e\u6765\u8868\u793aCtrl\u5feb\u6377\u952e\uff0c\u4f8b\u5982<code>Ctrl+C<\/code>\u3001<code>Ctrl+V<\/code>\u7b49\u3002\u4ee5\u4e0b\u662f\u4e00\u4e9b\u793a\u4f8b\u4ee3\u7801\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\"># \u6309\u4e0bCtrl\u952e<\/p>\n<p>pyautogui.keyDown(&#39;ctrl&#39;)<\/p>\n<h2><strong>\u6309\u4e0b\u5e76\u91ca\u653eC\u952e<\/strong><\/h2>\n<p>pyautogui.press(&#39;c&#39;)<\/p>\n<h2><strong>\u91ca\u653eCtrl\u952e<\/strong><\/h2>\n<p>pyautogui.keyUp(&#39;ctrl&#39;)<\/p>\n<h2><strong>\u8fd0\u884c\u4e00\u4e2a\u65e0\u9650\u5faa\u73af\uff0c\u4fdd\u6301\u7a0b\u5e8f\u8fd0\u884c<\/strong><\/h2>\n<p>while True:<\/p>\n<p>    pass<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u4e0a\u9762\u7684\u4ee3\u7801\u4e2d\uff0c<code>keyDown<\/code>\u65b9\u6cd5\u7528\u4e8e\u6309\u4e0bCtrl\u952e\uff0c<code>press<\/code>\u65b9\u6cd5\u7528\u4e8e\u6309\u4e0b\u5e76\u91ca\u653e\u6307\u5b9a\u7684\u952e\uff0c\u800c<code>keyUp<\/code>\u65b9\u6cd5\u7528\u4e8e\u91ca\u653eCtrl\u952e\u3002<\/p>\n<\/p>\n<p><p>\u4e09\u3001\u4f7f\u7528<code>pynput<\/code>\u6a21\u5757<\/p>\n<\/p>\n<p><p><code>pynput<\/code>\u6a21\u5757\u662f\u4e00\u4e2a\u7528\u4e8e\u63a7\u5236\u548c\u76d1\u89c6\u8f93\u5165\u8bbe\u5907\u7684Python\u5e93\u3002\u5b83\u53ef\u4ee5\u7528\u4e8e\u8868\u793a\u548c\u6a21\u62dfCtrl\u5feb\u6377\u952e\u3002<\/p>\n<\/p>\n<p><h3>\u5b89\u88c5\u548c\u5bfc\u5165\u6a21\u5757<\/h3>\n<\/p>\n<p><p>\u9996\u5148\uff0c\u9700\u8981\u5b89\u88c5<code>pynput<\/code>\u6a21\u5757\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-bash\">pip install pynput<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u7136\u540e\u5728Python\u811a\u672c\u4e2d\u5bfc\u5165\u8be5\u6a21\u5757\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">from pynput.keyboard import Key, Controller<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u4f7f\u7528Ctrl\u5feb\u6377\u952e<\/h3>\n<\/p>\n<p><p>\u5728<code>pynput<\/code>\u6a21\u5757\u4e2d\uff0c\u53ef\u4ee5\u4f7f\u7528\u7ec4\u5408\u952e\u6765\u8868\u793aCtrl\u5feb\u6377\u952e\uff0c\u4f8b\u5982<code>Ctrl+C<\/code>\u3001<code>Ctrl+V<\/code>\u7b49\u3002\u4ee5\u4e0b\u662f\u4e00\u4e9b\u793a\u4f8b\u4ee3\u7801\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\"># \u521b\u5efa\u952e\u76d8\u63a7\u5236\u5668<\/p>\n<p>keyboard = Controller()<\/p>\n<h2><strong>\u6309\u4e0bCtrl\u952e<\/strong><\/h2>\n<p>keyboard.press(Key.ctrl)<\/p>\n<h2><strong>\u6309\u4e0b\u5e76\u91ca\u653eC\u952e<\/strong><\/h2>\n<p>keyboard.press(&#39;c&#39;)<\/p>\n<p>keyboard.release(&#39;c&#39;)<\/p>\n<h2><strong>\u91ca\u653eCtrl\u952e<\/strong><\/h2>\n<p>keyboard.release(Key.ctrl)<\/p>\n<h2><strong>\u8fd0\u884c\u4e00\u4e2a\u65e0\u9650\u5faa\u73af\uff0c\u4fdd\u6301\u7a0b\u5e8f\u8fd0\u884c<\/strong><\/h2>\n<p>while True:<\/p>\n<p>    pass<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u4e0a\u9762\u7684\u4ee3\u7801\u4e2d\uff0c<code>press<\/code>\u65b9\u6cd5\u7528\u4e8e\u6309\u4e0b\u6307\u5b9a\u7684\u952e\uff0c\u800c<code>release<\/code>\u65b9\u6cd5\u7528\u4e8e\u91ca\u653e\u6307\u5b9a\u7684\u952e\u3002<\/p>\n<\/p>\n<p><h3>\u603b\u7ed3<\/h3>\n<\/p>\n<p><p>\u5728Python\u4e2d\uff0c\u8868\u793aCtrl\u5feb\u6377\u952e\u7684\u65b9\u6cd5\u4e3b\u8981\u6709\u4e09\u79cd\uff1a\u4f7f\u7528<code>keyboard<\/code>\u6a21\u5757\u3001\u4f7f\u7528<code>pyautogui<\/code>\u6a21\u5757\u3001\u4f7f\u7528<code>pynput<\/code>\u6a21\u5757\u3002\u6bcf\u79cd\u65b9\u6cd5\u90fd\u6709\u5176\u4f18\u70b9\u548c\u9002\u7528\u573a\u666f\uff0c\u53ef\u4ee5\u6839\u636e\u5177\u4f53\u9700\u6c42\u9009\u62e9\u5408\u9002\u7684\u65b9\u6cd5\u3002\u5e0c\u671b\u8fd9\u7bc7\u6587\u7ae0\u5bf9\u4f60\u6709\u6240\u5e2e\u52a9\uff01<\/p>\n<\/p>\n<h2><strong>\u76f8\u5173\u95ee\u7b54FAQs\uff1a<\/strong><\/h2>\n<p> <strong>\u5728Python\u4e2d\uff0c\u5982\u4f55\u4f7f\u7528Ctrl\u5feb\u6377\u952e\u6765\u63d0\u9ad8\u7f16\u7a0b\u6548\u7387\uff1f<\/strong><br \/>\u4f7f\u7528Ctrl\u5feb\u6377\u952e\u80fd\u591f\u663e\u8457\u63d0\u5347\u5728Python\u7f16\u7a0b\u65f6\u7684\u6548\u7387\u3002\u4f8b\u5982\uff0cCtrl+C\u7528\u4e8e\u590d\u5236\u9009\u4e2d\u7684\u4ee3\u7801\uff0cCtrl+V\u7528\u4e8e\u7c98\u8d34\uff0cCtrl+Z\u53ef\u4ee5\u64a4\u9500\u4e0a\u4e00\u6b65\u7684\u64cd\u4f5c\u3002\u8fd9\u4e9b\u57fa\u672c\u7684\u5feb\u6377\u952e\u53ef\u4ee5\u5e2e\u52a9\u5f00\u53d1\u8005\u66f4\u5feb\u901f\u5730\u8fdb\u884c\u4ee3\u7801\u7f16\u8f91\u548c\u8c03\u8bd5\u3002<\/p>\n<p><strong>\u6709\u54ea\u4e9b\u5e38\u7528\u7684Python IDE\u652f\u6301Ctrl\u5feb\u6377\u952e\uff1f<\/strong><br \/>\u591a\u79cdPython\u96c6\u6210\u5f00\u53d1\u73af\u5883\uff08IDE\uff09\u548c\u6587\u672c\u7f16\u8f91\u5668\u90fd\u652f\u6301Ctrl\u5feb\u6377\u952e\u3002\u5e38\u89c1\u7684IDE\u5982PyCharm\u3001Visual Studio Code\u548cJupyter Notebook\u90fd\u63d0\u4f9b\u4e30\u5bcc\u7684\u5feb\u6377\u952e\u8bbe\u7f6e\uff0c\u5e2e\u52a9\u5f00\u53d1\u8005\u9ad8\u6548\u7f16\u5199\u548c\u7ba1\u7406\u4ee3\u7801\u3002\u7528\u6237\u53ef\u4ee5\u901a\u8fc7\u67e5\u770bIDE\u7684\u6587\u6863\u6216\u8bbe\u7f6e\uff0c\u627e\u5230\u9002\u5408\u81ea\u5df1\u9700\u6c42\u7684\u5feb\u6377\u952e\u3002<\/p>\n<p><strong>\u5982\u4f55\u81ea\u5b9a\u4e49Python\u7f16\u8f91\u5668\u4e2d\u7684Ctrl\u5feb\u6377\u952e\uff1f<\/strong><br \/>\u8bb8\u591aPython\u7f16\u8f91\u5668\u5141\u8bb8\u7528\u6237\u81ea\u5b9a\u4e49\u5feb\u6377\u952e\uff0c\u4ee5\u9002\u5e94\u4e2a\u4eba\u7684\u7f16\u7a0b\u4e60\u60ef\u3002\u5728\u7f16\u8f91\u5668\u7684\u8bbe\u7f6e\u6216\u9996\u9009\u9879\u4e2d\uff0c\u901a\u5e38\u4f1a\u6709\u5feb\u6377\u952e\u914d\u7f6e\u9009\u9879\uff0c\u7528\u6237\u53ef\u4ee5\u6839\u636e\u81ea\u5df1\u7684\u9700\u8981\u66f4\u6539\u9ed8\u8ba4\u8bbe\u7f6e\uff0c\u4ee5\u4fbf\u66f4\u5feb\u901f\u3001\u987a\u7545\u5730\u8fdb\u884c\u7f16\u7a0b\u3002\u901a\u8fc7\u8fd9\u79cd\u65b9\u5f0f\uff0c\u7528\u6237\u53ef\u4ee5\u5c06\u5e38\u7528\u7684\u529f\u80fd\u6620\u5c04\u5230\u81ea\u5df1\u559c\u6b22\u7684Ctrl\u7ec4\u5408\u952e\u4e0a\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"\u5728Python\u4e2d\uff0c\u8868\u793aCtrl\u5feb\u6377\u952e\u7684\u65b9\u5f0f\u4e3b\u8981\u6709\u4ee5\u4e0b\u51e0\u79cd\uff1a\u4f7f\u7528keyboard\u6a21\u5757\u3001\u4f7f\u7528pyautogui\u6a21\u5757 [&hellip;]","protected":false},"author":3,"featured_media":1096726,"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\/1096722"}],"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=1096722"}],"version-history":[{"count":"1","href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1096722\/revisions"}],"predecessor-version":[{"id":1096728,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1096722\/revisions\/1096728"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/1096726"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=1096722"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=1096722"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=1096722"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}