简体   繁体   English

如何在python中使用paramiko库发送箭头键?

[英]How to send an arrow key use paramiko library in python?

I'm using python 2.7 and code ssh client with paramiko library, I use myhost.channel.send(chr(keycode)) to send every keycode to server.我正在使用 python 2.7 和带有paramiko库的代码 ssh 客户端,我使用myhost.channel.send(chr(keycode))将每个键码发送到服务器。 But It only works with 1 byte keycodes.但它仅适用于 1 字节键码。 I want to send other multi-byte keycodes like arrow keys.我想发送其他多字节键码,如箭头键。 How can I achieve this?我怎样才能做到这一点? Please help me.请帮我。

A GUI like Windows or MacOS identifies keys with 'keycodes', but an SSH pipe just transfers bytes, not keycodes.像 Windows 或 MacOS 这样的 GUI 用“密钥代码”识别密钥,但 SSH 管道只传输字节,而不是密钥代码。

Assuming the program running inside ssh on your server is interactive (that is, it's expecting a human to be using it), you'll need to find out what kind of byte-patterns it's expecting to receive.假设在您的服务器上的 ssh 中运行的程序是交互式的(也就是说,它希望有人使用它),您需要找出它希望接收什么样的字节模式。 When you open your channel, make sure you're calling .get_pty() and giving it a terminal parameter (the default, vt100 , is pretty safe).当您打开频道时,请确保您正在调用.get_pty()并为其提供一个终端参数(默认值vt100非常安全)。 Then, you'll need to read the documentation for the VT100 terminal to find out what byte sequences it sends when various keys are pressed.然后,您需要阅读 VT100 终端的文档以找出按下各种键时它发送的字节序列。 I recommend reading the Xterm Control Sequences documentation (Xterm is not strictly a vt100 emulator, but its documentation is very complete), and not confusingly mixed up with the hardware details of the original VT100 terminal).我建议阅读Xterm Control Sequences文档(严格来说 Xterm 不是 vt100 仿真器,但它的文档非常完整),并且不要与原始 VT100 终端的硬件细节混淆)。 Note that in that document, "CSI" effectively means the Python string '\\e[' .请注意,在该文档中,“CSI”实际上表示 Python 字符串'\\e['

For example, the Xterm Control Sequences document says that the arrow keys are "CSI A" for up, "CSI B" for down, "CSI C" for right, and "CSI D" for left.例如,Xterm Control Sequences 文档说箭头键是“CSI A”向上,“CSI B”向下,“CSI C”向右,“CSI D”向左。 In Python, that looks like:在 Python 中,这看起来像:

up = '\e[A'
down = '\e[B'
right = '\e[C'
left = '\e[D'

In macOS 10.13.2 you can use:在 macOS 10.13.2 中,您可以使用:

class Keyboard:
    up = '\x1b[A'
    down = '\x1b[B'
    right = '\x1b[C'
    left = '\x1b[D'

(I read them from sys.stdin ) (我从sys.stdin阅读它们)

I think in python you can do the following:我认为在 python 中你可以执行以下操作:

channel.sendall(chr(0x1b)+"[B")

0x1B is the ASCII Escape character for VT100 terminal. 0x1B 是 VT100 终端的 ASCII 转义字符。

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

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