{"id":18,"date":"2026-01-26T03:01:04","date_gmt":"2026-01-26T03:01:04","guid":{"rendered":"https:\/\/www.yangjifang.cn\/?p=18"},"modified":"2026-01-26T03:01:04","modified_gmt":"2026-01-26T03:01:04","slug":"python%e4%bd%bf%e7%94%a8pydirectinput%e6%a8%a1%e6%8b%9f%e6%93%8d%e4%bd%9c","status":"publish","type":"post","link":"https:\/\/www.yangjifang.cn\/index.php\/2026\/01\/26\/python%e4%bd%bf%e7%94%a8pydirectinput%e6%a8%a1%e6%8b%9f%e6%93%8d%e4%bd%9c\/","title":{"rendered":"python\u4f7f\u7528pydirectinput\u6a21\u62df\u64cd\u4f5c"},"content":{"rendered":"\n<pre class=\"wp-block-code\"><code>    import pydirectinput\nimport time\nimport random\nfrom typing import Tuple, Optional\n\nclass AutoClicker:\n    def __init__(self):\n        # \u521d\u59cb\u5316\u81ea\u52a8\u70b9\u51fb\u5668\n        # \u8bbe\u7f6e pydirectinput \u53c2\u6570\n        pydirectinput.FAILSAFE = True  # \u542f\u7528\u6545\u969c\u5b89\u5168\uff08\u79fb\u52a8\u9f20\u6807\u5230\u5de6\u4e0a\u89d2\u505c\u6b62\uff09\n        pydirectinput.PAUSE = 0.1  # \u64cd\u4f5c\u95f4\u9694\u65f6\u95f4\n\n    def click_at_position(self, x: int, y: int, button: str = \"left\", clicks: int = 1):\n        \"\"\"\n        \u5728\u6307\u5b9a\u4f4d\u7f6e\u70b9\u51fb\n\n        Args:\n            x: X \u5750\u6807\n            y: Y \u5750\u6807\n            button: \u9f20\u6807\u6309\u94ae (\"left\", \"right\", \"middle\")\n            clicks: \u70b9\u51fb\u6b21\u6570\n        \"\"\"\n        try:\n            pydirectinput.click(x, y, button=button, clicks=clicks)\n            print(f\"\u5728\u4f4d\u7f6e ({x}, {y}) \u70b9\u51fb\u4e86 {clicks} \u6b21 {button} \u952e\")\n        except Exception as e:\n            print(f\"\u70b9\u51fb\u5931\u8d25: {e}\")\n\n    def move_to_position(self, x: int, y: int):\n        \"\"\"\n        \u79fb\u52a8\u9f20\u6807\u5230\u6307\u5b9a\u4f4d\u7f6e\n\n        Args:\n            x: X \u5750\u6807\n            y: Y \u5750\u6807\n        \"\"\"\n        try:\n            pydirectinput.moveTo(x, y)\n            print(f\"\u9f20\u6807\u79fb\u52a8\u5230\u4f4d\u7f6e ({x}, {y})\")\n        except Exception as e:\n            print(f\"\u79fb\u52a8\u9f20\u6807\u5931\u8d25: {e}\")\n\n    def drag_and_drop(self, start_x: int, start_y: int, end_x: int, end_y: int):\n        \"\"\"\n        \u62d6\u62fd\u64cd\u4f5c\n\n        Args:\n            start_x: \u8d77\u59cb X \u5750\u6807\n            start_y: \u8d77\u59cb Y \u5750\u6807\n            end_x: \u7ed3\u675f X \u5750\u6807\n            end_y: \u7ed3\u675f Y \u5750\u6807\n        \"\"\"\n        try:\n            pydirectinput.moveTo(start_x, start_y)\n            pydirectinput.mouseDown(button=\"left\")\n            time.sleep(0.1)\n            pydirectinput.moveTo(end_x, end_y)\n            time.sleep(0.1)\n            pydirectinput.mouseUp(button=\"left\")\n            print(f\"\u4ece ({start_x}, {start_y}) \u62d6\u62fd\u5230 ({end_x}, {end_y})\")\n        except Exception as e:\n            print(f\"\u62d6\u62fd\u5931\u8d25: {e}\")\n\n    def double_click(self, x: int, y: int):\n        \"\"\"\n        \u5728\u6307\u5b9a\u4f4d\u7f6e\u53cc\u51fb\n\n        Args:\n            x: X \u5750\u6807\n            y: Y \u5750\u6807\n        \"\"\"\n        self.click_at_position(x, y, clicks=2)\n\n    def right_click(self, x: int, y: int):\n        \"\"\"\n        \u5728\u6307\u5b9a\u4f4d\u7f6e\u53f3\u952e\u70b9\u51fb\n\n        Args:\n            x: X \u5750\u6807\n            y: Y \u5750\u6807\n        \"\"\"\n        self.click_at_position(x, y, button=\"right\")\n\n    def scroll(self, x: int, y: int, direction: str = \"down\", clicks: int = 3):\n        \"\"\"\n        \u5728\u6307\u5b9a\u4f4d\u7f6e\u6eda\u52a8\n\n        Args:\n            x: X \u5750\u6807\n            y: Y \u5750\u6807\n            direction: \u6eda\u52a8\u65b9\u5411 (\"up\" \u6216 \"down\")\n            clicks: \u6eda\u52a8\u6b21\u6570\n        \"\"\"\n        try:\n            pydirectinput.moveTo(x, y)\n            if direction == \"down\":\n                pydirectinput.scroll(-clicks)  # \u5411\u4e0b\u6eda\u52a8\n            else:\n                pydirectinput.scroll(clicks)   # \u5411\u4e0a\u6eda\u52a8\n            print(f\"\u5728\u4f4d\u7f6e ({x}, {y}) {direction} \u6eda\u52a8\u4e86 {clicks} \u6b21\")\n        except Exception as e:\n            print(f\"\u6eda\u52a8\u5931\u8d25: {e}\")\n\n    def get_mouse_position(self) -&gt; Tuple&#91;int, int]:\n        \"\"\"\n        \u83b7\u53d6\u5f53\u524d\u9f20\u6807\u4f4d\u7f6e\n\n        Returns:\n            \u9f20\u6807\u5750\u6807 (x, y)\n        \"\"\"\n        try:\n            x, y = pydirectinput.position()\n            return (x, y)\n        except Exception as e:\n            print(f\"\u83b7\u53d6\u9f20\u6807\u4f4d\u7f6e\u5931\u8d25: {e}\")\n            return (0, 0)\n\n    def click_sequence(self, positions: list, delay: float = 1.0):\n        \"\"\"\n        \u6309\u987a\u5e8f\u70b9\u51fb\u591a\u4e2a\u4f4d\u7f6e\n\n        Args:\n            positions: \u4f4d\u7f6e\u5217\u8868\uff0c\u6bcf\u4e2a\u5143\u7d20\u4e3a (x, y) \u6216 (x, y, button, clicks)\n            delay: \u6bcf\u6b21\u70b9\u51fb\u4e4b\u95f4\u7684\u5ef6\u8fdf\u65f6\u95f4\n        \"\"\"\n        print(f\"\u5f00\u59cb\u6267\u884c\u70b9\u51fb\u5e8f\u5217\uff0c\u5171 {len(positions)} \u4e2a\u4f4d\u7f6e\")\n        for i, pos in enumerate(positions):\n            if len(pos) == 2:\n                x, y = pos\n                self.click_at_position(x, y)\n            elif len(pos) == 4:\n                x, y, button, clicks = pos\n                self.click_at_position(x, y, button, clicks)\n\n            if i &lt; len(positions) - 1:  # \u4e0d\u662f\u6700\u540e\u4e00\u4e2a\u4f4d\u7f6e\n                time.sleep(delay)\n                print(f\"\u7b49\u5f85 {delay} \u79d2...\")\n\n        print(\"\u70b9\u51fb\u5e8f\u5217\u6267\u884c\u5b8c\u6210\")\n\ndef demo_usage():\n    \"\"\"\u6f14\u793a\u4f7f\u7528\u65b9\u6cd5\"\"\"\n    print(\"=== PyDirectInput \u81ea\u52a8\u70b9\u51fb\u7a0b\u5e8f\u6f14\u793a ===\")\n    print(\"\u6ce8\u610f\uff1a\u79fb\u52a8\u9f20\u6807\u5230\u5c4f\u5e55\u5de6\u4e0a\u89d2\u53ef\u4ee5\u505c\u6b62\u7a0b\u5e8f\")\n\n    # \u521b\u5efa\u81ea\u52a8\u70b9\u51fb\u5668\u5b9e\u4f8b\n    clicker = AutoClicker()\n\n    # \u7b49\u5f85\u7528\u6237\u51c6\u5907\n    print(\"\\n\u7a0b\u5e8f\u5c06\u5728 3 \u79d2\u540e\u5f00\u59cb\u6f14\u793a...\")\n    time.sleep(3)\n\n    # \u83b7\u53d6\u5f53\u524d\u9f20\u6807\u4f4d\u7f6e\n    current_pos = clicker.get_mouse_position()\n    print(f\"\u5f53\u524d\u9f20\u6807\u4f4d\u7f6e: {current_pos}\")\n\n    # \u6f14\u793a\u5404\u79cd\u64cd\u4f5c\n    print(\"\\n1. \u79fb\u52a8\u9f20\u6807\u5230\u5c4f\u5e55\u4e2d\u592e\")\n    clicker.move_to_position(960, 540)  # \u5047\u8bbe 1920x1080 \u5c4f\u5e55\n\n    print(\"\\n2. \u5de6\u952e\u70b9\u51fb\")\n    clicker.click_at_position(960, 540)\n\n    print(\"\\n3. \u53f3\u952e\u70b9\u51fb\")\n    clicker.right_click(1000, 540)\n\n    print(\"\\n4. \u53cc\u51fb\")\n    clicker.double_click(1040, 540)\n\n    print(\"\\n5. \u62d6\u62fd\u64cd\u4f5c\")\n    clicker.drag_and_drop(800, 400, 1100, 600)\n\n    print(\"\\n6. \u6eda\u52a8\u64cd\u4f5c\")\n    clicker.scroll(960, 540, \"down\", 5)\n\n    print(\"\\n7. \u6267\u884c\u70b9\u51fb\u5e8f\u5217\")\n    positions = &#91;\n        (800, 400),\n        (900, 400),\n        (1000, 400),\n        (1100, 400),\n        (1200, 400)\n    ]\n    clicker.click_sequence(positions, delay=0.5)\n\n    print(\"\\n\u6f14\u793a\u5b8c\u6210\uff01\")\n\ndef custom_clicking():\n    \"\"\"\u81ea\u5b9a\u4e49\u70b9\u51fb\u529f\u80fd\"\"\"\n    clicker = AutoClicker()\n\n    print(\"\\n=== \u81ea\u5b9a\u4e49\u70b9\u51fb\u6a21\u5f0f ===\")\n    print(\"\u8f93\u5165\u5750\u6807\u8fdb\u884c\u70b9\u51fb\uff0c\u8f93\u5165 'q' \u9000\u51fa\")\n\n    while True:\n        try:\n            user_input = input(\"\\n\u8bf7\u8f93\u5165\u5750\u6807 (\u683c\u5f0f: x,y \u6216 x,y,button,clicks): \").strip()\n\n            if user_input.lower() == 'q':\n                print(\"\u9000\u51fa\u7a0b\u5e8f\")\n                break\n\n            # \u89e3\u6790\u8f93\u5165\n            parts = user_input.split(',')\n            if len(parts) == 2:\n                x, y = map(int, parts)\n                clicker.click_at_position(x, y)\n            elif len(parts) == 4:\n                x, y, button, clicks = int(parts&#91;0]), int(parts&#91;1]), parts&#91;2], int(parts&#91;3])\n                clicker.click_at_position(x, y, button, clicks)\n            else:\n                print(\"\u683c\u5f0f\u9519\u8bef\uff01\u8bf7\u4f7f\u7528 x,y \u6216 x,y,button,clicks \u683c\u5f0f\")\n\n        except ValueError:\n            print(\"\u8f93\u5165\u683c\u5f0f\u9519\u8bef\uff01\u8bf7\u8f93\u5165\u6709\u6548\u7684\u6570\u5b57\")\n        except KeyboardInterrupt:\n            print(\"\\n\u7a0b\u5e8f\u88ab\u4e2d\u65ad\")\n            break\n\nif __name__ == \"__main__\":\n    try:\n        # \u8fd0\u884c\u6f14\u793a\n        demo_usage()\n\n        # \u8be2\u95ee\u662f\u5426\u7ee7\u7eed\u81ea\u5b9a\u4e49\u70b9\u51fb\n        choice = input(\"\\n\u662f\u5426\u7ee7\u7eed\u81ea\u5b9a\u4e49\u70b9\u51fb\uff1f(y\/n): \").strip().lower()\n        if choice == 'y':\n            custom_clicking()\n\n    except Exception as e:\n        print(f\"\u7a0b\u5e8f\u8fd0\u884c\u51fa\u9519: {e}\")\n        print(\"\u8bf7\u786e\u4fdd\u5df2\u5b89\u88c5 pydirectinput: pip install pydirectinput\")<\/code><\/pre>\n","protected":false},"excerpt":{"rendered":"","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1],"tags":[],"class_list":["post-18","post","type-post","status-publish","format-standard","hentry","category-uncategorized"],"_links":{"self":[{"href":"https:\/\/www.yangjifang.cn\/index.php\/wp-json\/wp\/v2\/posts\/18","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.yangjifang.cn\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.yangjifang.cn\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.yangjifang.cn\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.yangjifang.cn\/index.php\/wp-json\/wp\/v2\/comments?post=18"}],"version-history":[{"count":1,"href":"https:\/\/www.yangjifang.cn\/index.php\/wp-json\/wp\/v2\/posts\/18\/revisions"}],"predecessor-version":[{"id":19,"href":"https:\/\/www.yangjifang.cn\/index.php\/wp-json\/wp\/v2\/posts\/18\/revisions\/19"}],"wp:attachment":[{"href":"https:\/\/www.yangjifang.cn\/index.php\/wp-json\/wp\/v2\/media?parent=18"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.yangjifang.cn\/index.php\/wp-json\/wp\/v2\/categories?post=18"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.yangjifang.cn\/index.php\/wp-json\/wp\/v2\/tags?post=18"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}