4

I really need an asyncio compatible getkey() so I can

async def stuff():
    await getkey()

So when the coroutine stuff hits the await our loop stops the task, and continues on another one.

I am new to coding but there sure is such a thing somewhere?

If not, it is possible to build such a coroutine or not? The getkey() could return the pressed key value in any form. But it should have cbreak and noecho on (Don't wait for enter and Don't print the pressed key).

(clarification, no real need to continue read.)

please help me^^ I know, that this way of doing it seems unusual. Curses running in it's own thread would be the right way to go. But I can use curses only for displaying.. also I am really new to coding.. and I have no time to look into this threading thing:/ I just need my 100 lines to work fluently really fast and also only once :!

Junaga
  • 93
  • 1
  • 11
  • 2
    Possible duplicate of [How do I make python to wait for a pressed key](https://stackoverflow.com/questions/983354/how-do-i-make-python-to-wait-for-a-pressed-key) – acruma Feb 15 '18 at 09:35
  • Have a look at my answer here: https://stackoverflow.com/questions/48787563/press-esc-to-stop-and-any-other-key-to-continue-in-python/48788260 – ChatterOne Feb 15 '18 at 09:38
  • 2
    @acruma that's not what the question asks. The question is how to await for a keypress *asynchronously* – Panagiotis Kanavos Feb 15 '18 at 09:39
  • 2
    @acruma I uhm.. don't think this works asynchronously. – Junaga Feb 15 '18 at 09:39
  • @ChatterOne thanks! I am already using curses but again if I try to screen.getkey() the code is stuck :/ – Junaga Feb 15 '18 at 09:47

2 Answers2

2

If you don't want to actually wait for the keypress, one way is to use a thread to detect the keypress:

from threading import Thread
import curses

key_pressed = False

def detect_key_press():
    global key_pressed
    stdscr = curses.initscr()
    key = stdscr.getch()
    key_pressed = True

def main():
    thread = Thread(target = detect_key_press)
    thread.start()
    while not key_pressed:
        print('Hello world\r')
    curses.endwin()

main()

It's not exactly nice to use global variables, but it's a quick way to do it.

ChatterOne
  • 2,844
  • 1
  • 13
  • 22
  • wow threading looks actually quite easy... I love python I'll check that out now, just give me please half an hour to check it outxd but have my upvote for now! – Junaga Feb 15 '18 at 10:17
  • You're doing `initscr` inside one thread and `endwin` inside another. That doesn't feel well. – ogurets May 04 '20 at 23:03
0

Here's a solution using keyboard instead of curses:

import keyboard
import time
from threading import Thread

key_pressed = False

def detect_key_press():
    global key_pressed
    while not keyboard.is_pressed('q'):
        pass
    key_pressed = True

def main():
    thread = Thread(target = detect_key_press)
    thread.start()
    while not key_pressed:
        print("Waiting for keypress...")
        time.sleep(1)
    
main()

Starts a thread inside main that waits for keypress

Real
  • 178
  • 1
  • 8