473,394 Members | 1,722 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,394 software developers and data experts.

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

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

Jan 30 '07 #1
4 2284

ti***********@tom.com wrote:
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

Jan 30 '07 #2
On 30 Jan 2007 05:44:40 -0800, Szabolcs Nagy <ns*******@gmail.comwrote:
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.

--
mvh Björn
Jan 30 '07 #3
"Szabolcs Nagy" <ns*******@gmail.comwrote:
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.

Jan 30 '07 #4
En Tue, 30 Jan 2007 05:50:29 -0300, <ti***********@tom.comescribió:
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 ---

--
Gabriel Genellina

Jan 30 '07 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
by: Scott | last post by:
I am using a TCPIP connection to communicate over port 23 (telnet) to a server, and I am having to mimic normal command line interface you owuld see in a telnet session (i.e. I have to write to...
3
by: Mr. x | last post by:
Hello, I want to capture the event when the <ctrl> key is pressed. How can I do that ? Thanks :)
10
by: chirs | last post by:
I have a code to disable ctrl-v (paste) on the 2nd box. The problem is that when I type ctrl-v, the text shows, then disappear after I release ctrl-v. How can I make it not to show in the box. In...
8
by: MLH | last post by:
I use a mouse-down procedure to trap right mouse clicks and CTRL-Right mouse clicks. Running the procedure must put honey or some other sticky substance into my keyboard because subsequent...
1
by: Pedro Graca | last post by:
Well ... I've been using Windows for a long time before I switched to Linux. I'm very used to type CTRL-ALT-DEL everytime I'm leaving my computer or arriving to it. It's hapenned _twice_ (in the...
3
by: Stefan | last post by:
Hy, i have an app and i must disable this combination: ALT+F4; CTRL+ALT+DEL; CTRL+ESC;ALT+TAB like this: i find something on Internet and i can block ALT+F4 protected override...
8
by: Christian Blackburn | last post by:
Hi Gang, In most programs Ctrl+A is Select All. I'm inclined to use the most common keyboard shortcuts, whenever possible. What are the names of some programs that have a key assigned for...
17
by: Kbalz | last post by:
I'm developing a Wizard type program, and I'm flipping between steps by using a TabControl.. buttons on each tabPage move to the next or previous tabPage. When I press CTRL+Tab however, the...
1
by: phcmi | last post by:
Within a mousedown event, can you please show a code sample of how to ascertain whether the CTRL and/or ALT and/or SHIFT keyboard key is pressed? Thank you
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.