GithubHelp home page GithubHelp logo

blessed-input's Introduction

Blessed Input

A terminal input sanitiser

This is a small module which attempts to provide a sane method of determining what keys have been pressed, in order to enable more complicated input handling. Ie, Ctrl, Alt, Shift, Meta, F-key combinations.

Without blessed-input

# Ctrl + Down Arrow
if key == '\x1b[1;5B':
    move_cursor_y(1)
# Ctrl + HOME
if key == '\x1b[1;5H':
    set_cursor_x(0)

With blessed-input

if key == [KEY_CTRL, KEY_DOWN]:
    move_cursor_y(1)
if key == [KEY_CTRL, KEY_HOME]:
    set_cursor_x(0)

Normally when a key is pressed in blessed/ncurses, it's either a character, or a series of terminal escape sequences, or a legacy mapping.

This can be incredibly unhelpful when you're trying to write simple code that just wants to perform an action on a key combination.

With this module, when an input is pressed, you instead get a a list of the keys that were part of that event.

Usage

As a blessed.inkey replacement:

from blessed_input import get_key

...

pressed = keys.get_key(term)
print(pressed)

This function attempts to detect key sequences, and will receive further key events if this is the case using receive_sequence. It then sends the key press 'string' input to convert_keys for conversion to a list of key presses.

Key sequences are always in the order: [ KEY_META, KEY_CTRL, KEY_ALT, KEY_SHIFT, <key> ]

This should make matching patterns easier, as you don't need to look inside the list of each key, you just provide the sequence of keys you want in the above order.

For best compatibility, set your application to use term.raw() and term.cbreak().

To get the 'text' version of a key, simply call key.value (as they are an enumeration).

Note: that upper and lower-case keys are considered different characters. So for standard characters, you should not include the KEY_SHIFT in the combination.

Ie. KEY_A = 'A' KEY_a = 'a'

It should be noted that the meta keys will print the terminal key sequence.

Ie. KEY_ESCAPE = '\x1b'

Function-keys + Meta are converted properly. Ie. you won't get F-24 being pressed, you'll get F1-F12 with the appropriate META key.

Demos

See demo.py for an example of a "Text Editor-like" application.

See debug_keys.py for an example of receiving key-events and printing out the key combinations.

Manual conversion

If you want to convert a key you've received from blessed/ncurses, refer to the get_key implementation.

Caveats

The different ncurses modes act differently and have different reserved key-combinations. YMMV.

I've tried to parse every key combination I could get working (Ctrl, Alt, Shift, Meta, F-keys). I believe I got all the ones I can get working, to work.

Terminals don't let you use some key sequences, as they are internal. Eg. Ctrl+s turns on scroll-lock and cannot be used for "Save".

Known reserved key combinations that cannot be handled:

  • CTRL + i = TAB
  • CTRL + j = ENTER in non-"raw" mode
  • CTRL + m = ENTER in "raw" mode
  • CTRL + [ = ESCAPE

There are most likely others (CTRL+z, CTRL+s).

Opinion

Having to write this code demonstrated that it is impossible to use a standard Unix terminal to write any sort of rich-input based application.

The difficulty in just getting a sane list of key events is just excessive. And the terminal's reserved key combinations occupy common industry established key-combinations.

This is a deal-breaker.

Use something native or make something in an embeddable webapp (webpy, etc), and make it act like a terminal.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.