windows hook + pyhook3 + python win32api hook + C 键盘hook
安装pyhook3见:https://www.cnblogs.com/lqerio/p/12096710.html
使用见:https://www.cnblogs.com/lqerio/p/12106771.html
下面是自己学的时候查到的东西的一些整理,
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
下面开始是整理
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
windows hook原理:
windows hook 原理与实现 https://blog.csdn.net/m0_37552052/article/details/81453591
hook 基本原理 https://blog.csdn.net/qq_36381855/article/details/79962673
《windows核心编程系列》十八谈谈windows钩子 https://blog.csdn.net/fanhenghui/article/details/54138080
windows hook api:
callnexthookex() https://baike.baidu.com/item/CallNextHookEx/3777953?fr=aladdin
C语言windows 键盘Hook:
https://blog.csdn.net/yan_star/article/details/88528631
https://blog.csdn.net/johnny_83/article/details/1701822
https://www.iteye.com/blog/huiytt-1829744
python 使用win32api windows hook :
https://www.cnblogs.com/megachen/p/9879224.html
使用到的库:
- ctypes(通过ctypes来调用Win32API, 主要就是调用钩子函数)
使用的Win32API
- SetWindowsHookEx(), 将用户定义的钩子函数添加到钩子链中, 也就是我们的注册钩子函数
- UnhookWindowsHookEx(), 卸载钩子函数
- CallNextHookEx()在我们的钩子函数中必须调用, 这样才能让程序的传递消息
在没有钩子函数的情况下windows程序运行机制
- 键盘输入 --> 系统消息队列 --> 对应应用程序的消息队列 --> 将消息发送到对应的窗口中
在有了钩子函数的情况下windows程序运行机制
- 键盘输入 --> 系统消息队列 --> 对应应用程序消息队列 --> 将消息发送到钩子链中 --> 消息一一调用完毕所有的钩子函数(需要调用CallNextHookEx函数才能将消息传递下去) --> 将消息发送到对应的窗口中
示例程序
- 注意:
- 在程序中, 我们通过CFUNCTYPE返回一个类对象, 通过该类对象可以实例化出我们需要的c类型的函数, 但是如果不将他放在全局的话则会失去效果, 因为在C语言中函数是全局的
# -*- coding: utf-8 -*-
import os
import sys
from ctypes import *
from ctypes.wintypes import * """
define constants
"""
WH_KEYBOARD = 13
WM_KEYDOWN = 0x0100
CTRL_CODE = 162 class JHKeyLogger(object): def __init__(self, user32, kernel32):
"""
Description:
Init the keylogger object, the property 'hook_' is the handle to control our hook function Args:
@(dll)user32: just put windll.user32 here
@(dll)kernel32: just put windll.kernel32 here Returns:
None
"""
self.user32_ = user32
self.kernel32_ = kernel32
self.hook_ = None def install_hookproc(self, hookproc):
"""
Description:
install hookproc function into message chain Args:
@(c type function)hookproc: hookproc is the hook function to call Returns:
@(bool):
if SetWindowHookExA() function works successfully, return True
else return False
"""
self.hook_ = self.user32_.SetWindowsHookExA(
WH_KEYBOARD,
hookproc,
self.kernel32_.GetModuleHandleW(None),
0)
if not self.hook_:
return False
return True def uninstall_hookproc(self):
"""
Description:
uninstall the hookproc function which means pick the hookproc pointer off the message chain
Args:
None
Returns:
None
"""
if not self.hook_:
return
self.user32_.UnhookWindowsHookEx(self.hook_)
self.hook_ = None def start(self):
"""
Description:
start logging, just get the message, the current thread will blocked by the GetMessageA() function Args:
None
Returns:
None
"""
msg = MSG()
self.user32_.GetMessageA(msg, 0, 0, 0) def stop(self):
self.uninstall_hookproc() def hookproc(nCode, wParam, lParam):
"""
Description:
An user-defined hook function Attention:
here we use the global variable named 'g_keylogger'
"""
if wParam != WM_KEYDOWN:
return g_keylogger.user32_.CallNextHookEx(g_keylogger.hook_, nCode, wParam, lParam) pressed_key = chr(lParam[0])
print pressed_key,
# hit ctrl key to stop logging
if CTRL_CODE == lParam[0]:
g_keylogger.stop()
sys.exit(-1)
return g_keylogger.user32_.CallNextHookEx(g_keylogger.hook_, nCode, wParam, lParam) # Attention: pointer must be defined as a global variable
cfunctype = CFUNCTYPE(c_int, c_int, c_int, POINTER(c_void_p))
pointer = cfunctype(hookproc) g_keylogger = JHKeyLogger(windll.user32, windll.kernel32) def main():
if g_keylogger.install_hookproc(pointer):
print 'install keylogger successfully!'
g_keylogger.start()
print 'hit ctrl to stop' if __name__ == '__main__':
main()
pyHook3:
pywin32简介:
https://www.cnblogs.com/achillis/p/10462585.html
pyHook 源码:
https://sourceforge.net/p/pyhook/code/ci/master/tree/README.txt
知乎pyhook:
https://www.zhihu.com/search?type=content&q=pyhook
使用pyHook的例子:
https://oldj.net/blog/2010/07/14/python-hook/
添加开始和结束按键 https://blog.csdn.net/cd_xuyue/article/details/50688748
添加开始和结束快捷键 https://blog.csdn.net/dyx1024/article/details/7338646
https://blog.csdn.net/q871063970/article/details/86648386
win32api.PostQuitMessage() https://www.cnblogs.com/xiaowuyi/archive/2012/03/15/2398665.html
Python3按照 pyhook,pycom:
https://blog.csdn.net/xiaoliu5396/article/details/46457585
https://blog.csdn.net/dongfuguo/article/details/70226384#reply
这里建议安装pyhook3,之前的博客里有写过安装anaconda pyhook3
https://www.cnblogs.com/lqerio/p/12096710.html
windows hook + pyhook3 + python win32api hook + C 键盘hook的更多相关文章
- Python win32api.keybd_event模拟键盘输入
win32api.keybd_event 该函数原型:keybd_event(bVk, bScan, dwFlags, dwExtraInfo) 第一个参数:虚拟键码(键盘键码对照表见附录): 第二个 ...
- 在C#中使用全局鼠标、键盘Hook
今天,有个同事问我,怎样在C#中使用全局钩子?以前写的全局钩子都是用unmanaged C或C++写个DLL来实现,可大家都知道,C#是基于.Net Framework的,是managed,怎么实现全 ...
- 如何在C#中使用全局鼠标、键盘Hook
今天,有个同事问我,怎样在C#中使用全局钩子?以前写的全局钩子都是用unmanaged C或C++写个DLL来实现,可大家都知道,C#是基于.Net Framework的,是managed,怎么实现全 ...
- 键盘Hook【Delphi版】
原文:https://www.cnblogs.com/edisonfeng/archive/2012/05/18/2507858.html 一.钩子的基本概念 a) Hook作用:监视windows消 ...
- windows 32位以及64位的inline hook
Tips : 这篇文章的主题是x86及x64 windows系统下的inline hook实现部分. 32位inline hook 对于系统API的hook,windows 系统为了达成hotpatc ...
- [python] PyMouse、PyKeyboard用python操作鼠标和键盘
1.PyUserInput 简介 PyUserInput是一个使用python的跨平台的操作鼠标和键盘的模块,非常方便使用.支持的平台及依赖如下: Linux - Xlib Mac - Quart ...
- LFD,非官方的Windows二进制文件的Python扩展包
LFD,非官方的Windows二进制文件的Python扩展包 LFD,非官方版本.32和64位.Windows.二进制文件.科学开源.Python扩展包 克里斯托夫·戈尔克(by Christoph ...
- Python模拟鼠标和键盘操作实现重复性操作
前言 由于工作需要,要利用某软件去采集数据,做重复的动作大概500多次.所以想写一个程序代替人,去点击和输入. 一开始的思路有两个:1.用Python或者windows对此软件直接操作.2.利用Pyt ...
- 1、[python] PyMouse、PyKeyboard用python操作鼠标和键盘
[python] PyMouse.PyKeyboard用python操作鼠标和键盘 1.PyUserInput 简介 PyUserInput是一个使用python的跨平台的操作鼠标和键盘的模块,非常方 ...
随机推荐
- logicaldisk本地磁盘管理
在网上搜了很多,但是基本都是一样的,差不多都是互相转载摘抄,就那么几个寥寥无几的例子,所以我冒了很大的风险,自己经过多次的测试,对这个命令有了一些新的认识!拿出来分享一下! LOGICALDISK ...
- RESTful风格、异常处理、Spring框架
1.RESTful风格 什么是RESTful风格? REST是REpressentational State Transfer的缩写,中文翻译为表述性状态转移,REST是一种体系结构,而HTTP是一种 ...
- 前端知识(二)08-Vue.js的路由-谷粒学院
目录 一.锚点的概念 二.路由的作用 三.路由实例 1.复制js资源 2.创建 路由.html 3.引入js 4.编写html 5.编写js 一.锚点的概念 案例:百度百科 特点:单页Web应用,预先 ...
- 用git合并分支时,如何保持某些文件不被合并
用git合并分支时,如何保持某些文件不被合并_fkaking的专栏-CSDN博客_git 合并分支 https://blog.csdn.net/fkaking/article/details/4495 ...
- 研发过程及工具支撑 DevOps 工具链集成
https://mp.weixin.qq.com/s/NYm63nkCymIV3DbL4O01dg 腾讯重新定义敏捷 |Q推荐 小智 InfoQ 2020-09-03 敏捷开发奠基人 Robert C ...
- ensure that both new and old access_token values are available within five minutes, so that third-party services are smoothly transitioned.
WeChat public doc https://developers.weixin.qq.com/doc/offiaccount/en/Basic_Information/Get_access_t ...
- Sapphire: Copying GC Without Stopping the World
https://people.cs.umass.edu/~moss/papers/jgrande-2001-sapphire.pdf Many concurrent garbage collectio ...
- etcd 性能优化实践
https://mp.weixin.qq.com/s/lD2b-DZyvRJ3qWqmlvHpxg 从零开始入门 K8s | etcd 性能优化实践 原创 陈星宇 阿里巴巴云原生 2019-12-16 ...
- 【SVN】windows 下的SVN常见问题及其解决方法
1.能提交和更新,但SVN查看log时提示:找不到路径 'svn/XXXX' 双击以清除错误信息 勾选这个选项就好了.因为该路径是通过重命名或者拷贝过来的,倘若不选中,SVN便会尝试同时从当前文件的拷 ...
- Python学习【第6篇】:集合的定义和基本方法
1.概念 (1)不同元素组成 例: s = {1,2,3,4,4,4,4,4,4}print(s)运行结果:{1, 2, 3, 4}因为是不同元素组成,因此去重了 (2)无序 例: s = {&quo ...