Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/windows/16.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/user-interface/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如果按了某个键,我怎么能做一次真正的休息呢?[Python]_Python_Windows - Fatal编程技术网

如果按了某个键,我怎么能做一次真正的休息呢?[Python]

如果按了某个键,我怎么能做一次真正的休息呢?[Python],python,windows,Python,Windows,我的脚本使一段时间为真:从按下F4开始,但我希望它在按下F2时停止,我该怎么做 我正在尝试这个(使用pyhook),但不起作用 def onKeyboardEvent(event): if event.KeyID == 115: #F4 while True: selectAndCopy(468,722) getClipboard() time.sleep(2) if

我的脚本使一段时间为真:从按下F4开始,但我希望它在按下F2时停止,我该怎么做

我正在尝试这个(使用pyhook),但不起作用

def onKeyboardEvent(event):
    if event.KeyID == 115:      #F4
        while True:
            selectAndCopy(468,722)
            getClipboard()
            time.sleep(2)
            if event.KeyID == 113:
                break
    return True

您没有在循环中更改
event
,因此您不会期望
event.KeyID
在以前为115时突然变为113

您可以做的是,在处理F4按键时,启动一个计时器,每两秒钟执行一次selectAndCopy。当您通过F2击键获得另一个事件时,终止计时器

它可能看起来像这样:

def onKeyboardEvent(event):
    if event.KeyID == 115:      #F4
        startTimer(doTimer, 2)
    if event.KeyID == 113:
        stopTimer()

def doTimer():
    selectAndCopy(468,722)
    getClipboard()

您必须提供或找到
startTimer()
stopTimer()

的实现,因为您没有在循环中更改
event
,所以您不会期望
event.KeyID
在之前为115时突然变为113

您可以做的是,在处理F4按键时,启动一个计时器,每两秒钟执行一次selectAndCopy。当您通过F2击键获得另一个事件时,终止计时器

它可能看起来像这样:

def onKeyboardEvent(event):
    if event.KeyID == 115:      #F4
        startTimer(doTimer, 2)
    if event.KeyID == 113:
        stopTimer()

def doTimer():
    selectAndCopy(468,722)
    getClipboard()
您必须提供或找到
startTimer()
stopTimer()
的实现

  • 用F4更改变量为真,如果变量仍然为真,则执行新的计时器事件 例如在

    mylabel.after(2000,process)#process是完成您的工作的函数

  • 用F2将变量更改为False 取消计时器(取消后)

  • 制作关键事件

  • 用F4更改变量为真,如果变量仍然为真,则执行新的计时器事件 例如在

    mylabel.after(2000,process)#process是完成您的工作的函数

  • 用F2将变量更改为False 取消计时器(取消后)


  • 你能给我举个例子吗?我对pyhook一无所知,所以我会编一些东西,看看我能做什么。有了线程。Timer我可以做一个计时器,但它不会循环=/你能给我举个例子吗?我对pyhook一无所知,所以我会编一些东西,看看我能做什么。有了线程。Timer我可以做一个计时器,但它不会循环=/