[Pythonmac-SIG] Re: Console input in the IDE?
Christopher Smith
csmith@blakeschool.org
Thu, 11 Apr 2002 23:05:58 -0500
> I'd like to avoid that awful Input window in the IDE that comes up
> whenever I call raw_input().
<cut>
The following is a step toward what you are looking for. It doesn't
allow editing of the input line, but if that's not a problem then it
will do the string input and echo the keypresses to the output window.
When you hit a backspace a character is echoed to the screen and a
character is subtracted from the string to be returned but the character
is not erased on teh screen.
Also, this prompt() function doesn't make the output window active so
if you request input you have to make sure that the output window is
visible before you do or else you won't be able to see that data is being
requested.
Below, inkey() is used by prompt()
HTH,
/c
####
def inkey():
"""
A function which returns the current ASCII key that is down;
if no ASCII key is down, the null string is returned. The
page http://www.mactech.com/macintosh-c/chap02-1.html was
very helpful in figuring out how to do this. If cmd-. is
pressed without the shift key a -1 is returned.
"""
#cps 4/2002
import Carbon
if Carbon.Evt.EventAvail(0x0008)[0]==0: # 0x0008 is the keyDownMask
return ''
else:
#
# The event contains the following info:
# (what,msg,when,where,mod)=Carbon.Evt.GetNextEvent(0x0008)[1]
#
# The message (msg) contains the ASCII char which is
# extracted with the 0x000000FF charCodeMask; this
# number is converted to an ASCII character with chr() and
# returned
#
(what,msg,when,where,mod)=Carbon.Evt.GetNextEvent(0x0008)[1]
if mod & 0x0100 and mod & 0x0200 == 0: #cmd and not shift
return -1
return chr(msg & 0x000000FF)
def prompt(say):
"""
A function that waits for a carriage return and concatenates
keyboard presses in the mean time and returns these presses
as a \n terminated string. If the user presses cmd-. without
the shift key down, a null string is returned; if the user
only typed chr(13) then a '\n' is returned.
"""
#cps 4/2002
import sys,Carbon
sys.stdout._buf = say or '? '
sys.stdout.flush()
ans=''
x=inkey()
while x<>-1 and x<>chr(13):
if x:
if ord(x)<>8:
ans+=x
else:
ans=ans[:-1]
x=inkey()
if x<>-1 and x<>'':
#don't know how to erase a character from the display, though.
#I suppose that if you detect a backspace you could print on
#a new line all the characters that have been entered so far.
sys.stdout._buf=x
sys.stdout.flush()
if x == -1:
return '' #got a user cancel
else:
return ans + '\n'
####