How to prevent termination of a running program us

2019-05-07 01:15发布

I have written a piece of code in python, in which I am asking questions and users should give their input. Sometimes, these questions are difficult for the user to understand(they are non-english). So most of the time they want to copy paste the sentence into google translate. However, since this code is running in the command prompt,they have to select the text and using "right click --> copy" they can copy the text into google translate. Sometimes, by mistake the press "ctrl+c"(it is natural for everyone to use this combination for copying). Doing this will terminate the code, and they have to start over. I need to know I can prevent this from happening. In other words, if they press "ctrl+c" nothing happens and my software doesn't abort. thanks

3条回答
可以哭但决不认输i
2楼-- · 2019-05-07 01:27
import signal
def SigIntHand(SIG, FRM):
    print("Please Right click-copy. Ctrl-C does not work on the cmd prompt")

signal.signal(signal.SIGINT, SigIntHand)

or if you want it completely ignored:

import signal
signal.signal(signal.SIGINT, signal.SIG_IGN)
查看更多
我想做一个坏孩纸
3楼-- · 2019-05-07 01:46

When you hit ctrl+c it sends SIGINT to the running process. You can catch it as described here.

You can find more about the different types of signals here.

查看更多
该账号已被封号
4楼-- · 2019-05-07 01:51

If using X, the text is normally copied to the clipboard once it's selected. Just paste it using middle mouse button or Shift+insert.

查看更多
登录 后发表回答