Discussion:
[python-win32] Get key press in Windows 7
John Sampson
2014-11-27 18:06:24 UTC
Permalink
I have tried a module called readchar to make a Python 2.7 script detect
keystrokes in Windows 7.
I found it via a link from Stack Overflow.
When <ctrl>z is pressed its output is printed in the console as
u'\x1a'
According to Python 2.7 its type is 'str'. Seeing that it is preceded by
a 'u', why is it not 'unicode'?
While it appears in the console as above, if it is assigned to a
variable ( c = repr(readchar.readkey()) )
and then the value of the variable is tested:
print c == u'\x1a'
the answer is 'False'
This does not make sense. What type of object is a keystroke?

Perhaps I need to find some other way for a Python script detect
keystrokes but I am confused as to what Python sees them as.

Any advice would be welcome.

Regards

John Sampson
Randy Syring
2014-11-27 18:26:19 UTC
Permalink
Post by John Sampson
Post by John Sampson
u'\x1a'
u'\x1a'
Post by John Sampson
Post by John Sampson
c = u'\x1a'
c == u'\x1a'
True
Post by John Sampson
Post by John Sampson
repr(c)
"u'\\x1a'"
Post by John Sampson
Post by John Sampson
repr(c) == u'\x1a'
False
*Randy Syring*
Husband | Father | Redeemed Sinner

/"For what does it profit a man to gain the whole world
and forfeit his soul?" (Mark 8:36 ESV)/
Post by John Sampson
I have tried a module called readchar to make a Python 2.7 script
detect keystrokes in Windows 7.
I found it via a link from Stack Overflow.
When <ctrl>z is pressed its output is printed in the console as
u'\x1a'
According to Python 2.7 its type is 'str'. Seeing that it is preceded
by a 'u', why is it not 'unicode'?
While it appears in the console as above, if it is assigned to a
variable ( c = repr(readchar.readkey()) )
print c == u'\x1a'
the answer is 'False'
This does not make sense. What type of object is a keystroke?
Perhaps I need to find some other way for a Python script detect
keystrokes but I am confused as to what Python sees them as.
Any advice would be welcome.
Regards
John Sampson
_______________________________________________
python-win32 mailing list
https://mail.python.org/mailman/listinfo/python-win32
John Sampson
2014-11-27 20:30:20 UTC
Permalink
Many thanks - by excluding repr the code behaves in a comprehensible
way. The example code that was provided at Stack Overflow included repr
so I assumed that it was necessary for some reason.
If I were allowed to comment there I would ask why.

Regards

John Sampson
Post by John Sampson
Post by John Sampson
Post by John Sampson
u'\x1a'
u'\x1a'
Post by John Sampson
Post by John Sampson
c = u'\x1a'
c == u'\x1a'
True
Post by John Sampson
Post by John Sampson
repr(c)
"u'\\x1a'"
Post by John Sampson
Post by John Sampson
repr(c) == u'\x1a'
False
*Randy Syring*
Husband | Father | Redeemed Sinner
/"For what does it profit a man to gain the whole world
and forfeit his soul?" (Mark 8:36 ESV)/
Post by John Sampson
I have tried a module called readchar to make a Python 2.7 script
detect keystrokes in Windows 7.
I found it via a link from Stack Overflow.
When <ctrl>z is pressed its output is printed in the console as
u'\x1a'
According to Python 2.7 its type is 'str'. Seeing that it is preceded
by a 'u', why is it not 'unicode'?
While it appears in the console as above, if it is assigned to a
variable ( c = repr(readchar.readkey()) )
print c == u'\x1a'
the answer is 'False'
This does not make sense. What type of object is a keystroke?
Perhaps I need to find some other way for a Python script detect
keystrokes but I am confused as to what Python sees them as.
Any advice would be welcome.
Regards
John Sampson
_______________________________________________
python-win32 mailing list
https://mail.python.org/mailman/listinfo/python-win32
Tim Roberts
2014-11-28 06:44:36 UTC
Permalink
Post by John Sampson
I have tried a module called readchar to make a Python 2.7 script detect
keystrokes in Windows 7.
I found it via a link from Stack Overflow.
When <ctrl>z is pressed its output is printed in the console as
u'\x1a’
Right, because that’s the ASCII value for Ctrl-Z.
Post by John Sampson
While it appears in the console as above, if it is assigned to a
variable ( c = repr(readchar.readkey()) )
In this line, “readchar.readkey()” returns a string of length 1. It contains one character, with a value of 1A in hex. When you pass that through “repr”, you end up with a string of length 7: “u”, apostrophe, backslash, “x”, “1”, “a”, and apostrophe. Naturally, when you compare that to a string of length 1, it doesn’t match.
Post by John Sampson
This does not make sense. What type of object is a keystroke?
It’s a string containing one character. Wasn’t that obvious from your output?
--
Tim Roberts, ***@probo.com
Providenza & Boekelheide, Inc.
John Sampson
2014-11-28 08:46:15 UTC
Permalink
In answer to Time Roberts, I saw an item prefixed with 'u' (u'\x1a').
What is the purpose of this prefix? I would have thought it meant
'Unicode' but the type according to Python is 'str'.

Thank you for the explanation of 'repr'. The issue turned out to be the
inclusion of 'repr' in the sample code without evidence or explanation
of its purpose there. Once I was given a clue that 'repr' was the
problem and I eliminated it things became clear.

Regards

JohnSampson
Werner
2014-11-28 09:57:50 UTC
Permalink
Post by John Sampson
In answer to Time Roberts, I saw an item prefixed with 'u' (u'\x1a').
What is the purpose of this prefix? I would have thought it meant
'Unicode' but the type according to Python is 'str'.
In Py2:
a = u'x'
type(a)
<type 'unicode'>

In Py3.4:
a = u'x'
type(a)
<class 'str'>

Note that u'' is not available in Py 3.0 - 3.2.

See e.g. here for more details:
https://docs.python.org/3/howto/pyporting.html?highlight=unicode%20literal#from-future-import-unicode-literals

Werner

Loading...