Home:ALL Converter>Why does the escape key have a delay in Python curses?

Why does the escape key have a delay in Python curses?

Ask Time:2014-12-09T13:29:50         Author:augurar

Json Formatter

In the Python curses module, I have observed that there is a roughly 1-second delay between pressing the esc key and getch() returning. This delay does not seem to occur for other keys. Why does this happen and what can I do about it?

Test case:

import curses
import time

def get_delay(window, key):
    while True:
        start = time.time()
        ch = window.getch()
        end = time.time()
        if ch == key:
            return end-start

def main(stdscr):
    stdscr.clear()
    stdscr.nodelay(1)

    stdscr.addstr("Press ESC")
    esc_delay = get_delay(stdscr, 27)

    stdscr.addstr("\nPress SPACE")
    space_delay = get_delay(stdscr, ord(' '))

    return esc_delay, space_delay

if __name__ == '__main__':
    esc_delay, space_delay = curses.wrapper(main)
    print("Escape delay: {} ms".format(esc_delay*1000))
    print("Space delay: {} ms".format(space_delay*1000))

Results:

Escape delay: 1001.09195709 ms
Space delay: 0.00596046447754 ms

Author:augurar,eproduced under the CC 4.0 BY-SA copyright license with a link to the original source and this disclaimer.
Link to original article:https://stackoverflow.com/questions/27372068/why-does-the-escape-key-have-a-delay-in-python-curses
Salo :

In order to customize the Esc delay you can set the environment variable ESCDELAY which curses uses to determine the time in milliseconds it waits before it delivers the Escape Key.\n\nIn order to define this variable in Python you could for example call the following function prior to your call to curses.wrapper(main):\n\ndef set_shorter_esc_delay_in_os():\n os.environ.setdefault('ESCDELAY', '25')\n\n\nwhich will set the environment variable to 25ms if it has not been set before.\n\nSee also the man page of ncurses (search for ESCDELAY).",
2015-01-19T08:31:56
yy