Skip to content

win32console

Interface for interacting with windows api console functions.

The core export is the enable_virtual_processing context manager that redirects console input events and inputs them as ascii codes; this includes mouse input.

Credits

StdDevice

Bases: Enum

The available standard devices for windows.

Source code in pytermgui/win32console.py
52
53
54
55
56
57
class StdDevice(Enum):
    """The available standard devices for windows."""

    IN = -10
    OUT = -11
    ERR = -12

enable_virtual_processing()

Dummy context manager for non windows systems

Source code in pytermgui/win32console.py
148
149
150
151
152
153
154
@contextmanager
def enable_virtual_processing():
    """Dummy context manager for non windows systems"""
    try:
        yield
    finally:
        pass

get_console_mode(std)

Get the console mode for the given standard device.

Parameters:

Name Type Description Default
std HANDLE

The handle to the standard device to get the settings from

required

Returns:

Name Type Description
False DWORD

when setting the mode fails

Source code in pytermgui/win32console.py
74
75
76
77
78
79
80
81
82
83
84
85
86
def get_console_mode(std: HANDLE) -> DWORD:
    """Get the console mode for the given standard device.

    Args:
        std (HANDLE): The handle to the standard device to get
            the settings from

    Returns:
        False: when setting the mode fails
    """
    mode = DWORD()
    _GetConsoleMode(std, byref(mode))
    return mode

get_std_handle(handle=StdDevice.OUT)

Retrieves a handle to the specified standard device (stdin, stdout, stderr)

Parameters:

Name Type Description Default
handle int

Indentifier for the standard device. Defaults to -11 (stdout).

StdDevice.OUT

Returns:

Type Description
HANDLE

wintypes.HANDLE: Handle to the standard device

Source code in pytermgui/win32console.py
59
60
61
62
63
64
65
66
67
68
def get_std_handle(handle: StdDevice = StdDevice.OUT) -> HANDLE:
    """Retrieves a handle to the specified standard device (stdin, stdout, stderr)

    Args:
        handle (int): Indentifier for the standard device. Defaults to -11 (stdout).

    Returns:
        wintypes.HANDLE: Handle to the standard device
    """
    return cast(HANDLE, _GetStdHandle(handle.value))

set_console_mode(std, mode)

Set the console mode for the given standard device.

Parameters:

Name Type Description Default
std HANDLE

The handle to the standard device

required
mode int

The mode / setting flags to set to the device

required

Returns:

Type Description
bool

False when setting the mode fails

Source code in pytermgui/win32console.py
88
89
90
91
92
93
94
95
96
97
98
def set_console_mode(std: HANDLE, mode: int) -> bool:
    """Set the console mode for the given standard device.

    Args:
        std (HANDLE): The handle to the standard device
        mode (int): The mode / setting flags to set to the device

    Returns:
        False when setting the mode fails
    """
    return _SetConsoleMode(std, mode) != 0