原来就见过@property,但是一直不知道这个是干嘛的
今天看pyHook的的使用代码的时候正好解决了这个问题
问题源自下面代码的第52行
-
# -*- coding: utf-8 -*-
-
#
-
# by oldj
-
# <a href="" rel="nofollow">http://oldj.net/</a>
-
#
-
-
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()
hm.MouseAll = onMouseEvent
这里我看得很奇怪,类里面的方法可以这样直接赋值(绑定)过去?
直接可以这样的话,平时就不用继承了吧....所以搜索了下正好有个相关的网页说了
https://blog.lzhaohao.info/archive/python-dynamic-instance-bound-method-access-to-private-method/
“要动态为实例绑定方法,可以使用new模块”——这个new好像更加复杂,所以我们先不关注这个new,只关注这个结论.....
这里没有用new,所以直接把这里理解为绑定方法是不对的
所以还是要看pyHook的源代码
找到KeyDown,发现
KeyDown = property(fset=SubscribeKeyDown)
这个
property是干啥的?
详细说明
http://www.ibm.com/developerworks/cn/opensource/os-pythondescriptors/
这个自己去看
简单说明
http://www.cnblogs.com/lovemo1314/archive/2011/05/03/2035600.html
--------------------------------------------------------------------------------转载的分割线---------------------------------------------------------------------------------------------------------------------------
1.现在介绍第一种使用property属性的方法:
在该类中定义三个函数,分别用作赋值、取值和删除变量(此处表达也许不很清晰,请看示例)
-
def getx(self):
-
return self.__x
-
def setx(self,value):
-
self.__x=value
-
def delx(self):
-
del self.__x
-
x=property(getx,setx,delx,'')
property函数原型为property(fget=None,fset=None,fdel=None,doc=None),所以根据自己需要定义相应的函数即可。
现在这个类中的x属性便已经定义好了,我们可以先定义一个C的实例c=C(),然后赋值c.x=100,取值y=c.x,删除:del c.x。是不是很简单呢?请看第二种方法(就是@
property,作用一样写法简洁了点我就不贴过来了)
--------------------------------------------------------------------------------转载的分割线---------------------------------------------------------------------------------------------------------------------------
回头看pyHook里的
SubscribeKeyDown
-
def SubscribeKeyDown(self, func):
-
'''
-
Registers the given function as the callback for this keyboard event type.
-
Use the KeyDown property as a shortcut.
-
-
@param func: Callback function
-
@type func: callable
-
'''
-
if func is None:
-
self.disconnect(self.keyboard_funcs, HookConstants.WM_KEYDOWN)
-
self.disconnect(self.keyboard_funcs, HookConstants.WM_SYSKEYDOWN)
-
else:
-
self.connect(self.keyboard_funcs, HookConstants.WM_KEYDOWN, func)
-
self.connect(self.keyboard_funcs, HookConstants.WM_SYSKEYDOWN, func)
通过上述
property的用法我们可以知道,由于定义了
KeyDown = property(fset=SubscribeKeyDown),所以外部调用hm.KeyDown = onKeyboardEvent
相当于hm.
SubscribeKeyDown(onKeyboardEvent)
作用有点类似于
__setitem__和__getitem__
我们顺便看看pyHook的其他地方源代码,发现
-
switch(idHook) {
-
case WH_MOUSE_LL:
-
if(callback_funcs[idHook] != NULL)
-
break;
-
-
callback_funcs[idHook] = pyfunc;
-
Py_INCREF(callback_funcs[idHook]);
-
-
Py_BEGIN_ALLOW_THREADS
-
hHooks[idHook] = SetWindowsHookEx(WH_MOUSE_LL, cLLMouseCallback, (HINSTANCE) hMod, 0);
-
Py_END_ALLOW_THREADS
-
break;
-
-
case WH_KEYBOARD_LL:
-
if(callback_funcs[idHook] != NULL)
-
break;
-
-
callback_funcs[idHook] = pyfunc;
-
Py_INCREF(callback_funcs[idHook]);
-
-
Py_BEGIN_ALLOW_THREADS
-
hHooks[idHook] = SetWindowsHookEx(WH_KEYBOARD_LL, cLLKeyboardCallback, (HINSTANCE) hMod, 0);
-
Py_END_ALLOW_THREADS
-
break;
-
-
default:
-
return 0;
-
}
也就是说只能用WH_MOUSE_LL和WH_KEYBOARD_LL两个钩子(pyHook底层的c api也只调用这两钩子,所以继承类添加其他钩子是没用的),这两个钩子很搓的,所以结论 pyHook基本没用,老老实实去学ctypes。
阅读(2557) | 评论(0) | 转发(0) |