【问题标题】:Which is the easiest way to simulate keyboard and mouse on Python?在 Python 上模拟键盘和鼠标的最简单方法是什么?
【发布时间】:2011-02-17 00:33:07
【问题描述】:

我需要做一些宏,我想知道最推荐的方法是什么。

所以,我需要写一些东西并用它点击一些地方,我需要模拟 TAB 键。

【问题讨论】:

  • 您可能希望更具体地了解您正在测试的内容。网页?桌面应用程序?
  • 网页、桌面应用程序,一切 =)
  • Here 是一种在 OS X 中模拟键盘事件的方法。

标签: python windows keyboard mouse


【解决方案1】:

我在 Python 中进行自动化测试。我倾向于使用以下内容:

http://www.tizmoi.net/watsup/intro.html
编辑:链接已失效,存档版本:https://web.archive.org/web/20100224025508/http://www.tizmoi.net/watsup/intro.html

http://www.mayukhbose.com/python/IEC/index.php

我并不总是(几乎从不)模拟按键和鼠标移动。我通常使用 COM 来设置 windows 对象的值并调用它们的 .click() 方法。

你可以用这个发送按键信号:

import win32com.client

shell = win32com.client.Dispatch("WScript.Shell")
shell.SendKeys("^a") # CTRL+A may "select all" depending on which window's focused
shell.SendKeys("{DELETE}") # Delete selected text?  Depends on context. :P
shell.SendKeys("{TAB}") #Press tab... to change focus or whatever

这一切都在 Windows 中。如果您在另一个环境中,我不知道。

【讨论】:

  • 温馨提示:要使用win32com,您首先必须安装Python for Windows extensions
  • 要激活您要发送密钥的程序,请使用shell.AppActivate("Notepad")(替换记事本)
  • 这非常有效 - 感谢@dagur 关于窗口焦点的提示!我还需要鼠标事件模拟 - this anser 对我来说是最好的,并且与这个键盘解决方案配合得很好。
【解决方案2】:

也许您正在寻找Sendkeys

SendKeys 是一个 Python 模块,用于 可以发送一个或多个的 Windows 击键或击键组合 到活动窗口。

好像只有windows

你还有pywinauto(复制自我的SO answer

pywinauto 是一套开源的 (LGPL) 模块,用于将 Python 用作 Windows NT 的 GUI 自动化“驱动程序” 基于操作系统 (NT/W2K/XP)。

网页上的例子

> from pywinauto import application
> app = application.Application.start("notepad.exe")
> app.notepad.TypeKeys("%FX")
> app.Notepad.MenuSelect("File->SaveAs")
> app.SaveAs.ComboBox5.Select("UTF-8")
> app.SaveAs.edit1.SetText("Example-utf8.txt")
> app.SaveAs.Save.Click()

【讨论】:

  • SendKeys 似乎仅适用于 Python 2.x。而且,它的网站现在已经死了
【解决方案3】:

pyautogui 是一个很棒的包,可以发送键和自动化几个键盘/鼠标相关的任务。查看Controlling the Keyboard and Mouse with GUI AutomationPyAutoGUI’s documentation

【讨论】:

    【解决方案4】:

    您可以将PyAutoGUI library 用于适用于 Windows、macOS 和 Linux 的 Python。

    鼠标

    下面是一个简单的代码,将鼠标移动到屏幕中间:

    import pyautogui
    screenWidth, screenHeight = pyautogui.size()
    pyautogui.moveTo(screenWidth / 2, screenHeight / 2)
    

    文档页面:Mouse Control Functions.

    相关问题:Controlling mouse with Python

    键盘

    例子:

    pyautogui.typewrite('Hello world!')                 # prints out "Hello world!" instantly
    pyautogui.typewrite('Hello world!', interval=0.25)  # prints out "Hello world!" with a quarter second delay after each character
    

    文档页面:Keyboard Control Functions.


    更多阅读:Controlling the Keyboard and Mouse with GUI Automation(电子书第18章)。

    相关问题:

    【讨论】:

      【解决方案5】:

      另外两个选项是:

      警告 - 如果您想在游戏中使用键盘控制,那么 pynput 并不总是有效 - 例如。它适用于 Valheim,但不适用于 Witcher 3 - PyDirectInput 将在其中工作。我还测试了 PyDirectInput,它适用于《半条命 2》(作为对旧游戏的测试)。

      提示 - 您可能需要减少(不要在游戏中删除)字符输入之间的延迟 - 使用 pydirectinput.PAUSE = 0.05

      例如,这里有一个允许虚拟键盘输入的功能 - 目前仅在 Windows 上测试:

      from pynput import keyboard
      try:
          import pydirectinput
          pydirectinput.PAUSE = 0.05
      except ImportError as err:
          pydirectinput = False
          print("pydirectinput not found:")
      
      def write_char(ch):
          upper = ch.isupper()
          if pydirectinput and pydirectinput.KEYBOARD_MAPPING.get(ch.lower(), False):
              if upper:
                  pydirectinput.keyDown('shift')
                  print('^')
              pydirectinput.write(ch.lower(), interval=0.0)
              print(ch)
              if upper:
                  pydirectinput.keyUp('shift')
          else:
              keyboard.Controller().type(ch)
      

      这允许发送一个字符串,通过 pydirectinput 处理大写字母字符。当字符不简单地映射时,该函数回退到使用 pynput。请注意,PyAutoGUI 也无法处理一些移位的字符 - 例如 £ 符号等。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2015-03-01
        • 2022-02-04
        相关资源
        最近更新 更多