Key monitoring #525
-
Let's suppose the programmer want to know every keyboard key press at the client, is there a simple way? |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment 5 replies
-
The first thing you'll need to do is to request a PTY on the SSH session where you want this. This can be done by setting the "term_type" argument on the session where you want this. Doing so will disable local echo and buffering and local editing on the client. Once you've got that, you can call something like process.stdin.read(1) to have it return input a byte at a time. This won't correspond directly to "key presses", though, as some keys send a sequence of bytes. Since different keys can start off with the same subset of bytes, you need to keep reading until you have enough to distinguish one key from another before you act on it. The built-in line editor in AsyncSSH has the ability to do all of this, and you can even register for it to call arbitrary code of yours when specific key sequences are received, but you need to tell it exactly which sequences to look for so that it knows which ones it has to distinguish between. Do you really need to be called on ALL key presses, or just specific ones? Do you only need to be able to match keys which send a single byte, or would you need to handle things like arrow keys or function keys where the key could send multiple bytes? |
Beta Was this translation helpful? Give feedback.
The first thing you'll need to do is to request a PTY on the SSH session where you want this. This can be done by setting the "term_type" argument on the session where you want this. Doing so will disable local echo and buffering and local editing on the client. Once you've got that, you can call something like process.stdin.read(1) to have it return input a byte at a time. This won't correspond directly to "key presses", though, as some keys send a sequence of bytes. Since different keys can start off with the same subset of bytes, you need to keep reading until you have enough to distinguish one key from another before you act on it. The built-in line editor in AsyncSSH has the ability …