I'm making a Python script and wanted to let the user use the Arrow Keys to navigate through the screens.
(我正在制作一个Python脚本,并希望让用户使用箭头键在屏幕上导航。)
I found this answer from @yatsa, that really worked for the Arrows: https://stackoverflow.com/a/46449300/11903801
(我从@yatsa找到了这个答案,它确实对Arrows有用: https ://stackoverflow.com/a/46449300/11903801)
#!/usr/bin/python
import click
printable = '0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ!"#$%&'()*+,-./:;<=>?@[\]^_`{|}~'
while True:
click.echo('Continue? [yn] ', nl=False)
c = click.getchar()
click.echo()
if c == 'y':
click.echo('We will go on')
elif c == 'n':
click.echo('Abort!')
break
elif c == 'x1b[D':
click.echo('Left arrow <-')
elif c == 'x1b[C':
click.echo('Right arrow ->')
else:
click.echo('Invalid input :(')
click.echo('You pressed: "' + ''.join([ '\'+hex(ord(i))[1:] if i not in printable else i for i in c ]) +'"' )
This example is supposed to identify if it was pressed left-key or right-keys and it also prints the "py-string representation" of any other keyboard key pressed.
(本示例应该确定是按下了左键还是右键,并且还打印了按下的任何其他键盘键的“ py-string表示形式”。)
Said that, I had to change the code to make it work for me...
(话虽如此, 我必须更改代码才能使其对我有用...)
When I ran the code above it didn't identify the Left and Right arrows.
(当我在上面的代码中运行时,没有标识左箭头和右箭头。)
Instead it printed to me:
(相反,它打印给我:)
You pressed "\xe0K"
(for left arrow)
(You pressed "\xe0K"
(向左箭头))
and
(和)
You pressed "\xe0M"
(for right arrow)
(You pressed "\xe0M"
(对于右箭头))
What did I do?
(我做了什么)
Replace that values in elifs with this the console printed to me, and IT WORKED!
(用打印在我上面的控制台替换掉elifs中的值,并且成功了!)
Ok, GOOD, so next I wanted to identify the "Enter Key", so I ran the script, press Enter, and it printed to me: You pressed "\xd"
(好的,好的,接下来我要确定“ Enter键”,因此我运行了脚本,按Enter键,然后打印给我: You pressed "\xd"
)
So, what did I do?
(那么,我做了什么?)
Created a new conditional using this string the console printed to me.
(控制台使用此字符串创建了一个新的条件。)
BUT...
(但...)
elif c == 'xd':
^
SyntaxError: (unicode error) 'unicodeescape' codec can't decode bytes in position 0-2: truncated xXX escape
Process finished with exit code 1
So, I don't know what's the problem, I tried to search the web but didn't found anything that explains it to me.
(因此,我不知道出了什么问题,我尝试在网上搜索,但没有找到任何可以解释给我的东西。)
I'm using Windows 7.
(我正在使用Windows 7。)
ask by Renato translate from so