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
Asked
Active
Viewed 939 times
4
-
1handle an exception, see: http://stackoverflow.com/questions/5803676/python-infinite-loop-after-aborting/5806450#5806450 – AnalyticsBuilder May 16 '11 at 15:01
-
@Dragan: catching `KeyboardInterrupt` doesn't facilitate to completely ignore Ctrl-C during `raw_input()` calls. – Sven Marnach May 16 '11 at 15:06
3 Answers
2
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)

tMC
- 18,105
- 14
- 62
- 98
-
WTF? if you're going to give me a down vote, why not tell me why? Does this not work? – tMC May 16 '11 at 23:52
-
1I think a comment indicated I transposed the options in the signal handler is more useful than a vote down with no explanation. I fixed it. – tMC May 17 '11 at 15:27
0
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.

Johan
- 5,003
- 3
- 36
- 50