当前位置:   article > 正文

python模拟鼠标和键盘_python代替鼠标

python代替鼠标

使用pymouse,win32api,win32con库,包括windows库和pyHook库,可能会有一些问题,但是都能搜到解决方案,最后附我自用的代码及注释

  1. import time
  2. import pymouse
  3. import win32api
  4. import win32con
  5. def get_loc(m):
  6. for i in range(99):
  7. a = m.position() #获取当前坐标的位置
  8. time.sleep(1)
  9. print(a)
  10. def ctrlc():
  11. win32api.keybd_event(17,0,0,0)#ctrl键位码是17
  12. win32api.keybd_event(67,0,0,0) #c键位码是67
  13. win32api.keybd_event(67,0,win32con.KEYEVENTF_KEYUP,0) #释放按键
  14. win32api.keybd_event(17,0,win32con.KEYEVENTF_KEYUP,0)
  15. def ctrlv():
  16. win32api.keybd_event(17,0,0,0) #ctrl键位码是17
  17. win32api.keybd_event(86,0,0,0) #v键位码是86
  18. win32api.keybd_event(86,0,win32con.KEYEVENTF_KEYUP,0) #释放按键
  19. win32api.keybd_event(17,0,win32con.KEYEVENTF_KEYUP,0)
  20. def ctrla():
  21. win32api.keybd_event(17,0,0,0)#ctrl键位码是17
  22. win32api.keybd_event(65,0,0,0) #a键位码是67
  23. win32api.keybd_event(65,0,win32con.KEYEVENTF_KEYUP,0) #释放按键
  24. win32api.keybd_event(17,0,win32con.KEYEVENTF_KEYUP,0)
  25. def right():
  26. win32api.keybd_event(39,0,0,0)#Right Arrow 键位码是39
  27. win32api.keybd_event(39,0,win32con.KEYEVENTF_KEYUP,0) #释放按键
  28. def delete():
  29. win32api.keybd_event(8,0,0,0)#Backspace键位码是39
  30. win32api.keybd_event(8,0,win32con.KEYEVENTF_KEYUP,0) #释放按键
  31. def input_num(i):
  32. win32api.keybd_event(i+96,0,0,0)#单个数字
  33. win32api.keybd_event(i+96,0,win32con.KEYEVENTF_KEYUP,0)
  34. def switchtime(m,x,y):
  35. time.sleep(0.5)
  36. m.click(222,898)#转换时间的位置
  37. time.sleep(0.5)
  38. m.click(222,898)
  39. time.sleep(0.5)
  40. delete()
  41. delete()
  42. time.sleep(0.5)
  43. input_num(x)
  44. input_num(y)
  45. time.sleep(1)
  46. m.click(421,1064)#apply
  47. time.sleep(0.5)
  48. def data1(m):
  49. m.click(1334,342)
  50. time.sleep(0.5)
  51. ctrla()
  52. time.sleep(0.5)
  53. ctrlc()
  54. time.sleep(0.5)
  55. m.click(275,389)
  56. time.sleep(0.5)
  57. ctrlv()
  58. time.sleep(0.5)
  59. right()
  60. time.sleep(0.5)
  61. right()
  62. time.sleep(0.5)
  63. right()
  64. time.sleep(0.5)
  65. def data2(m):
  66. m.click(1822,357)
  67. time.sleep(0.5)
  68. ctrla()
  69. time.sleep(0.5)
  70. ctrlc()
  71. time.sleep(0.5)
  72. m.click(743,383)
  73. time.sleep(0.5)
  74. ctrlv()
  75. time.sleep(0.5)
  76. right()
  77. time.sleep(0.5)
  78. right()
  79. time.sleep(0.5)
  80. right()
  81. time.sleep(0.5)
  82. def alterload(m,x):
  83. time.sleep(0.5)
  84. m.click(679, 913)#转换时间的位置
  85. time.sleep(0.5)
  86. m.click(679, 913)
  87. time.sleep(0.5)
  88. delete()
  89. time.sleep(0.5)
  90. input_num(x)
  91. time.sleep(1)
  92. m.click(973, 1067)#apply
  93. time.sleep(0.5)
  94. def run(m):
  95. m.click(1095,618)
  96. time.sleep(8)
  97. def main(m):
  98. time.sleep(10)#十秒准备
  99. for i in range(1,9):
  100. alterload(m,i)
  101. for j in range(1,4):
  102. for k in range(0,9):
  103. switchtime(m,j,k)
  104. run(m)
  105. data1(m)
  106. data2(m)
  107. #按下组合键ctrl+V
  108. # win32api.keybd_event(97,0,0,0) #1键位码是97
  109. # win32api.keybd_event(97,0,win32con.KEYEVENTF_KEYUP,0) #释放按键
  110. # win32api.keybd_event(98,0,0,0) #2键位码是98
  111. # win32api.keybd_event(98,0,win32con.KEYEVENTF_KEYUP,0)
  112. m = pymouse.PyMouse() # 鼠标的实例
  113. main(m)
  114. #get_loc(m)

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/weixin_40725706/article/detail/506467
推荐阅读
相关标签
  

闽ICP备14008679号