Skip to content

Commit

Permalink
Fix CTRL_C not working on Windows
Browse files Browse the repository at this point in the history
  • Loading branch information
bczsalba committed Feb 11, 2022
1 parent ea78b12 commit 0113963
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 2 deletions.
9 changes: 8 additions & 1 deletion pytermgui/input.py
Original file line number Diff line number Diff line change
Expand Up @@ -329,11 +329,18 @@ def getch(printable: bool = False, interrupts: bool = True) -> Any:

try:
key = _getch()

# msvcrt.getch returns CTRL_C as a character, unlike UNIX systems
# where an interrupt is raised. Thus, we need to manually raise
# the interrupt.
if key == chr(3):
raise KeyboardInterrupt

except KeyboardInterrupt as error:
if interrupts:
raise KeyboardInterrupt("Unhandled interrupt") from error

return chr(3)
key = chr(3)

if printable:
key = key.encode("unicode_escape").decode("utf-8")
Expand Down
6 changes: 5 additions & 1 deletion pytermgui/window_manager.py
Original file line number Diff line number Diff line change
Expand Up @@ -715,7 +715,11 @@ def process_input(self) -> None:
"""Processes incoming input."""

while self._is_running:
key = getch()
key = getch(interrupts=False)

if key == chr(3):
self.stop()
break

if self.handle_key(key):
self._should_print = True
Expand Down

0 comments on commit 0113963

Please sign in to comment.