Python 2.7 松开kivy中的按钮控制与pyhook结合使用
我在kivy中有一个小的记录器应用程序,可以控制屏幕捕获活动。记录器应用程序应根据记录器的状态显示状态行和录制/暂停按钮。最初的顺序是:等待(即等待某些操作-按rec按钮)>设置应用程序(即最终用户应通过鼠标单击将其他应用程序窗口置于前台)>录制(通过鼠标单击捕获屏幕图像)>暂停或停止。为了触发捕获活动,我使用pyHook读取鼠标事件。但是,一旦调用pyHook.HookManager(),我就失去了对kivy recorder应用程序的访问,无法控制录制过程:按钮单击未被捕获,状态行和事件ID未更新。我错过了什么 附上Python代码、kv文件和为方便起见的图像文件。 谢谢你的帮助和提前的时间Python 2.7 松开kivy中的按钮控制与pyhook结合使用,python-2.7,kivy,desktop-application,screen-capture,pyhook,Python 2.7,Kivy,Desktop Application,Screen Capture,Pyhook,我在kivy中有一个小的记录器应用程序,可以控制屏幕捕获活动。记录器应用程序应根据记录器的状态显示状态行和录制/暂停按钮。最初的顺序是:等待(即等待某些操作-按rec按钮)>设置应用程序(即最终用户应通过鼠标单击将其他应用程序窗口置于前台)>录制(通过鼠标单击捕获屏幕图像)>暂停或停止。为了触发捕获活动,我使用pyHook读取鼠标事件。但是,一旦调用pyHook.HookManager(),我就失去了对kivy recorder应用程序的访问,无法控制录制过程:按钮单击未被捕获,状态行和事件ID
import os
import pyHook
import pythoncom
from kivy.app import App
from kivy.uix.boxlayout import BoxLayout
from kivy.core.window import Window
from kivy.properties import StringProperty, NumericProperty
from kivy.clock import Clock
hm = None
recStatus = None
eventListID = 0
class hookHome(BoxLayout):
curevent = NumericProperty()
recpbutton = StringProperty()
stopbutton = StringProperty()
status = StringProperty()
def init_recorder(self):
global recStatus
global eventListID
self.recpbutton = './Record.png'
self.stopbutton = './Stop.png'
self.curevent = eventListID
self.status = 'waiting'
recStatus = 'INIT'
def recording_pause_proc(self):
global recStatus
global hm
if recStatus == 'INIT':
self.recpbutton = './Pause-r.png'
recStatus = 'SETAPPL'
self.status = 'set applic.'
Clock.schedule_once(self.proc_recorder, 0)
elif recStatus == 'RECORD':
self.recpbutton = './Record.png'
recStatus = 'PAUSE'
self.status = 'pause'
hm.UnhookMouse()
hm.UnhookKeyboard()
elif recStatus == 'PAUSE':
self.recpbutton = './Pause-r.png'
recStatus = 'RECORD'
self.status = 'recording'
hm.HookMouse()
hm.HookKeyboard()
elif recStatus == 'SETAPPL':
self.recpbutton = './Pause-r.png'
recStatus = 'RECORD'
self.status = 'recording'
def stop_recorder(self):
hm.UnhookMouse()
hm.UnhookKeyboard()
os._exit(0)
def onMouseEvent(self, event):
global recStatus
if recStatus == 'SETAPPL':
recStatus = 'RECORD'
self.status = 'recording'
elif recStatus == 'RECORD':
print "Capture window..."
return True
def proc_recorder(self, *args):
global hm
hm = pyHook.HookManager()
hm.MouseAllButtonsDown = self.onMouseEvent
hm.HookMouse()
pythoncom.PumpMessages()
class hooktestApp(App):
def build(self):
Window.clearcolor = (1,1,1,1)
Window.size = (180, 55)
homeWin = hookHome()
homeWin.init_recorder()
return homeWin
if __name__ == '__main__':
hooktestApp().run()
和kv文件:
<hookHome>:
orientation: "vertical"
BoxLayout:
orientation: 'vertical'
BoxLayout:
orientation: 'horizontal'
height: "15dp"
size_hint_y: None
Label:
canvas.before:
Color:
rgb: 0.7,0.9,1
Rectangle:
pos: self.pos
size: self.size
size_hint: 0.6,1
text: "Recorder"
text_size: self.width -10, self.height
halign: 'left'
valign: 'middle'
font_size: 12
color: .3,.3,.3,1
Label:
canvas.before:
Color:
rgb: 0.7,0.9,1
Rectangle:
pos: self.pos
size: self.size
size_hint: 0.4,1
text: root.status
text_size: self.width-10, self.height
halign: 'right'
valign: 'middle'
font_size: 12
color: .3,.3,.3,1
BoxLayout:
height: "40dp"
size_hint_y: None
orientation: "horizontal"
BoxLayout:
orientation: "vertical"
size_hint: 0.33,1
Label:
canvas.before:
Color:
rgb: 0,.24,.42
Rectangle:
pos: self.pos
size: self.size
size_hint: 1,0.65
text: str(root.curevent)
size: self.texture_size
halign: 'center'
valign: 'middle'
font_size: 16
color: 1,1,1,1
Label:
canvas.before:
Color:
rgb: 0,.24,.42
Rectangle:
pos: self.pos
size: self.size
size_hint: 1,0.35
#text: root.curevent
text: 'Event'
size: self.texture_size
halign: 'center'
valign: 'middle'
font_size: 14
color: 1,1,1,1
Button:
size_hint: 0.33,1
background_normal: './dblButton.png'
background_down: './lblButton.png'
on_press: root.recording_pause_proc()
Image:
source: root.recpbutton
center_x: self.parent.center_x
center_y: self.parent.center_y
Button:
size_hint: 0.33,1
background_normal: './dblButton.png'
background_down: './lblButton.png'
on_press: root.stop_recorder()
Image:
source: root.stopbutton
center_x: self.parent.center_x
center_y: self.parent.center_y
:
方向:“垂直”
盒子布局:
方向:“垂直”
盒子布局:
方向:“水平”
高度:“15dp”
尺寸提示:无
标签:
在以下情况之前:
颜色:
rgb:0.7,0.9,1
矩形:
pos:self.pos
大小:self.size
尺寸提示:0.6,1
文字:“录音机”
文本大小:self.width-10,self.height
哈利格:“左”
valign:“中间”
字体大小:12
颜色:.3、.3、.3,1
标签:
在以下情况之前:
颜色:
rgb:0.7,0.9,1
矩形:
pos:self.pos
大小:self.size
尺寸提示:0.4,1
文本:root.status
文本大小:self.width-10,self.height
哈利格:“对”
valign:“中间”
字体大小:12
颜色:.3、.3、.3,1
盒子布局:
高度:“40dp”
尺寸提示:无
方向:“水平”
盒子布局:
方向:“垂直”
尺寸提示:0.33,1
标签:
在以下情况之前:
颜色:
rgb:0.24.42
矩形:
pos:self.pos
大小:self.size
尺寸提示:1,0.65
文本:str(root.curevent)
大小:self.texture\u大小
哈利恩:“中心”
valign:“中间”
字体大小:16
颜色:1,1,1,1
标签:
在以下情况之前:
颜色:
rgb:0.24.42
矩形:
pos:self.pos
大小:self.size
尺寸提示:1,0.35
#文本:root.curevent
文本:“事件”
大小:self.texture\u大小
哈利恩:“中心”
valign:“中间”
字体大小:14
颜色:1,1,1,1
按钮:
尺寸提示:0.33,1
背景图片正常:'./dblButton.png'
背景向下:'./lblButton.png'
按:root.recording\u pause\u proc()
图片:
来源:root.recpbutton
center\u x:self.parent.center\u x
中心y:self.parent.center
按钮:
尺寸提示:0.33,1
背景图片正常:'./dblButton.png'
背景向下:'./lblButton.png'
按:root.stop\u recorder()
图片:
来源:root.stopbutton
center\u x:self.parent.center\u x
中心y:self.parent.center
仅就记录而言,将pumpmessages和pyhook与Kivy架构混用可能不是一个好主意。因此,我将实际的录制功能分离为一个单独的流程,其中kivy应用程序(通过kivy按钮的录制器控制)和录制器之间的状态处理通过双向队列进行通信