Question

I'm writing an application using Python curses and I would like to know if there is a way to disable function keys. I have set the keypad(1) to read one character at a time.. This works well for Insert, HOME, DELETE, PAGEUP and PAGEDOWN keys but does not work for function keys...

I need to disable the function keys totally and I want to control all the keys behaviour.

Thanks

No correct solution

OTHER TIPS

You can use the ascii codes: for example 27 is ESC

You cannot make function keys go away (unless you limit the users to very plain keyboards), but you can detect them and ignore them if that is what you want to do.

You can either turn keypad on, and ignore non-ASCII characters (outside the 0 to 255 range), or turn it off and filter them out yourself. While the Python curses interface provides symbolic constants by which keys can be compared, the underlying curses library returns values past 255 for function-keys. A simple range-check would be enough to exclude any unwanted keys.

Function keys (also termed special keys) if not single control-characters (codes 0 to 31 or 127 — see the Python curses.ascii class for names) are usually sent as escape sequences that follow the ECMA-48 formatting rules for control sequences sent in the other direction: from host computer to terminal.

The format rules use different ASCII characters for different parts of the escape sequence. Here is a summary of the most frequently used format (CSI):

  • ESC
  • [
  • optional character designating a private sequence (one of <, =, >, ?)
  • optional numeric parameters separated by semicolon (;)
  • final character (from the range 64 to 126).

Some programs (such as vi) accept a plain ESC character, and must wait a short time after receiving an escape character to decide if the following characters show that a special key is being typed. If you do not have to do this you can (like emacs) simply assume that every escape character starts a special key.

A related question was asked in Standard keys functions in curses module

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top