简体   繁体   中英

Turn off Windows 10 console "Mark" mode from my application

I've got a console application written in Python. Windows 10 console's "Mark" mode is frustrating me to no end as users accidentally click in the application while doing something as simple as switching windows. Do I have any way to control and stop this?

For those who are unaware of Mark mode, it is when a user selects some text in the console window. When the program next writes to stdout, the entire program is paused , which is very annoying.

Automatic "Mark" mode using the mouse in windows 10, is "Quick Edit" mode from prior versions. I believe the only difference is that it is now on by default. Quick Edit Mode can be enabled/disabled from code:

import time
import win32console

ENABLE_QUICK_EDIT_MODE = 0x40
ENABLE_EXTENDED_FLAGS = 0x80


def quick_edit_mode(turn_on=None):
    """ Enable/Disable windows console Quick Edit Mode """
    screen_buffer = win32console.GetStdHandle(-10)
    orig_mode = screen_buffer.GetConsoleMode()
    is_on = (orig_mode & ENABLE_QUICK_EDIT_MODE)
    if is_on != turn_on and turn_on is not None:
        if turn_on:
            new_mode = orig_mode | ENABLE_QUICK_EDIT_MODE
        else:
            new_mode = orig_mode & ~ENABLE_QUICK_EDIT_MODE
        screen_buffer.SetConsoleMode(new_mode | ENABLE_EXTENDED_FLAGS)

    return is_on if turn_on is None else turn_on

quick_edit_enabled = quick_edit_mode()
while True:
    print('Quick edit is %s' % ('on' if quick_edit_enabled else 'off'))
    time.sleep(3)
    quick_edit_enabled = quick_edit_mode(not quick_edit_enabled)

I'm a little short on reputation to comment on Stephen's answer so I'm posting a separate answer.

To make this multi-platform/environment friendly add some conditional checks to skip over the code when not running on windows or when no console is attached to the process such as when running inside an IDE or when built via pyinstaller etc:

if os.name == "nt" and sys.stdout.isatty():
    # stephen's code here...

This prevents errors from being raised in several cases. In my opinion you should also add a try/catch-all around the code block since os-implementations of the methods involved are unknown and are known to raise exceptions and be finicky. In the worst case scenario I would rather my code continue to run with QuickEdit enabled than fail because of it.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM