How to enable Windows console QuickEdit Mode from python? -


i'd force quickedit mode in console when running python script , disable right before terminating. there way that?

you can use ctypes call getconsolemode , setconsolemode.

ctypes definitions:

import msvcrt import atexit import ctypes 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  # 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 underlying winapi functions get_console_mode , set_console_mode. i've limited wrappers operating on console's active input buffer or active output buffer, i.e. \\.\conin$ , \\.\conout$. think simpler having worry file descriptors , handles. notably sys.stdin , sys.stdout may redirected elsewhere, may case c runtime's standard i/o file streams, file descriptors, , windows standard handles can getstdhandle. in these cases can still open conin$ , conout$, long process attached console.

def get_console_mode(output=false):     '''get mode of active console input or output        buffer. note if process isn't attached        console, function raises ebadf ioerror.     '''     device = r'\\.\conout$' if output else r'\\.\conin$'     open(device, 'r+') 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 mode of active console input or output        buffer. note if process isn't attached        console, function raises ebadf ioerror.     '''     device = r'\\.\conout$' if output else r'\\.\conin$'     open(device, 'r+') con:         hcon = msvcrt.get_osfhandle(con.fileno())         kernel32.setconsolemode(hcon, mode) 

update_console_mode combines latter functions let pass in flags want set , mask of flags modify. includes flags clear. allows restoring previous mode registering atexit function.

def update_console_mode(flags, mask, output=false, restore=false):     '''update masked subset of current mode of active        console input or output buffer. note if process        isn't attached console, function raises        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 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_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 

Comments

Popular posts from this blog

ios - RestKit 0.20 — CoreData: error: Failed to call designated initializer on NSManagedObject class (again) -

java - Digest auth with Spring Security using javaconfig -

laravel - PDOException in Connector.php line 55: SQLSTATE[HY000] [1045] Access denied for user 'root'@'localhost' (using password: YES) -