Python Forum
Keypress when running a python app in a console on windows
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Keypress when running a python app in a console on windows
#1
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
Reply
#2
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
Reply
#3
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
Reply
#4
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()
Reply
#5
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
Reply
#6
What you need is in the code I presented. I can't help you implement it unless you post your code.
Reply
#7
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
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  In Console,Python anna17 0 134 Mar-23-2024, 08:24 PM
Last Post: anna17
  Python script running under windows over nssm.exe JaroslavZ 0 672 May-12-2023, 09:22 AM
Last Post: JaroslavZ
  Python Flask Realtime system printout (console) ffmpeg jttolleson 3 2,860 Apr-18-2022, 06:39 PM
Last Post: jttolleson
  batch file for running python scipt in Windows shell MaartenRo 2 1,825 Jan-21-2022, 02:36 PM
Last Post: MaartenRo
  Python library for win32 console commands eldiener 3 3,384 Aug-24-2021, 10:28 PM
Last Post: bowlofred
  Read a single event (keypress) from asyncio cygnus_X1 3 4,205 May-26-2021, 07:43 PM
Last Post: deanhystad
  Problem: Restart kernel onPydev console when trying to install a python package poppy2020 1 7,567 Nov-25-2020, 06:13 PM
Last Post: Larz60+
  to call via console a python-programm Liki 9 3,137 Nov-22-2020, 01:10 AM
Last Post: bowlofred
  My Python Console doesn´t work ModuleNotFoundError: No module named 'tokenize' RuanKishibe 1 3,077 Aug-06-2020, 10:07 PM
Last Post: deanhystad
  Running this script on Windows? Winfried 2 2,265 May-15-2020, 01:39 PM
Last Post: Winfried

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020