4

I'd like to force QuickEdit Mode in the console when running a python script and then disable it right before terminating. Is there a way to do that?

1

4 Answers 4

8

for anyone trying to disable the QuickEdit and Insert mode for the current console only and is unable to find a simple solution:

import ctypes

kernel32 = ctypes.windll.kernel32
kernel32.SetConsoleMode(kernel32.GetStdHandle(-10), 128)
Sign up to request clarification or add additional context in comments.

2 Comments

Worked right out of the box like a dream and allowed me to end my work day on a high note. Thanks!
I don't like this solution as it also prevents me from stopping the python script using CTRL+C. 🤷‍♀️
7

You can use ctypes to call GetConsoleMode and SetConsoleMode.

ctypes definitions:

import msvcrt
import atexit
import ctypes
from ctypes import wintypes

kernel32 = ctypes.WinDLL('kernel32', use_last_error=True)

# input flags
ENABLE_PROCESSED_INPUT = 0x0001
ENABLE_LINE_INPUT      = 0x0002
ENABLE_ECHO_INPUT      = 0x0004
ENABLE_WINDOW_INPUT    = 0x0008
ENABLE_MOUSE_INPUT     = 0x0010
ENABLE_INSERT_MODE     = 0x0020
ENABLE_QUICK_EDIT_MODE = 0x0040
ENABLE_EXTENDED_FLAGS  = 0x0080

# output flags
ENABLE_PROCESSED_OUTPUT   = 0x0001
ENABLE_WRAP_AT_EOL_OUTPUT = 0x0002
ENABLE_VIRTUAL_TERMINAL_PROCESSING = 0x0004 # VT100 (Win 10)

def check_zero(result, func, args):    
    if not result:
        err = ctypes.get_last_error()
        if err:
            raise ctypes.WinError(err)
    return args

if not hasattr(wintypes, 'LPDWORD'): # PY2
    wintypes.LPDWORD = ctypes.POINTER(wintypes.DWORD)

kernel32.GetConsoleMode.errcheck= check_zero
kernel32.GetConsoleMode.argtypes = (
    wintypes.HANDLE,   # _In_  hConsoleHandle
    wintypes.LPDWORD,) # _Out_ lpMode

kernel32.SetConsoleMode.errcheck= check_zero
kernel32.SetConsoleMode.argtypes = (
    wintypes.HANDLE, # _In_  hConsoleHandle
    wintypes.DWORD,) # _Out_ lpMode

The following wraps the underlying WinAPI functions as get_console_mode and set_console_mode. I've limited the wrappers to operating on just the console's active input buffer or active output buffer, i.e. \\.\CONIN$ and \\.\CONOUT$. I think this is simpler than having to worry about file descriptors and handles. Notably sys.stdin and sys.stdout may be redirected elsewhere, as may also be the case for the C runtime's standard I/O FILE streams, file descriptors, and the Windows standard handles that you can get from GetStdHandle. In these cases you can still open CONIN$ and CONOUT$, as long as the process is attached to a console.

def get_console_mode(output=False):
    '''Get the mode of the active console input or output
       buffer. Note that if the process isn't attached to a
       console, this function raises an EBADF IOError.
    '''
    device = r'\\.\CONOUT$' if output else r'\\.\CONIN$'
    with open(device, 'r+') as con:
        mode = wintypes.DWORD()
        hCon = msvcrt.get_osfhandle(con.fileno())
        kernel32.GetConsoleMode(hCon, ctypes.byref(mode))
        return mode.value

def set_console_mode(mode, output=False):
    '''Set the mode of the active console input or output
       buffer. Note that if the process isn't attached to a
       console, this function raises an EBADF IOError.
    '''
    device = r'\\.\CONOUT$' if output else r'\\.\CONIN$'
    with open(device, 'r+') as con:
        hCon = msvcrt.get_osfhandle(con.fileno())
        kernel32.SetConsoleMode(hCon, mode)

update_console_mode combines the latter functions to let you pass in the flags you want to set and the mask of flags to modify. This includes flags to clear. It also allows restoring the previous mode by registering an atexit function.

def update_console_mode(flags, mask, output=False, restore=False):
    '''Update a masked subset of the current mode of the active
       console input or output buffer. Note that if the process
       isn't attached to a console, this function raises an
       EBADF IOError.
    '''
    current_mode = get_console_mode(output)
    if current_mode & mask != flags & mask:
        mode = current_mode & ~mask | flags & mask
        set_console_mode(mode, output)
    else:
        restore = False
    if restore:
        atexit.register(set_console_mode, current_mode, output)

Example:

if __name__ == '__main__':
    import os
    import sys
    import time

    if sys.stderr is None:
        os.close(2)
        sys.stderr = open('stderr.txt', 'w', buffering=1)

    print("%#06x, %#06x" % (get_console_mode(),
                            get_console_mode(output=True)))    

    flags = mask = ENABLE_EXTENDED_FLAGS | ENABLE_QUICK_EDIT_MODE
    update_console_mode(flags, mask, restore=True)

    print("%#06x, %#06x" % (get_console_mode(),
                            get_console_mode(output=True)))    

    time.sleep(10) # check console properties

3 Comments

Thank you! I'll give that a try.
I had to open CONIN$ with mode r rather than r+, otherwise I got the exception File or stream is not seekable, using the new Windows 10 console.
I should also note that, per the MS docs, you also need to have set ENABLE_EXTENDED_FLAGS to enable or disable quick edit mode. This tripped me up when I tried to do this in a PowerShell window.
4

This might be helpful to anyone trying to enable and disable only quick edit mode in Windows without disabling other features.

def quickedit(enabled=1): # This is a patch to the system that sometimes hangs
        import ctypes
        '''
        Enable or disable quick edit mode to prevent system hangs, sometimes when using remote desktop
        Param (Enabled)
        enabled = 1(default), enable quick edit mode in python console
        enabled = 0, disable quick edit mode in python console
        '''
        # -10 is input handle => STD_INPUT_HANDLE (DWORD) -10 | https://learn.microsoft.com/en-us/windows/console/getstdhandle
        # default = (0x4|0x80|0x20|0x2|0x10|0x1|0x40|0x200)
        # 0x40 is quick edit, #0x20 is insert mode
        # 0x8 is disabled by default
        # https://learn.microsoft.com/en-us/windows/console/setconsolemode
        kernel32 = ctypes.windll.kernel32
        if enabled:
            kernel32.SetConsoleMode(kernel32.GetStdHandle(-10), (0x4|0x80|0x20|0x2|0x10|0x1|0x40|0x100))
            print("Console Quick Edit Enabled")
        else:
            kernel32.SetConsoleMode(kernel32.GetStdHandle(-10), (0x4|0x80|0x20|0x2|0x10|0x1|0x00|0x100))
            print("Console Quick Edit Disabled")

quickedit(0) # Disable quick edit in terminal

Just disable the 0x40 flag which is quick edit

Comments

0

This is my Python solution to disable QuickEdit in windows Command Prompt (aka cmd.exe):

import platform
if platform.system() == 'Windows':
    # disable QuickEdit mode in Windows console
    import ctypes
    import ctypes.wintypes
    console_mode = ctypes.wintypes.DWORD()
    kernel32 = ctypes.windll.kernel32
    ret_bool = kernel32.GetConsoleMode(kernel32.GetStdHandle(-10), ctypes.byref(console_mode))
    console_mode.value &=  ~  0x0040 # clear quick edit mode bit
    if ret_bool:
        kernel32.SetConsoleMode(kernel32.GetStdHandle(-10), console_mode)
        print("Disabling quick edit in console");

This code is a mix of all answers given here so far. Thanks and deference is given to all these contributors.

If you want to restore the console mode, store the original console_mode in a variable for later use with the SetConsoleMode() call.

If you want to enable a console mode flag, just set the associated bit, e.g., for enabling quick edit mode:

console_mode.value |= 0x0040 # set quick edit mode bit

For more details on console mode bits, jump over to https://learn.microsoft.com/en-us/windows/console/setconsolemode

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.