Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
371 views
in Technique[技术] by (71.8m points)

python - 如何使用click.getchar()识别“ Enter Key”(Python)(How to use click.getchar() to identify “Enter Key” (python))

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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

I would suggest using (or referring to) the keyboard library.

(我建议使用(或引用) 键盘库。)

The benefit here is that you'll be able to comply with Windows and Linux (OSX seems to be experimental in this library)

(这样做的好处是您将能够遵守Windows和Linux(此库中的OSX似乎是实验性的))

To answer your question specifically, it seems like you should be using 0x0d for Enter

(要具体回答您的问题,似乎您应该使用0x0d作为Enter键)

Edit: Add link to Enter key

(编辑:将链接添加到Enter键)


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...