4

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

2
  • 1
    handle an exception, see: stackoverflow.com/questions/5803676/… Commented May 16, 2011 at 15:01
  • @Dragan: catching KeyboardInterrupt doesn't facilitate to completely ignore Ctrl-C during raw_input() calls. Commented May 16, 2011 at 15:06

3 Answers 3

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)
Sign up to request clarification or add additional context in comments.

2 Comments

WTF? if you're going to give me a down vote, why not tell me why? Does this not work?
I 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.
2

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.

Comments

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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.