Skip to content

Commit

Permalink
allow for configuration of what raises a Interrupt
Browse files Browse the repository at this point in the history
  • Loading branch information
Cube707 committed Aug 15, 2022
1 parent 2661999 commit c1c735a
Show file tree
Hide file tree
Showing 5 changed files with 26 additions and 3 deletions.
11 changes: 11 additions & 0 deletions README.rst
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,17 @@ This submodule contains a list of available keys to compare against. The constan
fully portable. If a key is listed here for your platform, :code:`readkey()` can read it, and you can compare against it.


:code:`config` module
---------------------

This submodule contains configurations for :code:`readchar`. It holds Constants that are used in other parts of the code. You can override/change these
to modify its behaviour. Here is a description of the existing configs:

:code:`INTERRUPT_KEYS`
| List of keys that will result in :code:`readkey()` raising a :code:`KeyboardInterrupt`. You can modify or add to it.
| *Default:* :code:`[key.CTRL_C]`

OS Support
==========

Expand Down
4 changes: 3 additions & 1 deletion readchar/__init__.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
"""Library to easily read single chars and key strokes"""

__version__ = "4.0.1-dev0"
__all__ = ["readchar", "readkey", "key"]
__all__ = ["readchar", "readkey", "key", "config"]

from sys import platform

from . import config


if platform.startswith(("linux", "darwin", "freebsd")):
from . import _posix_key as key
Expand Down
4 changes: 3 additions & 1 deletion readchar/_posix_read.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
import sys
import termios

from .config import INTERRUPT_KEYS


# Initially taken from:
# http://code.activestate.com/recipes/134892/
Expand Down Expand Up @@ -31,7 +33,7 @@ def readkey() -> str:

c1 = readchar()

if c1 == "\x03":
if c1 in INTERRUPT_KEYS:
raise KeyboardInterrupt

if c1 != "\x1B":
Expand Down
4 changes: 3 additions & 1 deletion readchar/_win_read.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
import msvcrt

from .config import INTERRUPT_KEYS


def readchar() -> str:
"""Reads a single character from the input stream.
Expand All @@ -15,7 +17,7 @@ def readkey() -> str:

ch = readchar()

if ch == "\x03":
if ch in INTERRUPT_KEYS:
raise KeyboardInterrupt

# if it is a normal character:
Expand Down
6 changes: 6 additions & 0 deletions readchar/config.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
from typing import List

from . import _base_key as key


INTERRUPT_KEYS: List[str] = [key.CTRL_C]

0 comments on commit c1c735a

Please sign in to comment.