Repository: locustio/locust
File: locust/input_events.py (Python)

Code before:
            self.stdin = sys.stdin.fileno()
            self.tattr = termios.tcgetattr(self.stdin)
            tty.setcbreak(self.stdin, termios.TCSANOW)

        return self

    def __exit__(self, type, value, traceback):
        if isWindows:
            pass
        else:
            termios.tcsetattr(self.stdin, termios.TCSANOW, self.tattr)

    def poll(self):
        if isWindows:
            if not len(self.captured_chars) == 0:
                return self.captured_chars.pop(0)

            events_peek = self.read_handle.PeekConsoleInput(10000)

            if len(events_peek) == 0:
                return None

            if not len(events_peek) == self.cur_event_length:
                for cur_event in events_peek[self.cur_event_length :]:
                    if cur_event.EventType == KEY_EVENT:
                        if ord(cur_event.Char) == 0 or not cur_event.KeyDown:
                            pass
                        else:
                            cur_char = str(cur_event.Char)
                            self.captured_chars.append(cur_char)

                self.cur_event_length = len(events_peek)

            if not len(self.captured_chars) == 0:
                return self.captured_chars.pop(0)
            else:
                return None
        else:
            dr, dw, de = select.select([sys.stdin], [], [], 0)
            if not dr == []:
                return sys.stdin.read(1)
            return None


def input_listener(key_to_func_map):
    def input_listener_func():
        with KeyPoller() as poller:
            map = key_to_func_map
            while True:
                input = poller.poll()
                if input is not None:

Review comment (suggestion):
Most likely, this:

                        if ord(cur_event.Char) == 0 or not cur_event.KeyDown:
                            pass
                        else:
                            cur_char = str(cur_event.Char)
                            self.captured_chars.append(cur_char)

can be replaced by 

                        if ord(cur_event.Char) and cur_event.KeyDown:
                            cur_char = str(cur_event.Char)
                            self.captured_chars.append(cur_char)

Code after:
    def poll(_self):
        dr, dw, de = select.select([sys.stdin], [], [], 0)
        if not dr == []:
            return sys.stdin.read(1)
        return None


class WindowsKeyPoller:
    def __init__(self):
        self.read_handle = GetStdHandle(STD_INPUT_HANDLE)
        self.read_handle.SetConsoleMode(ENABLE_LINE_INPUT | ENABLE_ECHO_INPUT | ENABLE_PROCESSED_INPUT)
        self.cur_event_length = 0
        self.cur_keys_length = 0
        self.captured_chars = []

    def cleanup(self):
        pass

    def poll(self):
        if self.captured_chars:
            return self.captured_chars.pop(0)

        events_peek = self.read_handle.PeekConsoleInput(10000)

        if not events_peek:
            return None

        if not len(events_peek) == self.cur_event_length:
            for cur_event in events_peek[self.cur_event_length :]:
                if cur_event.EventType == KEY_EVENT:
                    if ord(cur_event.Char) and cur_event.KeyDown:
                        cur_char = str(cur_event.Char)
                        self.captured_chars.append(cur_char)

            self.cur_event_length = len(events_peek)

        if self.captured_chars:
            return self.captured_chars.pop(0)
        else:
            return None


def get_poller():
    if os.name == "nt":
        return WindowsKeyPoller()
    else:
        return UnixKeyPoller()


def input_listener(key_to_func_map):
    poller = get_poller()