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

寵の児 提交于 2020-01-14 19:30:30

问题


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. But It only works with 1 byte keycodes. I want to send other multi-byte keycodes like arrow keys. How can I achieve this? Please help me.


回答1:


A GUI like Windows or MacOS identifies keys with 'keycodes', but an SSH pipe just transfers bytes, not keycodes.

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. When you open your channel, make sure you're calling .get_pty() and giving it a terminal parameter (the default, vt100, is pretty safe). 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. 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). Note that in that document, "CSI" effectively means the Python string '\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. In Python, that looks like:

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



回答2:


In macOS 10.13.2 you can use:

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

(I read them from sys.stdin)




回答3:


I think in python you can do the following:

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

0x1B is the ASCII Escape character for VT100 terminal.



来源:https://stackoverflow.com/questions/11606569/how-to-send-an-arrow-key-use-paramiko-library-in-python

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!