ArthurSonzogni / FTXUI

:computer: C++ Functional Terminal User Interface. :heart:
MIT License
6.64k stars 399 forks source link

Key strokes with modifiers #72

Closed HiImJulien closed 3 years ago

HiImJulien commented 3 years ago

Hey there! First of all, in the past three hours, upon discovering this project, I started to fall in love with it and its design. ❤️ Well done!

For my project I'd like to open a modal upon pressing CTRL-P. The modal is pretty much what you expect: A fuzzy finder for commands.

However, I failed to determine which event to check for when processing events. The example print_key_press only prints:

( 16 ) -> (special)

How can I detect that CTRL-P is being pressed?

While I am at it; how about the other modifier keys?

HiImJulien commented 3 years ago

Step 1: Try idea 1 to 10 Step 2: Fail Step 3: Write issue Step 4: Try idea 11 Step 5: Succeed


In FTXUI/src/.../event.cpp I found following line:

const Event Event::Tab = Event::Special({9});

I don't know why, but it didn't come to my mind to try Event::Special({16}) but everything else before.

For everyone stumbling upon this issue, scratching their head wondering what to do; do the following to estimate certain event codes:

  1. Run the example print_key_press
  2. Press a key or combination which you're searching; let's assume you're searching for the left array key. The sample prints ( 27 91 68 ). Which is the event code we need.
  3. Create an event object like this: auto myEvent = Event::Special({27, 91, 68})
  4. Use this to detect this event; i.e.:

    bool MyComponent::OnEvent(ftxui::Event ev) override
    {
    if(ev == myEvent)
    doSomethingAwesome();
    
    return true;
    }

@ArthurSonzogni Can this be documented somewhere? Took my a while to understand this; especially with the comment

// Documentation:
// https://invisible-island.net/xterm/ctlseqs/ctlseqs.html

which was a bit misleading.

ArthurSonzogni commented 3 years ago

Thanks!

Yes, it seems the characters map to:

ctrl: [a,z] -> [1,26]  (With many edge case)
shift: [a,z] -> [65,90]
normal: [a,z] -> [97,123]

https://en.wikipedia.org/wiki/ASCII#Control_code_chart

I will update the documentation and talk about ./example/util/print_key_press