5

I am writing an application which interfaces with Nano in linux. Nano requires to receive control sequences to save/exit/and work with the files (^G ^R ^O ^Y ^K, etc...)

I figured out the unicode for ^X = U+0018 by blind chance. But as I want my application to be complete I wish to be able to have a complete list of unicode chars for combinations of ctrl/alt/shift + any other key.

I tried to do this by connecting between shells with netcat pressing (for example) CTRL+B and seeing what appears on the other side. This works for some of them, not for all as the terminal 'interprets' the escape before it is sent.

I'm offering a bounty now, what I want is either

  • A) a method to acquire all the escape codes, or
  • B) a comprehensive list that includes the ones I have noted above.
1

4 Answers 4

6

Here is the Control-{} code from the ascii table, maybe this is what you need.

Char    Decimal     Hex
ctrl-@      0        00
ctrl-A      1        01
ctrl-B      2        02
ctrl-C      3        03
ctrl-D      4        04
ctrl-E      5        05
ctrl-F      6        06
ctrl-G      7        07
ctrl-H      8        08
ctrl-I      9        09
ctrl-J      10       0A
ctrl-K      11       0B
ctrl-L      12       0C
ctrl-M      13       0D
ctrl-N      14       0E
ctrl-O      15       0F
ctrl-P      16       10
ctrl-Q      17       11
ctrl-R      18       12
ctrl-S      19       13
ctrl-T      20       14
ctrl-U      21       15
ctrl-V      22       16
ctrl-W      23       17
ctrl-X      24       18
ctrl-Y      25       19
ctrl-Z      26       1A
ctrl-[      27       1B
ctrl-\      28       1C
ctrl-]      29       1D
ctrl-^      30       1E
ctrl-_      31       1F

and to input ctrl-X as you know it, you need to input unicode (18) in anyway you can to the file. Sometimes \x0018 may do the trick, it depends on how the interpreter will explain it

Sign up to request clarification or add additional context in comments.

Comments

1

You haven't include so much code in the question so I don't know exactly how you're accomplishing your task. The best way to handle subprocess with Python is to use the subprocess module. If you can provide more detail of your code like how are you handling subprocesses and binding them, we could be more helpful.

Having said that, Linux support sending signals to any process. This is accomplished with the kill system call. Python also wraps this system call in os.kill. You only have to pass the target's pid and the signal number as arguments. If you're using subprocess, Popen.send_signal should be the method to use.

To resume it, you need the Process ID of the target process --in your case should be the terminal or Nano-- and need to know the signal number.

In your case, I don't think CTRL+X is a registered signal. Instead it is a special character (some value mapped to CTRL+X in the application or the terminal). You need to figure out what this value is and send it through your process.

Hope this helps!

8 Comments

IF CTRL+X isn't a registered signal, how does nano know you aren't writing the key into the file rather than asking to save?..
By reading the letter combination CTRL and then X while CTRL still pressed. Normally you won't type any letter combination beginning with CTRL wouldn't you?
It's not a signal, it's just a special character.
@smassey That should be! Thanks :)
Got it, its: Hexadecimal code: 18, U+0018. ctrl+shift+U+0018 in gedit, which gives you the unicode character, copy/paste it into the terminal, and voila. thanks for your assistance anyway.
|
0

Although I really don't think you should be doing what you're doing (opening Nano as a subprocess then sending in control sequences to it). It seems to me like you're trying to control Nano with an expect script or at least the same style. In python (I'm unfamiliar with pty.spawn but the signature already seems lacking the function you need: bi-directional communication with the child) you should look up popen instead: http://docs.python.org/2/library/subprocess.html#subprocess.Popen

With popen you can spawn off a child and still "read and write" to it from python by setting up your custom stdin/out/err file descriptors.

As for the exact value of what to send, check this out: http://publib.boulder.ibm.com/infocenter/comphelp/v8v101/index.jsp?topic=%2Fcom.ibm.xlcpp8a.doc%2Fcompiler%2Fref%2Fruascii.htm

It would appear as if: "\x18" should do the trick just fine.

Comments

0

Figured it out:

As Paulo Bu said, its not signals that the terminal receives when pressing keys like CTRL+X, it is just key combinations. CTRL+X has a unicode equivalent.

Hexadecimal code: 18, U+0018. 

Process to get the character: press ctrl+shift+U+0018 in an editor like gedit which displays the character.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.