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

augurar

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
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.

In order to define this variable in Python you could for example call the following function prior to your call to curses.wrapper(main):

def set_shorter_esc_delay_in_os():
    os.environ.setdefault('ESCDELAY', '25')

which will set the environment variable to 25ms if it has not been set before.

See also the man page of ncurses (search for ESCDELAY).

この記事はインターネットから収集されたものであり、転載の際にはソースを示してください。

侵害の場合は、連絡してください[email protected]

編集
0

コメントを追加

0

関連記事

分類Dev

Spacemacs escape key needs delay to work

分類Dev

Where does curses inject KEY_RESIZE on endwin and refresh

分類Dev

Why does the xarray reftime key suddenly have a 1 at the end?

分類Dev

Why does '/' have an '..' entry?

分類Dev

Why does Docker have a daemon?

分類Dev

Python Curses Refreshing Text With a Loop

分類Dev

Why does C++ not have reflection?

分類Dev

Why does C++ not have reflection?

分類Dev

Why does C++ not have reflection?

分類Dev

Why std::vector does not have a release method?

分類Dev

Why does my viewmodel name have to be 'model'?

分類Dev

Why does the Javascript EventLoop have different result?

分類Dev

Why does __new__ not have classmethod decorator

分類Dev

Why does Windows have multiple Desktop views?

分類Dev

Why does overriding OnPaint() have no noticeable effect?

分類Dev

Why does Chrome have two encoding settings?

分類Dev

Does Google Chrome have some primary key in its cookies database?

分類Dev

'(key: AnyObject, value: AnyObject)' does not have a member named 'subscript'

分類Dev

Android postDelayed does not delay

分類Dev

curses.wrapper() isn't working [python]

分類Dev

get updated screen size in python curses

分類Dev

Why does an entity escape at the end of the string not show up for document.write(x) on domready?

分類Dev

Does Primary key in Dimension table have to be part of the Primary key in the Fact table?

分類Dev

Why does npm install say I have unmet dependencies?

分類Dev

Why does pthread_cond_wait have spurious wakeups?

分類Dev

Why does numpy have a corresponding function for many ndarray methods?

分類Dev

Why does AVPlayerView not have a member named "player" in Swift

分類Dev

Why does Rust have both call by value and call by reference?

分類Dev

Why does a Redux reducer have to be side-effect free?

Related 関連記事

  1. 1

    Spacemacs escape key needs delay to work

  2. 2

    Where does curses inject KEY_RESIZE on endwin and refresh

  3. 3

    Why does the xarray reftime key suddenly have a 1 at the end?

  4. 4

    Why does '/' have an '..' entry?

  5. 5

    Why does Docker have a daemon?

  6. 6

    Python Curses Refreshing Text With a Loop

  7. 7

    Why does C++ not have reflection?

  8. 8

    Why does C++ not have reflection?

  9. 9

    Why does C++ not have reflection?

  10. 10

    Why std::vector does not have a release method?

  11. 11

    Why does my viewmodel name have to be 'model'?

  12. 12

    Why does the Javascript EventLoop have different result?

  13. 13

    Why does __new__ not have classmethod decorator

  14. 14

    Why does Windows have multiple Desktop views?

  15. 15

    Why does overriding OnPaint() have no noticeable effect?

  16. 16

    Why does Chrome have two encoding settings?

  17. 17

    Does Google Chrome have some primary key in its cookies database?

  18. 18

    '(key: AnyObject, value: AnyObject)' does not have a member named 'subscript'

  19. 19

    Android postDelayed does not delay

  20. 20

    curses.wrapper() isn't working [python]

  21. 21

    get updated screen size in python curses

  22. 22

    Why does an entity escape at the end of the string not show up for document.write(x) on domready?

  23. 23

    Does Primary key in Dimension table have to be part of the Primary key in the Fact table?

  24. 24

    Why does npm install say I have unmet dependencies?

  25. 25

    Why does pthread_cond_wait have spurious wakeups?

  26. 26

    Why does numpy have a corresponding function for many ndarray methods?

  27. 27

    Why does AVPlayerView not have a member named "player" in Swift

  28. 28

    Why does Rust have both call by value and call by reference?

  29. 29

    Why does a Redux reducer have to be side-effect free?

ホットタグ

アーカイブ