Question:
Python, SendKeys module. What is causing these errors in the same line all the time?
Russel
2012-04-28 06:41:14 UTC
Software:
OS. Windows7-64bit Ultimate
Python 2.3 & 2.6 & 2.7-64bit

Quick overview of SendKeys in Python:
SendKeys source: http://pastebin.com/hYDzdVtM
If you know how the module works, then you know that you can simulate a keystroke with one line as follows after importing SendKeys.

SendKeys.SendKeys('a')

That would simulate a single keypress of the key a. To simulate something like Ctrl + a:

SendKeys.SendKeys('^a')

Where the ^ character represents the Ctrl key.

Overview of the problem:
Every combination of keys that I've used so far works fine.

But... when I try Ctrl + c, something must go wrong. These are the two errors I get.

Error #1
Traceback (most recent call last):
..File "", line 1, in
....SendKeys.SendKeys('^c')
..File "C:\Python26\lib\site-packages\SendKeys.py", line 358, in SendKeys
....playkeys(_keys, pause)
..File "C:\Python26\lib\site-packages\SendKeys.py", line 311, in playkeys
....time.sleep(pause)
KeyboardInterrupt

Error #2:
Traceback (most recent call last):
..File "", line 1, in
....SendKeys.SendKeys('^c')
..File "C:\Python26\lib\site-packages\SendKeys.py", line 358, in SendKeys
....playkeys(_keys, pause)
..File "C:\Python26\lib\site-packages\SendKeys.py", line 311, in playkeys
....time.sleep(pause)
TypeError: 'int' object is not callable

Observations:
After the program stops and traceback prints the Ctrl key is permanently pressed(activated) unless I tap it to get it to get it to release.

All the searching I've done so far suggests that this is a problem not just with python SendKeys but various other modules in various languages. At this point it would make sense to just look to another way of copying, such as cutting instead of copying which works, but it would be really nice to know just what is the cause of this. Thanks for any help.

I'm a programming student so still learning.
Four answers:
mata
2012-04-28 17:38:12 UTC
Ctrl-C sends a signal to the running program (in this case the python interpreter), which causes the KeyboardInterrupt-Exception to be raised in all waiting methods (here the time.wait used to delay between keydown and keyup) and terminates the interpreter... that also means that the keyup-signal never is sent, that's why your ctrl-key remains 'pressed' until you touch it.

so the beheviour you're seeing is normal and expected.

on linux it's fairly easy to trap the SIGINT-signal using:

signal.signal(signal.SIGINT, handlerfunction)

but i don't think that works on windows...



edit: here is an example how to do it on windows:

http://danielkaes.wordpress.com/2009/06/04/how-to-catch-kill-events-with-python/
lattin
2017-01-05 02:02:24 UTC
Sendkeys Python
proto
2016-11-10 09:32:24 UTC
Python Sendkeys
anonymous
2016-12-03 02:06:28 UTC
i'm no longer useful how a lot outcry it brought about or did not reason, reason it replaced into many years before my time. yet i have considered it, and that i loved it for the most area. it really is after all playing off the theory that the "genuine" Jesus replaced into there and human beings did not see him for who he replaced into, they anointed their own messiah (Brian) as a change. the basically challenge that did problem me replaced into the crucifixion scene. The music replaced into lovable. besides the indisputable fact that the imagery replaced into uncomfortably sacrilegious. I later stated an interview with the producer (director? i am going to't remember which) of the movie putting forward he had those similar sentiments at the same time as they were capturing that scene. It replaced into really over the line. nevertheless nowhere close to very nearly as good because the Holy Grail, besides the indisputable fact that. Peace to you.


This content was originally posted on Y! Answers, a Q&A website that shut down in 2021.
Loading...