3

I need to disable user mouse selection in the Windows console. Is it possible and how? I tried the function SetConsoleMode() to disable mouse input with it, but it did not work as I expected. Selecting was still possible.

Eryk Sun
  • 33,190
  • 5
  • 92
  • 111
seqedugi
  • 57
  • 2
  • 7
  • 1
    Is this what you really need, or just what you think is your solution? This really smells like an [XY Problem](http://xyproblem.info/). And even if it isn't, this setting is at the user's discretion. – IInspectable Oct 04 '17 at 14:22
  • I'm guessing that you're talking about using the mouse for cut-and-copy type operations? I think all you can control with `SetConsoleMode()` is whether mouse events can be read by the program. I don't know if console cut-and-copy is controllable using an API call; even if it is, I suspect it might vary between Windows releases. – Kevin Boone Oct 04 '17 at 14:22
  • @Ron: That flag controls, whether you get mouse input notifications in the console. It does not change, how the console itself responds to mouse input. – IInspectable Oct 04 '17 at 14:22
  • @IInspectable True. I stand corrected. – Ron Oct 04 '17 at 14:23
  • @eryksun Yes it works, thanks. – seqedugi Oct 05 '17 at 18:37

1 Answers1

6

The console's quick-edit mode allows the user to quickly select and copy text using the mouse, without having to first enter mark mode (i.e. Ctrl+M, or Edit -> Mark on the menu). It's usually convenient to enable quick-edit mode, but it does interfere with getting mouse input. You can disable it using a handle for the console input buffer as follows:

DWORD prev_mode;
GetConsoleMode(hInput, &prev_mode); 
SetConsoleMode(hInput, ENABLE_EXTENDED_FLAGS | 
    (prev_mode & ~ENABLE_QUICK_EDIT_MODE));

Remember to restore the previous mode at exit.

Eryk Sun
  • 33,190
  • 5
  • 92
  • 111
  • This doesn't work on Attached Console. I ` AllocConsole(); AttachConsole(GetCurrentProcessId());` I want to disable the selection or any input so that no interaction or selection with the console windows is possible. – Zingam May 22 '19 at 20:48
  • @Zingam, it seems like you want to disable all forms of input; use `SetConsoleMode(hInput, ENABLE_EXTENDED_FLAGS)`, which will disable all input modes. FYI, `AllocConsole` allocates and attaches to a console. Don't call `AttachConsole`; the call is probably failing anyway because the process is already attached to a console. – Eryk Sun May 22 '19 at 23:48
  • You have to obtain the handle using CreateFile (CONIN$) as opposed to using GetConsoleWindow - e.g. "HANDLE hInput = CreateFileW(L"CONIN$", GENERIC_READ | GENERIC_WRITE, FILE_SHARE_READ | FILE_SHARE_WRITE, nullptr, OPEN_EXISTING, 0, nullptr); " – Den-Jason Mar 05 '20 at 15:31
  • 1
    @Den-Jason, first try `GetStdHandle(STD_INPUT_HANDLE)`. If that handle cause `GetConsoleMode` to fail with `ERROR_INVALID_HANDLE`, then you can fall back on opening "CONIN$". FYI, `GetConsoleWindow` returns the console window handle, if the console has a window, which it may not. A window handle refers to a window object in the window manager, not a file object in the kernel. – Eryk Sun Mar 05 '20 at 23:08
  • @ErykSun OK, noted. The use of opening CONIN$ is based on the answer given to the MSDN question "how use the ReadConsole() for read enter key or any key" – Den-Jason Mar 06 '20 at 09:43