How can I know both the Key c and Ctrl on the keyboard are pressed?

T

tidegenerator

Hi;
How can I know the Key c and Ctrl on the keyboard are pressed? Or how
to let the program press the

key Ctrl+c automatically? I just want to use python to develop a
script program.
gear
 
S

Szabolcs Nagy

Hi;
How can I know the Key c and Ctrl on the keyboard are pressed? Or how
to let the program press the

key Ctrl+c automatically? I just want to use python to develop a
script program.
gear

depends on where you got your input from and what do you exactly want

eg:
in a gui app you get input events from the gui toolkit (wx, gtk, sdl/
pygame...)
in a console app if you use curses lib then you can use getch() oslt

if you want a general solution to get input key events in a simple
script then you cannot do that.

however Ctrl+C is a special key combination: running python in a unix
terminal it raises KeyboardInterrupt exception, imho in a windows cmd
promt it raises SystemExit

so you can emulate those by using:
raise KeyboardInterrupt
or
raise SystemExit

hope this helps
 
?

=?ISO-8859-1?Q?BJ=F6rn_Lindqvist?=

however Ctrl+C is a special key combination: running python in a unix
terminal it raises KeyboardInterrupt exception, imho in a windows cmd
promt it raises SystemExit

No it is KeyboardInterrupt in Windows too.
 
D

Duncan Booth

Szabolcs Nagy said:
however Ctrl+C is a special key combination: running python in a unix
terminal it raises KeyboardInterrupt exception, imho in a windows cmd
promt it raises SystemExit
Your humble opinion is wrong.

Under windows Ctrl-C raises KeyboardInterrupt just as it does under
linux. Ctrl-break by default terminates the program (without invoking
Python's usual cleanup), but you can override that behaviour by
registering a different signal handler.

e.g.

import signal
signal.signal(signal.SIGBREAK,
signal.default_int_handler)

will make Ctrl-break raise KeyboardInterrupt just like Ctrl-C.
 
G

Gabriel Genellina

How can I know the Key c and Ctrl on the keyboard are pressed? Or how
to let the program press the

key Ctrl+c automatically? I just want to use python to develop a
script program.
gear

If you are on Windows and want to trap the Ctrl-C combination, just catch
the KeyboardInterrupt exception:

--- cut ---
from time import sleep

while 1:
try: sleep(1)
except KeyboardInterrupt: print "Ctrl-C pressed"
--- cut ---
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,770
Messages
2,569,584
Members
45,075
Latest member
MakersCBDBloodSupport

Latest Threads

Top