如何从python启用Windows控制台QuickEdit模式?

R01*_*01k 4 python windows console prompt

我想在运行python脚本时在控制台中强制使用QuickEdit Mode,然后在终止之前禁用它.有没有办法做到这一点?

Ery*_*Sun 7

您可以使用ctypes来调用GetConsoleModeSetConsoleMode.

ctypes定义:

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
Run Code Online (Sandbox Code Playgroud)

以下将基础WinAPI函数包装为get_console_modeset_console_mode.我已经将包装器限制为仅在控制台的活动输入缓冲区或活动输出缓冲区上运行,即\\.\CONIN$\\.\CONOUT$.我认为这比担心文件描述符和句柄更简单.值得注意的是sys.stdin,sys.stdout可能会重定向到其他地方,也可能是C运行时的标准I/O FILE流,文件描述符和您可以获得的Windows标准句柄的情况GetStdHandle.在这些情况下,您仍然可以打开CONIN$CONOUT$,只要过程是连接到控制台.

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)
Run Code Online (Sandbox Code Playgroud)

update_console_mode结合后面的函数,让你传入flags你想要设置的和mask要修改的标志.这包括要清除的标志.它还允许通过注册atexit函数来恢复先前的模式.

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)
Run Code Online (Sandbox Code Playgroud)

例:

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
Run Code Online (Sandbox Code Playgroud)


小智 5

对于任何试图仅对当前控制台禁用快速编辑和插入模式且无法找到简单解决方案的人:

import ctypes

kernel32 = ctypes.windll.kernel32
kernel32.SetConsoleMode(kernel32.GetStdHandle(-10), 128)
Run Code Online (Sandbox Code Playgroud)