zoukankan      html  css  js  c++  java
  • Python模拟键盘输入和鼠标操作

    Python模拟键盘输入和鼠标操作

    一、Python键盘输入模拟

    import win32api

    import win32con

    win32api.keybd_event(17,0,0,0)  #ctrl键位码是17

    win32api.keybd_event(86,0,0,0)  #v键位码是86

    win32api.keybd_event(86,0,win32con.KEYEVENTF_KEYUP,0) #释放按键

    win32api.keybd_event(17,0,win32con.KEYEVENTF_KEYUP,0)

    附个键位码表:

    字母和数字键     数字小键盘的键       功能键         其它键 

          键   键码     键   键码       键   键码     键      键码 

          A   65       0   96        F1   112     Backspace    8 

          B   66       1   97        F2   113     Tab       9 

          C   67       2   98        F3   114     Clear      12 

          D   68       3   99        F4   115     Enter      13 

          E   69       4   100       F5   116     Shift      16 

          F   70       5   101       F6   117     Control     17 

          G   71       6   102       F7   118      Alt       18 

          H   72       7   103       F8   119     Caps Lock    20 

          I   73       8   104       F9   120     Esc       27 

          J   74       9   105       F10  121     Spacebar    32 

          K   75       *   106       F11  122     Page Up     33 

          L   76       +   107       F12  123     Page Down    34 

          M   77       Enter 108       --   --      End       35 

          N   78       -   109       --   --       Home      36 

          O   79       .   110       --   --      Left Arrow   37 

          P   80       /   111       --   --      Up Arrow    38 

          Q   81       --   --       --   --      Right Arrow   39 

          R   82       --   --       --   --      Down Arrow    40 

          S   83       --   --       --   --      Insert      45 

          T   84       --   --       --   --      Delete      46 

          U   85       --   --       --   --      Help       47 

          V   86       --   --       --   --      Num Lock     144 

           其他未列出的字母和数字键盘为:ord(c)

    二、 使用windll.user32实现鼠标模拟

    from ctypes import *

    windll.user32.SetCursorPos(100, 100)

    三. 使用AutoItX实现鼠标模拟

    #将AutoItX3.dll 文件复制到 window目录然后注册一下

    regsvr32.exe AutoItX3.dll

    from win32com.client import Dispatch

    def enter_game():

         AutoItX = Dispatch( "AutoItX3.Control" )

        # Block All Input

        AutoItX.BlockInput( 1 )

        AutoItX.Sleep( 20000 )

        if AutoItX.WinActivate( GAME_WINDOW_TITLE, '' ):

            pass

        else:

            if AutoItX.WinWaitActive( GAME_WINDOW_TITLE, '', 8 ):

                pass

            else:

                # Unblock input

                AutoItX.BlockInput( 0 )

                return False

        AutoItX.WinSetTitle( GAME_WINDOW_TITLE, '', _pre_title )

        AutoItX.WinSetState( _pre_title, '', AutoItX.SW_MAXIMIZE )

        AutoItX.Sleep( 5000 )

        AutoItX.MouseMove( 462, 396, 10 )

        AutoItX.MouseClick( "left" )

        AutoItX.Sleep( 1000 )

        AutoItX.Send( GAME_ACCT_NAME )

        AutoItX.Sleep( 1000 )

        AutoItX.MouseMove ( 462, 472, 10 )

        AutoItX.MouseClick( "left" )

        AutoItX.Sleep( 1000 )

        AutoItX.Send( GAME_ACCT_PASS )

        AutoItX.Send( "{ENTER}" )

        AutoItX.Sleep( 10000 )

        # Unblock input

        AutoItX.BlockInput( 0 )

        return True

  • 相关阅读:
    ArrayList和LinkedList的区别
    线程的基本概念、线程的基本状态以及状态之间的关 系
    当一个线程进入一个对象的一个synchronized方法后, 其它线程是否可进入此对象的其它方法?
    Caused by: org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type 'com.thinkplatform.dao.UserLogDao' available: expected at least 1 bean which qualifies as autowi
    IDEA设置热部署
    spring的核心组件及作用(一)
    解决Linux启动redis时出现提示权限不够问题
    Struts自动装配和四种放入Session作用域的方式
    Struts第一个案例搭建
    当List<String> list =new ArrayList<String>(20); 他会扩容多少次
  • 原文地址:https://www.cnblogs.com/nemolmt/p/5807702.html
Copyright © 2011-2022 走看看