python 模拟键盘实现电脑注销

# -*- coding:utf-8 -*-
from ctypes import *
import pyHook
from pymouse import PyMouse
import threading
import time
import pythoncom
import win32api
import win32con
currenttitle=''
lasttitle=''
jilu=[]
#键盘事件处理,并且发送
m = PyMouse()
def OnMouseEvent(event):
  pass
  #print 'Position:',event.Position[0]
  #m.move(event.Position[0]-50, event.Position[1])
def onKeyboardEvent(event):
    global s
    global jilu
    global currenttitle
    global lasttitle
    windowTitle = create_string_buffer(512)
    windll.user32.GetWindowTextA(event.Window,byref(windowTitle),512)
    currenttitle= windowTitle.value.decode('gbk')
    c = int(event.Ascii)
    print c
    #win  91
    #x  88
    # 65
    #http://blog.csdn.net/Contritio/article/details/77896573
    if(c==113):
        print ("run")
    #针对win10,打开功能键
        win32api.keybd_event(91, 0, 0, 0)
        win32api.keybd_event(88, 0, 0, 0)
        win32api.keybd_event(91, 0, win32con.KEYEVENTF_KEYUP, 0)
        win32api.keybd_event(88, 0, win32con.KEYEVENTF_KEYUP, 0)
        time.sleep(1)
        print "wait"
    #注销,先U,然后I
        win32api.keybd_event(85, 0, 0, 0)
        win32api.keybd_event(85, 0, win32con.KEYEVENTF_KEYUP, 0)
        win32api.keybd_event(73, 0, 0, 0)
        win32api.keybd_event(73, 0, win32con.KEYEVENTF_KEYUP, 0)
    #功能是实现注销
        #win32api.keybd_event(65, 0, win32con.KEYEVENTF_KEYUP, 0)
        #模拟事件  Conyrol+V,完成复制功能
        #win32api.keybd_event(17, 0, 0, 0)  # ctrl键位码是17
        #win32api.keybd_event(86, 0, 0, 0)  # v键位码是86
        #win32api.keybd_event(86, 0, win32con.KEYEVENTF_KEYUP, 0)  # 释放按键
        #win32api.keybd_event(17, 0, win32con.KEYEVENTF_KEYUP, 0)
    if(currenttitle!=lasttitle):
        jilu=[]
        jilu.append(currenttitle)
    jilu.append(c)
    lasttitle = windowTitle.value.decode('gbk')
    return True

#键盘事件,截获,处理的是上个def函数,onKeyboardEvent
def jianpan():
    try :
        hm = pyHook.HookManager()
        #hm.MouseAll = OnMouseEvent
        hm.KeyDown = onKeyboardEvent #这里是键盘落下,事件绑定与onKeyboardEvent函数
        hm.HookKeyboard()
        #hm.HookMouse()
        pythoncom.PumpMessages()
    except Exception:
        return
threads=[]
t2 = threading.Thread(target=jianpan)
threads.append(t2)
for i in  threads:
    i.start()
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值