How to capture `Ctrl + Enter` keyboard-event in console on macOS?

I'm working on a CLI tool which need multi-line input. It's naturally, Enter => Newline; Ctrl + Enter => Submit`.

However, I tried console, crossterm and termion crates, all of them cannot capture Ctrl+Enter keyboard event idlealy.

For example,

  • On Windows, console crate treats Enter as Key::Enter, and Ctrl+Enter as Key::Unknown + Key::Char('\n')
  • On macOS, both Enter and Ctrl+Enter as Key::Enter

In other words, I cannot know wheter Ctrl pressed when Enter pressed on macOS. It seems hard on unix-term.

If someone can solve it with winit without Window focused, please give me a little demo :pray:!

That's true. What do you expect, then? A program which magically does something that's not possible to do?

It's not that crates cannot capture Ctrl+Enter keyboard event idlealy, it's just terminal doesn't distinguish them.

Now, on some terminals that's, indeed, possible, but there are many ways to do that and many ways to do that, you would need to create a whole database to handle different terminals.

Maybe get the status of Ctrl key (pressed or released) through OS.

I also tried winit, it captured ctrl keyboard event, but need Window focused. I saw someone said it may work.

As said before, Windows can... Ctrl + Enter => Unknown + Char('\n') while Enter => Enter on Windows.

And better to use winapi GetAsyncKeyState.

But for macOS or Linux, it's hard.

Here's another thread on the topic, but there is no posted solution:

Most modern OSes closed that loophole by now. Ironically enough Windows may be the only exception.

Not even Windows can do that, try to connect to your Windows system via SSH and see how it'll behave.

Just on Windows there are terminal apps and console apps and most people talk about “terminal apps” when they actually want/need “console apps”.

It just wasn't a thing when ADM-3A was developed (google it). And POSIX terminals still live in that world.

Extended somewhat, sure, and, sometimes, even extended to the level where you may reliably distingush Enter from Ctrl+Enter… but extended differently by different vendors!

That's why it's hard: there are many different terminals (even on Windows!) but there are just one Windows console[1].

Windows too, when you connect to it remotely, but on Windows that's not a common situation.


  1. Actually no, it's two now. ↩︎

Life is hard ...

I should give up getting Ctrl state from a terminal then, since it's impassible.

For Windows, using winapi, Linux x11, macOS cocoa may be the solution, but it must be beyond my ability just for a CLI tool. And it's useless for remote ssh.

Now I think using Tab to switch between Edit and View mode to support multi-line input is good enough then.

By the way, if anyone is searching for a CLI dialog crate like dialoguer but supporting multi-line input, this worths a try.

This topic was automatically closed 90 days after the last reply. We invite you to open a new topic if you have further questions or comments.