Python Forum
Keypress when running a python app in a console on windows - Printable Version

+- Python Forum (https://python-forum.io)
+-- Forum: Python Coding (https://python-forum.io/forum-7.html)
+--- Forum: General Coding Help (https://python-forum.io/forum-8.html)
+--- Thread: Keypress when running a python app in a console on windows (/thread-17568.html)



Keypress when running a python app in a console on windows - Stephen - Apr-16-2019

Hi all,

I have a small python program running for the windows command window (console app) I want to detect a keypress when the console app has not got focus.

I tried several ways to do this but it never seems to work.

Steve


RE: Keypress when running a python app in a console on windows - Larz60+ - Apr-16-2019

for any key? or for a particular key?

you can use something like:
import socket

# then around code where you want to intercept keys:
    try:
       myroutine() # call your routine in place of this
    except (KeyboardInterrupt, SystemExit):
       print(f'\nKeyboard Interrupt')
       # Call routine you want to run on keypress here
    except:
       # Call routine you want to run on keypress here
       raise



RE: Keypress when running a python app in a console on windows - Stephen - Apr-16-2019

Than ks for the quick reply,

Sorry I did not specify fully, I would like to check for a few different keys like 'q' = quit or space = continue the program

I am happy to wait for a keypress then based on the return make choices but as I say when the console has lost focus it does not respond


Steve


RE: Keypress when running a python app in a console on windows - Larz60+ - Apr-16-2019

I found this:
from pynput import keyboard

class CaptureKeys:
    def __init__(self):
        pass

    def on_press(self, key):
        try:
            print('alphanumeric key {0} pressed'.format(
                key.char))
        except AttributeError:
            print('special key {0} pressed'.format(
                key))

    def on_release(self, key):
        print('{0} released'.format(
            key))
        if key == keyboard.Key.esc:
            # Stop listener
            return False

    # Collect events until released
    def main(self):
        with keyboard.Listener(
                on_press=self.on_press,
                on_release=self.on_release) as listener:
            listener.join()

    def start_listener(self):
        keyboard.Listener.start
        self.main()

if __name__ == '__main__':
    ck = CaptureKeys()
    ck.start_listener()



RE: Keypress when running a python app in a console on windows - Stephen - Apr-16-2019

Hi Larz60,

I have a large main routine, I want to be able to add code at a point where I want to wait until a key is pressed then make a decision on the resulting key value. So I am unsure how I can implement your suggestion.

my code
my code
my code
my code
    a = WaitForKeypress()
    if a == 'q':
         quit()
my code
my code
my code
my code
my code
But as I say the python window has NOT got focus.

Thanks


RE: Keypress when running a python app in a console on windows - Larz60+ - Apr-16-2019

What you need is in the code I presented. I can't help you implement it unless you post your code.


RE: Keypress when running a python app in a console on windows - Stephen - Apr-16-2019

Hi,

it's very simple test code

    print("Input choice")
    key = keyboard()
    if key == 'q':
        quit()
    if key == 'c':
        print("C pressed")
The problem is this needs to work when window has not got focus

Thanks