Python2.7: 使用Pyhook模块监听鼠标键盘事件-获取坐标。因该模块对Python3 有兼容性问题,故采用python2.7解释器。
原程序可监听所有事件,现注释掉部分功能,只输出鼠标左键触发的坐标,用于获取坐标,方便自动化取点。
# -*- coding: utf-8 -*- # # import pythoncom import pyHook def onMouseEvent(event): # 监听鼠标事件 # print ("MessageName:", event.MessageName) # print ("Message:", event.Message) # print ("Time:", event.Time) # print ("Window:", event.Window) # print ("WindowName:", event.WindowName) # print ("Position:", event.Position) # print ("Wheel:", event.Wheel) # print ("Injected:", event.Injected) # print ("---") if event.MessageName == 'mouse left down': print 'mouse left down' print ("Position:", event.Position) # 返回 True 以便将事件传给其它处理程序 # 注意,这儿如果返回 False ,则鼠标事件将被全部拦截 # 也就是说你的鼠标看起来会僵在那儿,似乎失去响应了 return True def onKeyboardEvent(event): # 监听键盘事件 # print ("MessageName:", event.MessageName) # print ("Message:", event.Message) # print ("Time:", event.Time) # print ("Window:", event.Window) # print ("WindowName:", event.WindowName) # print ("Ascii:", event.Ascii, chr(event.Ascii)) # print ("Key:", event.Key) # print ("KeyID:", event.KeyID) # print ("ScanCode:", event.ScanCode) # print ("Extended:", event.Extended) # print ("Injected:", event.Injected) # print ("Alt", event.Alt) # print ("Transition", event.Transition) # print ("---") # 同鼠标事件监听函数的返回值 return True def main(): # 创建一个“钩子”管理对象 hm = pyHook.HookManager() # 监听所有键盘事件 #hm.KeyDown = onKeyboardEvent # 设置键盘“钩子” #hm.HookKeyboard() # 监听所有鼠标事件 hm.MouseAll = onMouseEvent # 设置鼠标“钩子” hm.HookMouse() # 进入循环,如不手动关闭,程序将一直处于监听状态 pythoncom.PumpMessages() if __name__ == "__main__": main()
补充知识:python 工具mouse_find 鼠标定位
我就废话不多说了,还是直接看代码吧!
import os,time import pyautogui as pag try: while True: print ("Press Ctrl-C to end") x,y = pag.position() #返回鼠标的坐标 posStr="Position:"+str(x).rjust(4)+','+str(y).rjust(4) print (posStr)#打印坐标 time.sleep(0.2) os.system('cls')#清楚屏幕 except KeyboardInterrupt: print( 'end....')
以上这篇Python2.7:使用Pyhook模块监听鼠标键盘事件-获取坐标实例就是小编分享给大家的全部内容了,希望能给大家一个参考,也希望大家多多支持。
华山资源网 Design By www.eoogi.com
广告合作:本站广告合作请联系QQ:858582 申请时备注:广告合作(否则不回)
免责声明:本站资源来自互联网收集,仅供用于学习和交流,请遵循相关法律法规,本站一切资源不代表本站立场,如有侵权、后门、不妥请联系本站删除!
免责声明:本站资源来自互联网收集,仅供用于学习和交流,请遵循相关法律法规,本站一切资源不代表本站立场,如有侵权、后门、不妥请联系本站删除!
华山资源网 Design By www.eoogi.com
暂无评论...
更新日志
2024年09月27日
2024年09月27日
- 张学友.1999-有个人【宝丽金】【WAV+CUE】
- 纯音入心系列纯音乐《古筝佛曲》1CD[MP3][411.1MB]
- 纯音入心系列纯音乐《古筝世界名曲》1CD[MP3][38.9MB]
- 纯音入心系列纯音乐《古筝新奏-菩提妙音》1CD[MP3][112.3MB]
- 陈思安.1993-怀念福建金曲5CD【歌丽美】【WAV+CUE】
- 张学友.1999-有个人【宝丽金】【WAV+CUE】
- 许美静.1999-快乐无罪【上华】【WAV+CUE】
- 群星.2024-Jambo.Medley(BEYOND40周年·致敬2)【Kinns】【FLAC分轨】
- 林翠萍.1991-舞曲世界2辑【星河】【WAV+CUE】
- 关智斌.2007-In.Progress【英皇娱乐】【FLAC分轨】
- 群星.2000-电影主题曲原声带·琼瑶的世界【歌林】【WAV+CUE】
- 范世錡.2024-錡妙夜(EP)【天娱传媒】【FLAC分轨】
- 谭咏麟.2000-魅力千禧演唱会2CD(2024环球红馆40复刻系列)【环球】【WAV+CUE】
- 纯音入心系列纯音乐《器乐古筝》1CD[MP3][239MB]
- 纯音入心系列纯音乐《华夏民乐之古筝篇》1CD[MP3][192MB]