After more research I managed to piece together a working solution using snippets from the asciimatics library source code. Hope this helps anyone with the same question
import win32console
import win32file
import win32con
ENABLE_EXTENDED_FLAGS = 0x0080
ENABLE_QUICK_EDIT_MODE = 0x0040
catch_interrupt = False
win_in = win32console.PyConsoleScreenBufferType(
win32file.CreateFile("CONIN$",
win32file.GENERIC_READ | win32file.GENERIC_WRITE,
win32file.FILE_SHARE_READ,
None,
win32file.OPEN_ALWAYS,
0,
None
)
)
win_in.SetStdHandle(win32console.STD_INPUT_HANDLE)
in_mode = win_in.GetConsoleMode()
new_mode = (in_mode | win32console.ENABLE_MOUSE_INPUT | ENABLE_EXTENDED_FLAGS)
new_mode &= ~ENABLE_QUICK_EDIT_MODE
if catch_interrupt:
# Ignore ctrl-c handlers if specified.
new_mode &= ~win32console.ENABLE_PROCESSED_INPUT
win_in.SetConsoleMode(new_mode)
try:
while True:
if len(win_in.PeekConsoleInput(1)) <= 0:
continue
event = win_in.ReadConsoleInput(1)[0]
if event.EventType == win32console.KEY_EVENT:
key_code = ord(event.Char)
print("Processing key: ", key_code)
elif event.EventType == win32console.MOUSE_EVENT:
print("Processing mouse: ", event.MousePosition.X, event.MousePosition.Y)
button = 0
if event.EventFlags == 0:
if (event.ButtonState &
win32con.FROM_LEFT_1ST_BUTTON_PRESSED != 0):
print("left button")
if (event.ButtonState &
win32con.RIGHTMOST_BUTTON_PRESSED != 0):
print("right button")
elif event.EventFlags & win32con.DOUBLE_CLICK != 0:
print("double click")
finally:
win_in.SetConsoleMode(in_mode)
Note: I did not want to use any terminal applications framework like textual because it is too complex to adapt my code into, and I would like to learn the logic behind it