以编程方式发送键盘中断

时间:2011-09-29 19:01:59

标签: python windows

应用程序要求键盘中断。如何以编程方式发送键盘中断?我需要它来实现自动化。

3 个答案:

答案 0 :(得分:3)

在单独的线程中运行的代码可以通过调用KeyboardInterrupt在主线程中生成thread.interrupt_main()。 见https://docs.python.org/2/library/thread.html#thread.interrupt_main

答案 1 :(得分:1)

既然你提到了自动化,我假设你想要一个SendKeys for Python。试试这个:http://rutherfurd.net/python/sendkeys/

答案 2 :(得分:0)

如果你想通过 shell ./program 运行一个程序。在 Linux 中你可以做的是:

# Made a function to handle more complex programs which require multiple inputs.
run(){
    ./program
}

# Making a child process that will run the program while you stop it later.
run & 
childPid=($!)
# Process id of the program which you want to interrupt (via `run`).

# Time after which you want to interrupt
sleep 5

# Actual command to send the interrupt
kill -SIGINT $childPid

让我知道它是否也适用于 Windows。