简体   繁体   English

听 ssh 终端上的击键

[英]listen to keystrokes on an ssh terminal

I have a Python script that listens to arrow down and arrow up keys and reacts by moving a cursor between some choices.我有一个 Python 脚本,它监听向下箭头键和向上箭头键,并通过在某些选项之间移动 cursor 来做出反应。 I have achieved this using pynput .我已经使用pynput实现了这一点。 It works fine on my machine, but when I try to run it on my Raspberry Pi via SSH, it doesn't work.它在我的机器上运行良好,但是当我尝试通过 SSH 在我的 Raspberry Pi 上运行它时,它不起作用。 This is what I see on the terminal:这是我在终端上看到的:

终端输出

I have hit the down arrow twice, hence the double ^[[B , and the x cursor should have moved to the last square bracket.我已经击中了两次向下箭头,因此双^[[Bx cursor 应该移动到最后一个方括号。

I have seen this issue and it's leading me to think that pynput is not suited for my task.我已经看到了这个问题,这让我认为 pynput 不适合我的任务。 Is there any workaround?有什么解决方法吗?

The pynput module monitors the Linux input subsystem, which is for input devices attached to your local machine (keyboards, joysticks, mice, etc). pynput模块监控 Linux 输入子系统,该子系统用于连接到本地机器的输入设备(键盘、操纵杆、鼠标等)。 Keystrokes received over an ssh connection aren't handled by the input subsystem, because they're not coming from an input device -- they're just data coming over a network connection.通过 ssh 连接接收的击键不由输入子系统处理,因为它们不是来自输入设备——它们只是来自网络连接的数据。

Rather than relying on pynput , your code should just be monitoring standard input for keystrokes.而不是依赖pynput ,您的代码应该只监视标准输入的击键。 This will work both when running locally and when running over an ssh connection.这在本地运行和通过 ssh 连接运行时都有效。

The tricky part is that, as you have shown in your question, cursor movement keys like the down arrow generate multi-character escape sequences.棘手的部分是,正如您在问题中所示,cursor 移动键(如向下箭头)会生成多字符转义序列。 Fortunately, the curses module will handle much of this for you;幸运的是, curses模块会为你处理大部分事情; take a look at this example code :看看这个示例代码

#!/usr/bin/python

import curses

# get the curses screen window
screen = curses.initscr()

# turn off input echoing
curses.noecho()

# respond to keys immediately (don't wait for enter)
curses.cbreak()

# map arrow keys to special values
screen.keypad(True)

try:
    while True:
        char = screen.getch()
        if char == ord('q'):
            break
        elif char == curses.KEY_RIGHT:
            # print doesn't work with curses, use addstr instead
            screen.addstr(0, 0, 'right')
        elif char == curses.KEY_LEFT:
            screen.addstr(0, 0, 'left ')
        elif char == curses.KEY_UP:
            screen.addstr(0, 0, 'up   ')
        elif char == curses.KEY_DOWN:
            screen.addstr(0, 0, 'down ')
finally:
    # shut down cleanly
    curses.nocbreak()
    screen.keypad(0)
    curses.echo()
    curses.endwin()

If your only goal is to get the key presses registered, sshkeyboard library might be a better choice than curses library as it is simpler to use.如果您的唯一目标是注册按键,则 sshkeyboard库可能是比 curses 库更好的选择,因为它更易于使用。 It cannot handle drawing to display though.它无法处理要显示的绘图。

Installing:安装:

pip install sshkeyboard

Reacting to arrows:对箭头的反应:

from sshkeyboard import listen_keyboard

def press(key):
    if key == "up":
        print("up pressed")
    elif key == "down":
        print("down pressed")
    elif key == "left":
        print("left pressed")
    elif key == "right":
        print("right pressed")

listen_keyboard(on_press=press)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM