简体   繁体   English

如果按下某些键,我该如何休息一会儿? [蟒蛇]

[英]How can I make a while True break if certain key is pressed? [Python]

My script make a while True: begin with the F4 pressed, but I want it to stop when the F2 is pressed, how can I do it? 我的脚本有一段时间是True:从按下F4开始,但是我希望在按下F2时停止它,我该怎么办?

I'm trying this (using pyhook) but doesn't work... 我正在尝试这个(使用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

You're not changing event within your loop, so you wouldn't expect event.KeyID to suddenly become 113 when it was 115 previously. 您不会在循环中更改event ,因此不会期望event.KeyID在以前为115时突然变为113。

What you might do is, on handling an F4 keypress, start a timer that does the selectAndCopy every two seconds. 您可能要做的是,在处理F4按键时,启动一个计时器,每两秒钟执行一次selectAndCopy。 When you get another event with an F2 keystroke, kill the timer. 当您再次按下F2键时,请杀死计时器。

It could look something like this: 它可能看起来像这样:

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

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

You would have to provide or find implementations of startTimer() and stopTimer() . 您将必须提供或找到startTimer()stopTimer()

Make key event which 进行关键事件

  1. change variable True with F4 and if the variable is still True do new timer event for example in Tkinter 用F4更改变量True,如果变量仍然为True,则在Tkinter中执行新的计时器事件

    mylabel.after(2000, process) # process is the function to do your stuff mylabel.after(2000,process)#process是执行您的工作的函数

  2. change variable False with F2 and cancels the timer (after_cancel) 用F2更改变量False并取消计时器(after_cancel)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何判断Python是否按下了某个键? - How can I tell if a certain key was pressed in Python? 我如何做到在按下一个键并释放另一个键的同时发生动作? (pygame的/蟒) - How do I make it so that while a key is pressed and another key is released an action occurs? (Pygame/python) 无法跳出 Python “while True” 循环 - Can not break out of Python “while True” loop 如何使 function 重复,直到使用 pynput 在 python 上按下特定键? - How can I make a function repeat until a specific key is pressed on python with pynput? 如何打破 Python 上的 while 循环? - How can I break the while loop on Python? 按下键时返回 true 的 Python 代码如果释放 false? - Python Code that returns true while key is pressed down false if release? 如何在我键入一个指示“中断”的值时停止 Python - How to stop Python while I key in a value that indicates "break" 如何在 python 暂停期间检测到按下的键? - How can I detect a key pressed during a pause in python? 如何暂停while循环,直到按下某个键? - How to pause a while loop until a certain key is pressed? 检测何时按下一个键并在按下另一个键时中断[Python] - Detecting when a key is pressed and break when another key is pressed[Python]
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM